2018-05-08 00:58:46 -04:00
|
|
|
|
using IW4MAdmin.Application.API.Master;
|
|
|
|
|
using SharedLibraryCore;
|
2020-01-26 19:06:50 -05:00
|
|
|
|
using SharedLibraryCore.Interfaces;
|
2018-04-22 16:04:18 -04:00
|
|
|
|
using System;
|
|
|
|
|
using System.Collections.Generic;
|
|
|
|
|
using System.Globalization;
|
|
|
|
|
using System.IO;
|
|
|
|
|
using System.Text;
|
2020-11-11 18:31:26 -05:00
|
|
|
|
using Microsoft.Extensions.Logging;
|
|
|
|
|
using SharedLibraryCore.Configuration;
|
|
|
|
|
using ILogger = Microsoft.Extensions.Logging.ILogger;
|
2018-04-22 16:04:18 -04:00
|
|
|
|
|
|
|
|
|
namespace IW4MAdmin.Application.Localization
|
|
|
|
|
{
|
2020-11-11 18:31:26 -05:00
|
|
|
|
public static class Configure
|
2018-04-22 16:04:18 -04:00
|
|
|
|
{
|
2020-11-11 18:31:26 -05:00
|
|
|
|
public static ITranslationLookup Initialize(ILogger logger, IMasterApi apiInstance, ApplicationConfiguration applicationConfiguration)
|
2018-04-22 16:04:18 -04:00
|
|
|
|
{
|
2020-11-11 18:31:26 -05:00
|
|
|
|
var useLocalTranslation = applicationConfiguration?.UseLocalTranslations ?? true;
|
|
|
|
|
var customLocale = applicationConfiguration?.EnableCustomLocale ?? false
|
|
|
|
|
? (applicationConfiguration.CustomLocale ?? "en-US")
|
|
|
|
|
: "en-US";
|
|
|
|
|
var currentLocale = string.IsNullOrEmpty(customLocale) ? CultureInfo.CurrentCulture.Name : customLocale;
|
|
|
|
|
var localizationFiles = Directory.GetFiles(Path.Join(Utilities.OperatingDirectory, "Localization"), $"*.{currentLocale}.json");
|
2018-05-05 16:36:26 -04:00
|
|
|
|
|
2020-01-26 19:06:50 -05:00
|
|
|
|
if (!useLocalTranslation)
|
2018-05-08 00:58:46 -04:00
|
|
|
|
{
|
2019-04-11 21:43:05 -04:00
|
|
|
|
try
|
|
|
|
|
{
|
2020-06-30 17:39:32 -04:00
|
|
|
|
var localization = apiInstance.GetLocalization(currentLocale).Result;
|
2019-04-11 21:43:05 -04:00
|
|
|
|
Utilities.CurrentLocalization = localization;
|
2020-01-26 19:06:50 -05:00
|
|
|
|
return localization.LocalizationIndex;
|
2019-04-11 21:43:05 -04:00
|
|
|
|
}
|
|
|
|
|
|
2020-11-11 18:31:26 -05:00
|
|
|
|
catch (Exception ex)
|
2019-04-11 21:43:05 -04:00
|
|
|
|
{
|
|
|
|
|
// the online localization failed so will default to local files
|
2020-11-11 18:31:26 -05:00
|
|
|
|
logger.LogWarning(ex, "Could not download latest translations");
|
2019-04-11 21:43:05 -04:00
|
|
|
|
}
|
2018-05-08 00:58:46 -04:00
|
|
|
|
}
|
|
|
|
|
|
2018-05-05 16:36:26 -04:00
|
|
|
|
// culture doesn't exist so we just want language
|
|
|
|
|
if (localizationFiles.Length == 0)
|
|
|
|
|
{
|
2018-10-10 20:22:08 -04:00
|
|
|
|
localizationFiles = Directory.GetFiles(Path.Join(Utilities.OperatingDirectory, "Localization"), $"*.{currentLocale.Substring(0, 2)}*.json");
|
2018-05-05 16:36:26 -04:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// language doesn't exist either so defaulting to english
|
|
|
|
|
if (localizationFiles.Length == 0)
|
|
|
|
|
{
|
2018-10-10 20:22:08 -04:00
|
|
|
|
localizationFiles = Directory.GetFiles(Path.Join(Utilities.OperatingDirectory, "Localization"), "*.en-US.json");
|
2018-05-05 16:36:26 -04:00
|
|
|
|
}
|
2018-04-22 16:04:18 -04:00
|
|
|
|
|
2018-05-05 16:36:26 -04:00
|
|
|
|
// this should never happen unless the localization folder is empty
|
|
|
|
|
if (localizationFiles.Length == 0)
|
2018-04-22 16:04:18 -04:00
|
|
|
|
{
|
2018-05-05 16:36:26 -04:00
|
|
|
|
throw new Exception("No localization files were found");
|
2018-04-22 16:04:18 -04:00
|
|
|
|
}
|
|
|
|
|
|
2018-05-05 16:36:26 -04:00
|
|
|
|
var localizationDict = new Dictionary<string, string>();
|
|
|
|
|
|
|
|
|
|
foreach (string filePath in localizationFiles)
|
2018-04-22 16:04:18 -04:00
|
|
|
|
{
|
2018-05-05 16:36:26 -04:00
|
|
|
|
var localizationContents = File.ReadAllText(filePath, Encoding.UTF8);
|
|
|
|
|
var eachLocalizationFile = Newtonsoft.Json.JsonConvert.DeserializeObject<SharedLibraryCore.Localization.Layout>(localizationContents);
|
2020-11-19 21:48:25 -05:00
|
|
|
|
if (eachLocalizationFile == null)
|
|
|
|
|
{
|
|
|
|
|
continue;
|
|
|
|
|
}
|
2018-05-05 16:36:26 -04:00
|
|
|
|
|
|
|
|
|
foreach (var item in eachLocalizationFile.LocalizationIndex.Set)
|
|
|
|
|
{
|
|
|
|
|
if (!localizationDict.TryAdd(item.Key, item.Value))
|
|
|
|
|
{
|
2020-11-11 18:31:26 -05:00
|
|
|
|
logger.LogError("Could not add locale string {key} to localization", item.Key);
|
2018-05-05 16:36:26 -04:00
|
|
|
|
}
|
|
|
|
|
}
|
2018-04-22 16:04:18 -04:00
|
|
|
|
}
|
|
|
|
|
|
2018-05-05 16:36:26 -04:00
|
|
|
|
Utilities.CurrentLocalization = new SharedLibraryCore.Localization.Layout(localizationDict)
|
|
|
|
|
{
|
|
|
|
|
LocalizationName = currentLocale,
|
|
|
|
|
};
|
2020-01-26 19:06:50 -05:00
|
|
|
|
|
|
|
|
|
return Utilities.CurrentLocalization.LocalizationIndex;
|
2018-04-22 16:04:18 -04:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|