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;
|
|
|
|
|
|
|
|
|
|
namespace IW4MAdmin.Application.Localization
|
|
|
|
|
{
|
|
|
|
|
public class Configure
|
|
|
|
|
{
|
2020-01-26 19:06:50 -05:00
|
|
|
|
public static ITranslationLookup Initialize(bool useLocalTranslation, string customLocale = null)
|
2018-04-22 16:04:18 -04:00
|
|
|
|
{
|
2018-05-05 16:36:26 -04:00
|
|
|
|
string currentLocale = string.IsNullOrEmpty(customLocale) ? CultureInfo.CurrentCulture.Name : customLocale;
|
2018-10-10 20:22:08 -04:00
|
|
|
|
string[] 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
|
|
|
|
|
{
|
|
|
|
|
var api = Endpoint.Get();
|
|
|
|
|
var localization = api.GetLocalization(currentLocale).Result;
|
|
|
|
|
Utilities.CurrentLocalization = localization;
|
2020-01-26 19:06:50 -05:00
|
|
|
|
return localization.LocalizationIndex;
|
2019-04-11 21:43:05 -04:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
catch (Exception)
|
|
|
|
|
{
|
|
|
|
|
// the online localization failed so will default to local files
|
|
|
|
|
}
|
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);
|
|
|
|
|
|
|
|
|
|
foreach (var item in eachLocalizationFile.LocalizationIndex.Set)
|
|
|
|
|
{
|
|
|
|
|
if (!localizationDict.TryAdd(item.Key, item.Value))
|
|
|
|
|
{
|
2018-10-06 12:47:14 -04:00
|
|
|
|
Program.ServerManager.GetLogger(0).WriteError($"Could not add locale string {item.Key} to localization");
|
2018-05-05 16:36:26 -04:00
|
|
|
|
}
|
|
|
|
|
}
|
2018-04-22 16:04:18 -04:00
|
|
|
|
}
|
|
|
|
|
|
2018-10-10 20:22:08 -04:00
|
|
|
|
string localizationFile = $"{Path.Join(Utilities.OperatingDirectory, "Localization")}{Path.DirectorySeparatorChar}IW4MAdmin.{currentLocale}-{currentLocale.ToUpper()}.json";
|
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
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|