From 6489ef479346e4b07fef3d0fd7e562e32ef66b28 Mon Sep 17 00:00:00 2001 From: "waffle.lord" Date: Sat, 9 Dec 2023 17:23:13 -0500 Subject: [PATCH] update dictionary extensions and locale provider --- .../Extensions/DictionaryExtensions.cs | 29 ++++++-- .../Helpers/LocalizationProvider.cs | 70 ++++++++++++------- 2 files changed, 66 insertions(+), 33 deletions(-) diff --git a/project/Aki.Launcher.Base/Extensions/DictionaryExtensions.cs b/project/Aki.Launcher.Base/Extensions/DictionaryExtensions.cs index d6d82db..bb6fb8f 100644 --- a/project/Aki.Launcher.Base/Extensions/DictionaryExtensions.cs +++ b/project/Aki.Launcher.Base/Extensions/DictionaryExtensions.cs @@ -14,24 +14,39 @@ namespace Aki.Launcher.Extensions { public static class DictionaryExtensions { - public static TKey GetKeyByValue(this Dictionary Dic, TValue value) + public static TKey GetKeyByValue(this Dictionary dic, TValue value) { - List Keys = Dic.Keys.ToList(); + List keys = dic.Keys.ToList(); - for (int x = 0; x < Keys.Count(); x++) + for (var x = 0; x < keys.Count; x++) { - TValue tempValue; - - if (Dic.TryGetValue(Keys[x], out tempValue)) + if (dic.TryGetValue(keys[x], out var tempValue)) { if (tempValue != null && tempValue.Equals(value)) { - return Keys[x]; + return keys[x]; } } } return default; } + + public static TKey GetKeyByInput(this Dictionary dic, + string input) + { + var keys = dic.Keys.ToList(); + var values = dic.Values.ToList(); + + for (var x = 0; x < dic.Count; x++) + { + if (values[x] is string s && (input.ToLower() == s.ToLower() || input.ToLower().StartsWith(s.ToLower()))) + { + return keys[x]; + } + } + + return default; + } } } diff --git a/project/Aki.Launcher.Base/Helpers/LocalizationProvider.cs b/project/Aki.Launcher.Base/Helpers/LocalizationProvider.cs index 16aab96..afd78b6 100644 --- a/project/Aki.Launcher.Base/Helpers/LocalizationProvider.cs +++ b/project/Aki.Launcher.Base/Helpers/LocalizationProvider.cs @@ -24,11 +24,11 @@ namespace Aki.Launcher.Helpers { public static string DefaultLocaleFolderPath = Path.Join(Environment.CurrentDirectory, "Aki_Data", "Launcher", "Locales"); - public static Dictionary LocaleNameDictionary = GetLocaleDictionary(); + public static Dictionary LocaleNameDictionary = GetLocaleDictionary("native_name"); public static event EventHandler LocaleChanged = delegate { }; - public static void LoadLocaleFromFile(string localeName) + public static void LoadLocalByName(string localeName) { string localeRomanName = LocaleNameDictionary.GetKeyByValue(localeName); @@ -36,7 +36,12 @@ namespace Aki.Launcher.Helpers { localeRomanName = localeName; } + + LoadLocaleFromFile(localeRomanName); + } + public static void LoadLocaleFromFile(string localeRomanName) + { LocaleData newLocale = Json.LoadClassWithoutSaving(Path.Join(DefaultLocaleFolderPath, $"{localeRomanName}.json")); if (newLocale != null) @@ -55,30 +60,24 @@ namespace Aki.Launcher.Helpers //could possibly raise an event here to say why the local wasn't changed. } - private static string GetSystemLocale() - { - string UIlocaleName = CultureInfo.CurrentUICulture.DisplayName; - - var regexMatch = Regex.Match(UIlocaleName, @"^(\w+)"); - - if (regexMatch.Groups.Count == 2) - { - string localRomanName = LocaleNameDictionary.GetValueOrDefault(regexMatch.Groups[1].Value, ""); - - bool localExists = GetAvailableLocales().Where(x => x == localRomanName).Count() > 0; - - if (localExists) - { - return localRomanName; - } - } - - return "English"; - } - public static void TryAutoSetLocale() { - LoadLocaleFromFile(GetSystemLocale()); + // get local dictionary based on ietf_tag property in locale files. like: ("English", "en") + // "English" being the file name + var localeTagDictionary = GetLocaleDictionary("ietf_tag"); + + // get system locale. Like: "en-US" + var tag = CultureInfo.CurrentUICulture.IetfLanguageTag; + + // get the locale file name from the dictionary based on the input tag. If it matches, or starts with the value + var localeRomanName = localeTagDictionary.GetKeyByInput(tag); + + if (String.IsNullOrEmpty(localeRomanName)) + { + localeRomanName = "English"; + } + + LoadLocaleFromFile(localeRomanName); } public static LocaleData GenerateEnglishLocale() @@ -88,6 +87,7 @@ namespace Aki.Launcher.Helpers LocaleData englishLocale = new LocaleData(); #region Set All English Defaults + englishLocale.ietf_tag = "en"; englishLocale.native_name = "English"; englishLocale.retry = "Retry"; englishLocale.server_connecting = "Connecting"; @@ -189,14 +189,14 @@ namespace Aki.Launcher.Helpers return englishLocale; } - public static Dictionary GetLocaleDictionary() + public static Dictionary GetLocaleDictionary(string property) { List localeFiles = new List(Directory.GetFiles(DefaultLocaleFolderPath).Select(x => new FileInfo(x)).ToList()); Dictionary localeDictionary = new Dictionary(); foreach (FileInfo file in localeFiles) { - localeDictionary.Add(file.Name.Replace(".json", ""), Json.GetPropertyByName(file.FullName, "native_name")); + localeDictionary.Add(file.Name.Replace(".json", ""), Json.GetPropertyByName(file.FullName, property)); } return localeDictionary; @@ -215,6 +215,24 @@ namespace Aki.Launcher.Helpers #region All Properties + #region ietf_tag + + private string _ietf_tag; + + public string ietf_tag + { + get => _ietf_tag; + set + { + if (_ietf_tag != value) + { + _ietf_tag = value; + RaisePropertyChanged(nameof(ietf_tag)); + } + } + } + #endregion + #region native_name private string _native_name; public string native_name