From ad0fee1dbf7bcc93fa35087f21ded29d744be4ee Mon Sep 17 00:00:00 2001 From: Chomp Date: Fri, 13 Jun 2025 09:41:06 +0100 Subject: [PATCH] Improved LocaleService Better logging when desired locale not found Made private methods protected Made some methods static Added edge-case handling of ZH locale --- .../Services/LocaleService.cs | 96 ++++++++++--------- 1 file changed, 49 insertions(+), 47 deletions(-) diff --git a/Libraries/SPTarkov.Server.Core/Services/LocaleService.cs b/Libraries/SPTarkov.Server.Core/Services/LocaleService.cs index 4b66cc9f..77e0a892 100644 --- a/Libraries/SPTarkov.Server.Core/Services/LocaleService.cs +++ b/Libraries/SPTarkov.Server.Core/Services/LocaleService.cs @@ -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(); - protected Dictionary> customClientLocales = new(); + protected readonly LocaleConfig _localeConfig = _configServer.GetConfig(); + protected readonly Dictionary> customClientLocales = new(); /// /// 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( /// Dictionary of locales for desired language - en/fr/cn public Dictionary GetLocaleDb(string? language = null) { - var languageToUse = string.IsNullOrEmpty(language) ? GetDesiredGameLocale() : language; - Dictionary? 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( /// The language key for which the locale database should be retrieved. /// The resulting locale database as a dictionary, or null if the operation fails. /// True if the locale database was successfully retrieved, otherwise false. - private bool TryGetLocaleDbWithCustomLocales(string languageKey, out Dictionary? localeToReturn) + protected bool TryGetLocaleDbWithCustomLocales(string languageKey, out Dictionary? localeToReturn) { localeToReturn = null; if (!_databaseServer.GetTables().Locales.Global.TryGetValue(languageKey, out var keyedLocales)) @@ -71,7 +72,7 @@ public class LocaleService( /// The dictionary containing locale entries from the database. /// The dictionary containing custom locale entries to be merged. /// A dictionary representing the merged result of database and custom locales. - private Dictionary CombineDbWithCustomLocales(Dictionary dbLocales, Dictionary customLocales) + protected Dictionary CombineDbWithCustomLocales(Dictionary dbLocales, Dictionary customLocales) { try { @@ -92,17 +93,14 @@ public class LocaleService( /// /// 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 /// /// Locale e.g en/ge/cz/cn 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 } /// @@ -112,12 +110,9 @@ public class LocaleService( /// Locale e.g en/ge/cz/cn 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 } /// @@ -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"; } /// @@ -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"; } /// - /// This is in a function so we can overwrite it during testing + /// Get the current machines locale data /// /// The current platform locale - protected CultureInfo GetPlatformLocale() + protected static CultureInfo GetPlatformLocale() { return CultureInfo.InstalledUICulture; }