113 lines
4.0 KiB
C#
113 lines
4.0 KiB
C#
using Thermo.Active.Model.DTOModels;
|
|
using System.Collections.Generic;
|
|
using System.Globalization;
|
|
using System.IO;
|
|
using System.Linq;
|
|
using System.Xml.Linq;
|
|
using System.Xml.Schema;
|
|
using static Thermo.Active.Model.Constants;
|
|
|
|
namespace Thermo.Active.Utils
|
|
{
|
|
public static class LanguageController
|
|
{
|
|
private static bool FileIsValid = true;
|
|
|
|
public static bool LanguageIsAvailable(string language)
|
|
{
|
|
// Create a valid language
|
|
CultureInfo lang = CultureInfo.CreateSpecificCulture(language);
|
|
// File path with 2 letter iso language
|
|
string filePath = LANGUAGE_PACK_DIRECTORY + lang.TwoLetterISOLanguageName + ".xml";
|
|
|
|
if (!File.Exists(filePath))
|
|
return false;
|
|
|
|
return true;
|
|
}
|
|
|
|
public static Dictionary<string, string> GetTranslationsFromFile(string language)
|
|
{
|
|
CultureInfo lang = CultureInfo.CreateSpecificCulture(language);
|
|
|
|
// File path with 2 letter iso language
|
|
string filePath = LANGUAGE_PACK_DIRECTORY + lang.TwoLetterISOLanguageName + ".xml";
|
|
|
|
// Open file reader
|
|
XDocument xmlLanguageFile = XDocument.Load(filePath);
|
|
|
|
//if (!ValidateTranslationsFile(xmlLanguageFile, LANGUAGE_SCHEMA_PATH))
|
|
// return null;
|
|
|
|
// Read file content
|
|
return xmlLanguageFile
|
|
.Root?
|
|
.Elements()
|
|
.ToDictionary(x => x.Name.ToString(), x => x.Value); // Populate dictionary
|
|
}
|
|
|
|
public static List<DTOLanguageModel> GetLanguageListFromDirectory()
|
|
{
|
|
// Check if directory exists
|
|
if (Directory.Exists(LANGUAGE_PACK_DIRECTORY))
|
|
{
|
|
// Check if directory is empty
|
|
if (Directory.EnumerateFileSystemEntries(LANGUAGE_PACK_DIRECTORY).Any())
|
|
{
|
|
// Read all the files in the lang directory
|
|
return Directory.GetFiles(LANGUAGE_PACK_DIRECTORY, "*.xml", SearchOption.TopDirectoryOnly)
|
|
.Select(Path.GetFileNameWithoutExtension) // Get only fileName without extensions
|
|
.Where(x => IsValidLanguage(x)) // Filter file names by valid language
|
|
.Select(x =>
|
|
{
|
|
CultureInfo info = new CultureInfo(x);
|
|
DTOLanguageModel language = new DTOLanguageModel() // Create language model
|
|
{
|
|
IsoId = info.TwoLetterISOLanguageName,
|
|
Name = char.ToUpper(info.NativeName[0]) + info.NativeName.Substring(1)
|
|
};
|
|
return language;
|
|
})
|
|
.ToList();
|
|
}
|
|
}
|
|
|
|
return null;
|
|
}
|
|
|
|
public static bool IsValidLanguage(string language)
|
|
{
|
|
CultureInfo info = CultureInfo
|
|
.GetCultures(CultureTypes.AllCultures)
|
|
.FirstOrDefault(l => l.TwoLetterISOLanguageName == language.ToLower()); // Find user language from system language
|
|
|
|
if (info == null)
|
|
return false;
|
|
|
|
return true;
|
|
}
|
|
|
|
private static bool ValidateTranslationsFile(XDocument xmlLanguageFile, string validatorFilePath)
|
|
{
|
|
FileIsValid = true;
|
|
|
|
// Read validation file
|
|
XmlSchemaSet readerSettings = new XmlSchemaSet();
|
|
|
|
// Add Schema
|
|
readerSettings.Add(null, validatorFilePath);
|
|
|
|
// Validate file
|
|
xmlLanguageFile.Validate(readerSettings, ValidationHandler);
|
|
|
|
return FileIsValid;
|
|
}
|
|
|
|
// Validation XML Callback
|
|
private static void ValidationHandler(object sender, ValidationEventArgs e)
|
|
{
|
|
if (e.Severity == XmlSeverityType.Error)
|
|
FileIsValid = false;
|
|
}
|
|
}
|
|
} |