0
0
mirror of https://github.com/sp-tarkov/launcher.git synced 2025-02-12 17:10:44 -05:00

update dictionary extensions and locale provider

This commit is contained in:
IsWaffle 2023-12-09 17:23:13 -05:00
parent 2443ab4a24
commit 6489ef4793
2 changed files with 66 additions and 33 deletions

View File

@ -14,24 +14,39 @@ namespace Aki.Launcher.Extensions
{
public static class DictionaryExtensions
{
public static TKey GetKeyByValue<TKey, TValue>(this Dictionary<TKey, TValue> Dic, TValue value)
public static TKey GetKeyByValue<TKey, TValue>(this Dictionary<TKey, TValue> dic, TValue value)
{
List<TKey> Keys = Dic.Keys.ToList();
List<TKey> 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<TKey, TValue>(this Dictionary<TKey, TValue> 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;
}
}
}

View File

@ -24,11 +24,11 @@ namespace Aki.Launcher.Helpers
{
public static string DefaultLocaleFolderPath = Path.Join(Environment.CurrentDirectory, "Aki_Data", "Launcher", "Locales");
public static Dictionary<string, string> LocaleNameDictionary = GetLocaleDictionary();
public static Dictionary<string, string> 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<LocaleData>(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<string, string> GetLocaleDictionary()
public static Dictionary<string, string> GetLocaleDictionary(string property)
{
List<FileInfo> localeFiles = new List<FileInfo>(Directory.GetFiles(DefaultLocaleFolderPath).Select(x => new FileInfo(x)).ToList());
Dictionary<string, string> localeDictionary = new Dictionary<string, string>();
foreach (FileInfo file in localeFiles)
{
localeDictionary.Add(file.Name.Replace(".json", ""), Json.GetPropertyByName<string>(file.FullName, "native_name"));
localeDictionary.Add(file.Name.Replace(".json", ""), Json.GetPropertyByName<string>(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