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

Merge pull request 'fix/first-run-locale-load' (!32) from waffle.lord/Launcher:fix/first-run-locale-load into master

Reviewed-on: SPT-AKI/Launcher#32
This commit is contained in:
IsWaffle 2023-12-09 22:28:04 +00:00
commit a595111bbd
15 changed files with 107 additions and 65 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

@ -16,7 +16,6 @@ using System.ComponentModel;
using System.Globalization;
using System.IO;
using System.Linq;
using System.Text.RegularExpressions;
namespace Aki.Launcher.Helpers
{
@ -24,11 +23,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 +35,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 +59,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 +86,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 +188,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 +214,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

View File

@ -1,4 +1,5 @@
{
"ietf_tag": "zh-hans",
"native_name": "简体中文",
"retry": "重试",
"server_connecting": "连接中",

View File

@ -1,4 +1,5 @@
{
"ietf_tag": "zh-hant",
"native_name": "繁體中文",
"retry": "重試",
"server_connecting": "正在連接伺服器",

View File

@ -1,4 +1,5 @@
{
"ietf_tag": "en",
"native_name": "English",
"retry": "Retry",
"server_connecting": "Connecting",

View File

@ -1,4 +1,5 @@
{
"ietf_tag": "fr",
"native_name": "Français",
"retry": "Réessayer",
"server_connecting": "Connexion",

View File

@ -1,4 +1,5 @@
{
"ietf_tag": "de",
"native_name": "Deutsch",
"retry": "Erneut versuchen",
"server_connecting": "Verbinden",

View File

@ -1,4 +1,5 @@
{
"ietf_tag": "it",
"native_name": "Italiano",
"retry": "Riprova",
"server_connecting": "Connessione in corso",

View File

@ -1,4 +1,5 @@
{
"ietf_tag": "ja",
"native_name": "日本語",
"retry": "リトライ",
"server_connecting": "接続中",

View File

@ -1,4 +1,5 @@
{
"ietf_tag": "ko",
"native_name": "한국어",
"retry": "재시도",
"server_connecting": "연결 중",

View File

@ -1,4 +1,5 @@
{
"ietf_tag": "pl",
"native_name": "Polski",
"retry": "Ponów",
"server_connecting": "Łączenie",

View File

@ -1,4 +1,5 @@
{
"ietf_tag": "ru",
"native_name": "Русский",
"retry": "Повторить",
"server_connecting": "Соединение",

View File

@ -1,4 +1,5 @@
{
"ietf_tag": "es",
"native_name": "Español",
"retry": "Reintentar",
"server_connecting": "Conectando",

View File

@ -7,6 +7,10 @@ using System.IO;
using Splat;
using Aki.Launcher.Models.Aki;
using Aki.Launcher.Helpers;
using Aki.Launcher.ViewModels.Dialogs;
using Avalonia.Threading;
using dialogHost = DialogHost.DialogHost;
namespace Aki.Launcher.ViewModels
{
@ -29,6 +33,32 @@ namespace Aki.Launcher.ViewModels
LauncherSettingsProvider.Instance.AllowSettings = true;
if (LauncherSettingsProvider.Instance.FirstRun)
{
Dispatcher.UIThread.InvokeAsync(async () =>
{
LauncherSettingsProvider.Instance.FirstRun = false;
LocalizationProvider.TryAutoSetLocale();
var viewModel = new ConfirmationDialogViewModel(this,
LocalizationProvider.Instance.copy_live_settings_question,
LocalizationProvider.Instance.yes,
LocalizationProvider.Instance.no);
var confirmCopySettings = await dialogHost.Show(viewModel);
if (confirmCopySettings is bool and true)
{
var settingsVm = new SettingsViewModel(this);
await settingsVm.ResetGameSettingsCommand();
}
LauncherSettingsProvider.Instance.SaveSettings();
});
}
this.WhenActivated((CompositeDisposable disposables) =>
{
Router.Navigate.Execute(new ConnectServerViewModel(this));

View File

@ -8,7 +8,6 @@ using System.Threading.Tasks;
using Aki.Launcher.Attributes;
using Aki.Launcher.ViewModels.Dialogs;
using Avalonia.Threading;
using System.Reactive.Disposables;
using System.Diagnostics;
using System.IO;
using Aki.Launcher.Models.Aki;
@ -62,36 +61,6 @@ namespace Aki.Launcher.ViewModels
public ProfileViewModel(IScreen Host) : base(Host)
{
this.WhenActivated((CompositeDisposable disposables) =>
{
Task.Run(async () =>
{
await GameVersionCheck();
await Dispatcher.UIThread.InvokeAsync(async () =>
{
if (LauncherSettingsProvider.Instance.FirstRun)
{
LauncherSettingsProvider.Instance.FirstRun = false;
LauncherSettingsProvider.Instance.SaveSettings();
var confirmCopySettings = await ShowDialog(new ConfirmationDialogViewModel(Host,
LocalizationProvider.Instance.copy_live_settings_question,
LocalizationProvider.Instance.yes,
LocalizationProvider.Instance.no));
if (confirmCopySettings != null && confirmCopySettings is bool confirmed && confirmed)
{
var settingsVM = new SettingsViewModel(Host);
await settingsVM.ResetGameSettingsCommand();
}
}
});
});
});
// cache and load side image if profile has a side
if(AccountManager.SelectedProfileInfo != null && AccountManager.SelectedProfileInfo.Side != null)
{