Skip to content

Commit

Permalink
feat: set localization based on game or system language on first init (
Browse files Browse the repository at this point in the history
  • Loading branch information
IgorMCesar authored Oct 28, 2024
1 parent 434fdfc commit d1419d7
Show file tree
Hide file tree
Showing 5 changed files with 125 additions and 3 deletions.
26 changes: 26 additions & 0 deletions Hearthstone Deck Tracker/Config.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down Expand Up @@ -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)
Expand Down
10 changes: 8 additions & 2 deletions Hearthstone Deck Tracker/Enums/Language.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -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)")]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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));
Expand Down
46 changes: 46 additions & 0 deletions Hearthstone Deck Tracker/Utility/LocUtil.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
using System;
using System.Collections.Generic;
using System.Globalization;
using Microsoft.Win32;
using WPFLocalizeExtension.Engine;

namespace Hearthstone_Deck_Tracker.Utility
Expand Down Expand Up @@ -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());
Expand Down
Original file line number Diff line number Diff line change
@@ -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; }
}
}

0 comments on commit d1419d7

Please sign in to comment.