diff --git a/Hearthstone Deck Tracker/Config.cs b/Hearthstone Deck Tracker/Config.cs index feb3e3613..e77858c8a 100644 --- a/Hearthstone Deck Tracker/Config.cs +++ b/Hearthstone Deck Tracker/Config.cs @@ -6,6 +6,7 @@ using System.Linq; using System.Xml.Serialization; using Hearthstone_Deck_Tracker.Enums; +using Hearthstone_Deck_Tracker.HsReplay; using Hearthstone_Deck_Tracker.Utility; using Hearthstone_Deck_Tracker.Utility.Logging; @@ -1374,6 +1375,31 @@ public static void Load() { if(Instance.ConfigDir != string.Empty) Directory.CreateDirectory(Instance.ConfigDir); + + var systemLanguage = LocUtil.GetWindowsDisplayLanguageFromRegistry(); + var gameLanguage = LocUtil.GetHearthstoneLanguageFromRegistry(); + var supportedGameLanguages = Helper.LanguageDict.Values.Where(x => x != "enGB").ToList(); + + if(supportedGameLanguages.Contains(gameLanguage)) + { + Instance.Localization = (Language)Enum.Parse(typeof(Language), gameLanguage); + Instance.SelectedLanguage = gameLanguage; + HSReplayNetClientAnalytics.OnSetInitialConfigLanguage("game_language", gameLanguage, systemLanguage, + gameLanguage); + } + else if(supportedGameLanguages.Contains(systemLanguage)) + { + Instance.Localization = (Language)Enum.Parse(typeof(Language), systemLanguage); + Instance.SelectedLanguage = systemLanguage; + HSReplayNetClientAnalytics.OnSetInitialConfigLanguage("system_language", systemLanguage, systemLanguage, + gameLanguage); + } + else + { + HSReplayNetClientAnalytics.OnSetInitialConfigLanguage("default_language", "enUS", systemLanguage, + gameLanguage); + } + Save(); } #if(!SQUIRREL) diff --git a/Hearthstone Deck Tracker/Enums/Language.cs b/Hearthstone Deck Tracker/Enums/Language.cs index 18ca0e650..1fe16988c 100644 --- a/Hearthstone Deck Tracker/Enums/Language.cs +++ b/Hearthstone Deck Tracker/Enums/Language.cs @@ -8,9 +8,13 @@ public enum Language enUS, [Description("German")] deDE, + [Description("Italian")] + itIT, [Description("French")] frFR, - [Description("Spanish")] + [Description("Spanish (Mexico)")] + esMX, + [Description("Spanish (Spain)")] esES, [Description("Japanese")] jaJP, @@ -23,9 +27,11 @@ public enum Language [Description("Portuguese (Portugal)")] ptPT, [Description("Ukrainian")] - ukUA, + ukUA, [Description("Russian")] ruRU, + [Description("Thai")] + thTH, [Description("Chinese (Simplified)")] zhCN, [Description("Chinese (Traditional)")] diff --git a/Hearthstone Deck Tracker/HsReplay/HSReplayNetClientAnalytics.cs b/Hearthstone Deck Tracker/HsReplay/HSReplayNetClientAnalytics.cs index 7caa87d31..0177ee19c 100644 --- a/Hearthstone Deck Tracker/HsReplay/HSReplayNetClientAnalytics.cs +++ b/Hearthstone Deck Tracker/HsReplay/HSReplayNetClientAnalytics.cs @@ -141,7 +141,12 @@ public static void OnMercenariesMatchEnds(GameStats gameStats, GameMetrics gameM else TrackAction(new EndMatchMercenariesAction(matchResult, gameType, gameMetrics)); } - + + public static void OnSetInitialConfigLanguage(string source, string language, string systemLanguage, string gameLanguage) + { + TrackAction(new SetInitialLanguageAction(source, language, systemLanguage, gameLanguage)); + } + public static void OnCopyDeck(CopyDeckAction.Action action) { TrackAction(new CopyDeckAction(Franchise.HSConstructed, action)); diff --git a/Hearthstone Deck Tracker/Utility/LocUtil.cs b/Hearthstone Deck Tracker/Utility/LocUtil.cs index c64904852..2b2172684 100644 --- a/Hearthstone Deck Tracker/Utility/LocUtil.cs +++ b/Hearthstone Deck Tracker/Utility/LocUtil.cs @@ -1,6 +1,7 @@ using System; using System.Collections.Generic; using System.Globalization; +using Microsoft.Win32; using WPFLocalizeExtension.Engine; namespace Hearthstone_Deck_Tracker.Utility @@ -33,6 +34,51 @@ private static CultureInfo GetCultureInfoFromLocale(string locale) return CultureInfo.GetCultureInfo(locale); } + public static string GetWindowsDisplayLanguageFromRegistry() + { + try + { + const string subKey = @"Control Panel\International\User Profile"; + const string valueName = "Languages"; + + using var key = Registry.CurrentUser.OpenSubKey(subKey); + var value = key?.GetValue(valueName); + if (value is string[] { Length: > 0 } languages) + { + return languages[0].Replace("-", string.Empty); + } + } + catch (Exception ex) + { + Console.WriteLine($@"An error occurred while accesing registry: {ex.Message}"); + } + + return string.Empty; + } + + public static string GetHearthstoneLanguageFromRegistry() + { + try + { + const string subKey = @"SOFTWARE\Blizzard Entertainment\Battle.net\Launch Options\WTCG"; + const string valueName = "LOCALE"; + + using var key = Registry.CurrentUser.OpenSubKey(subKey); + + var value = key?.GetValue(valueName); + if(value is string locale) + { + return locale.Replace("-", string.Empty); + } + } + catch (Exception ex) + { + Console.WriteLine($@"An error occurred while accesing registry: {ex.Message}"); + } + + return string.Empty; + } + public static void UpdateCultureInfo() { LocalizeDictionary.Instance.Culture = GetCultureInfoFromLocale(Config.Instance.Localization.ToString()); diff --git a/Hearthstone Deck Tracker/Utility/ValueMoments/Actions/SetInitialLanguageAction.cs b/Hearthstone Deck Tracker/Utility/ValueMoments/Actions/SetInitialLanguageAction.cs new file mode 100644 index 000000000..bb0929c35 --- /dev/null +++ b/Hearthstone Deck Tracker/Utility/ValueMoments/Actions/SetInitialLanguageAction.cs @@ -0,0 +1,39 @@ +using Hearthstone_Deck_Tracker.Utility.ValueMoments.Actions.Action; +using Hearthstone_Deck_Tracker.Utility.ValueMoments.Enums; +using Newtonsoft.Json; + +namespace Hearthstone_Deck_Tracker.Utility.ValueMoments.Actions +{ + public class SetInitialLanguageAction : VMAction + { + public SetInitialLanguageAction(string languageSource, string language, string systemLanguage, string gameLanguage) + : base(Franchise.HSConstructed, null, null) + { + Language = language; + LanguageSource = languageSource; + SystemLanguage = systemLanguage; + GameLanguage = gameLanguage; + } + + [JsonProperty("name")] + public override string Name => "Set Initial Language"; + + [JsonProperty("action_source")] + public override ActionSource Source { get; } + + [JsonProperty("action_type")] + public override string Type => "Set Initial Language"; + + [JsonProperty("language")] + public string Language { get; } + + [JsonProperty("language_source")] + public string LanguageSource { get; } + + [JsonProperty("system_language")] + public string SystemLanguage { get; } + + [JsonProperty("game_language")] + public string GameLanguage { get; } + } +}