Improved LocaleService

Better logging when desired locale not found
Made private methods protected
Made some methods static
Added edge-case handling of ZH locale
This commit is contained in:
Chomp
2025-06-13 09:41:06 +01:00
parent 610e971568
commit ad0fee1dbf
@@ -14,8 +14,8 @@ public class LocaleService(
)
{
// we have to LazyLoad the data from the database and then combine it with the custom data before returning it
protected LocaleConfig _localeConfig = _configServer.GetConfig<LocaleConfig>();
protected Dictionary<string, Dictionary<string, string>> customClientLocales = new();
protected readonly LocaleConfig _localeConfig = _configServer.GetConfig<LocaleConfig>();
protected readonly Dictionary<string, Dictionary<string, string>> customClientLocales = new();
/// <summary>
/// Get the eft globals db file based on the configured locale in config/locale.json, if not found, fall back to 'en'
@@ -24,11 +24,12 @@ public class LocaleService(
/// <returns> Dictionary of locales for desired language - en/fr/cn </returns>
public Dictionary<string, string> GetLocaleDb(string? language = null)
{
var languageToUse = string.IsNullOrEmpty(language) ? GetDesiredGameLocale() : language;
Dictionary<string, string>? localeToReturn;
var languageToUse = string.IsNullOrEmpty(language)
? GetDesiredGameLocale()
: language;
// if it can't get locales for language provided, default to en
if (TryGetLocaleDbWithCustomLocales(languageToUse, out localeToReturn) ||
if (TryGetLocaleDbWithCustomLocales(languageToUse, out var localeToReturn) ||
TryGetLocaleDbWithCustomLocales("en", out localeToReturn))
{
// TODO: need to see if this needs to be cloned
@@ -44,7 +45,7 @@ public class LocaleService(
/// <param name="languageKey">The language key for which the locale database should be retrieved.</param>
/// <param name="localeToReturn">The resulting locale database as a dictionary, or null if the operation fails.</param>
/// <returns>True if the locale database was successfully retrieved, otherwise false.</returns>
private bool TryGetLocaleDbWithCustomLocales(string languageKey, out Dictionary<string, string>? localeToReturn)
protected bool TryGetLocaleDbWithCustomLocales(string languageKey, out Dictionary<string, string>? localeToReturn)
{
localeToReturn = null;
if (!_databaseServer.GetTables().Locales.Global.TryGetValue(languageKey, out var keyedLocales))
@@ -71,7 +72,7 @@ public class LocaleService(
/// <param name="dbLocales">The dictionary containing locale entries from the database.</param>
/// <param name="customLocales">The dictionary containing custom locale entries to be merged.</param>
/// <returns>A dictionary representing the merged result of database and custom locales.</returns>
private Dictionary<string, string> CombineDbWithCustomLocales(Dictionary<string, string> dbLocales, Dictionary<string, string> customLocales)
protected Dictionary<string, string> CombineDbWithCustomLocales(Dictionary<string, string> dbLocales, Dictionary<string, string> customLocales)
{
try
{
@@ -92,17 +93,14 @@ public class LocaleService(
/// <summary>
/// Gets the game locale key from the locale.json file,
/// if value is 'system' get system locale
/// if value is 'system' get system-configured locale
/// </summary>
/// <returns> Locale e.g en/ge/cz/cn </returns>
public string GetDesiredGameLocale()
{
if (string.Equals(_localeConfig.GameLocale, "system", StringComparison.OrdinalIgnoreCase))
{
return GetPlatformForClientLocale();
}
return _localeConfig.GameLocale.ToLower();
return string.Equals(_localeConfig.GameLocale, "system", StringComparison.OrdinalIgnoreCase)
? GetPlatformForClientLocale()
: _localeConfig.GameLocale.ToLower(); // Use custom locale value
}
/// <summary>
@@ -112,12 +110,9 @@ public class LocaleService(
/// <returns> Locale e.g en/ge/cz/cn </returns>
public string GetDesiredServerLocale()
{
if (string.Equals(_localeConfig.ServerLocale, "system", StringComparison.OrdinalIgnoreCase))
{
return GetPlatformForServerLocale();
}
return _localeConfig.ServerLocale.ToLower();
return string.Equals(_localeConfig.ServerLocale, "system", StringComparison.OrdinalIgnoreCase)
? GetPlatformForServerLocale()
: _localeConfig.ServerLocale.ToLower(); // Use custom locale value
}
/// <summary>
@@ -152,33 +147,34 @@ public class LocaleService(
}
var baseNameCode = platformLocale.TwoLetterISOLanguageName.ToLower();
if (!_localeConfig.ServerSupportedLocales.Contains(baseNameCode))
if (_localeConfig.ServerSupportedLocales.Contains(baseNameCode))
{
// Check if base language (e.g. CN / EN / DE) exists
var languageCode = platformLocale.Name.ToLower();
if (_localeConfig.ServerSupportedLocales.Contains(languageCode))
{
if (baseNameCode == "zh")
// Handle edge case of zh
{
return "zh-cn";
}
return languageCode;
}
if (baseNameCode == "pt")
// Handle edge case of pt
{
return "pt-pt";
}
_logger.Warning($"Unsupported system language found: {baseNameCode}, falling back to english");
return "en";
// Found a matching locale
return baseNameCode;
}
return baseNameCode;
// Check if base language (e.g. CN / EN / DE) exists
var languageCode = platformLocale.Name.ToLower();
if (_localeConfig.ServerSupportedLocales.Contains(languageCode))
{
if (baseNameCode == "zh")
// Handle edge case of zh
{
return "zh-cn";
}
return languageCode;
}
if (baseNameCode == "pt")
// Handle edge case of pt
{
return "pt-pt";
}
_logger.Warning($"Unsupported system language found: {baseNameCode}, langCode: {languageCode} falling back to english for server locale");
return "en";
}
/// <summary>
@@ -219,15 +215,21 @@ public class LocaleService(
return "ge";
}
_logger.Warning($"Unsupported system language found: {languageCode}, falling back to english");
if (baseNameCode == "zh")
// Handle edge case of zh
{
return "cn";
}
_logger.Warning($"Unsupported system language found: {languageCode} baseLocale: {baseNameCode}, falling back to english for client locale");
return "en";
}
/// <summary>
/// This is in a function so we can overwrite it during testing
/// Get the current machines locale data
/// </summary>
/// <returns> The current platform locale </returns>
protected CultureInfo GetPlatformLocale()
protected static CultureInfo GetPlatformLocale()
{
return CultureInfo.InstalledUICulture;
}