diff --git a/Libraries/Core/Controllers/InsuranceController.cs b/Libraries/Core/Controllers/InsuranceController.cs index d0712638..9439f9ac 100644 --- a/Libraries/Core/Controllers/InsuranceController.cs +++ b/Libraries/Core/Controllers/InsuranceController.cs @@ -59,11 +59,10 @@ public class InsuranceController( } } - /** - * Process insurance items of a single profile prior to being given back to the player through the mail service. - * - * @returns void - */ + /// + /// Process insurance items of a single profile prior to being given back to the player through the mail service + /// + /// Player id public void ProcessReturnByProfile(string sessionId) { // Filter out items that don't need to be processed yet. @@ -157,13 +156,11 @@ public class InsuranceController( return insuranceDetails.Select(ins => ins.Items.Count).Count(); } - /** - * Remove an insurance package from a profile using the package's system data information. - * - * @param sessionID The session ID of the profile to remove the package from. - * @param index The array index of the insurance package to remove. - * @returns void - */ + /// + /// Remove an insurance package from a profile using the package's system data information. + /// + /// The session ID of the profile to remove the package from. + /// The array index of the insurance package to remove. protected void RemoveInsurancePackageFromProfile(string sessionId, Insurance insPackage) { var profile = _saveServer.GetProfile(sessionId); diff --git a/Libraries/Core/Services/CustomLocaleService.cs b/Libraries/Core/Services/CustomLocaleService.cs index 832fdbb5..2df4f1b4 100644 --- a/Libraries/Core/Services/CustomLocaleService.cs +++ b/Libraries/Core/Services/CustomLocaleService.cs @@ -1,26 +1,28 @@ using Core.Models.Utils; -using Core.Utils; using SptCommon.Annotations; namespace Core.Services; -[Injectable] +[Injectable(InjectionType.Singleton)] public class CustomLocaleService( - ISptLogger logger, - FileUtil fileUtil, - DatabaseService databaseService + ISptLogger logger ) { + protected Dictionary> customServerLocales = new(); + protected Dictionary> customClientLocales = new(); + /// /// Path should link to a folder containing every locale that should be added to the server locales /// e.g. en.json for english, fr.json for french /// Inside each JSON should be a Dictionary of the locale key and localised text /// - /// A path to a folder that contains locales to add to SPT - public void AddServerLocales(string pathToServerLocales) + /// en/fr/de + /// locale key to store values against + /// Localised string to store + public void AddServerLocales(string locale, string localeKey, string localeValue) { - + AddToDictionary(locale, localeKey, localeValue, customServerLocales); } /// @@ -28,9 +30,46 @@ public class CustomLocaleService( /// e.g. en.json for english, fr.json for french /// Inside each JSON should be a Dictionary of the locale key and localised text /// - /// A path to a folder that contains locales to add to SPT - public void AddGameLocales(string pathToGameLocales) + /// en/fr/de + /// locale key to store values against + /// Localised string to store + public void AddGameLocales(string locale, string localeKey, string localeValue) { + AddToDictionary(locale, localeKey, localeValue, customClientLocales); + } + protected void AddToDictionary(string locale, string localeKey, string localeValue, + Dictionary> dictionaryToAddTo) + { + dictionaryToAddTo.TryAdd(locale, new Dictionary()); + if (!dictionaryToAddTo.TryGetValue(locale, out var localeDictToAddTo)) + { + logger.Error($"Unable to get custom locale dictionary keyed by: {locale}"); + + return; + } + + if (!localeDictToAddTo.TryAdd(localeKey, localeValue)) + { + logger.Error($"Unable to add: {localeKey} {localeValue} to custom locale dictionary: {locale}"); + } + } + + public string? GetServerValue(string locale, string localeKey) + { + return GetValueFromDictionary(locale, localeKey, customServerLocales); + } + + public string? GetClientValue(string locale, string localeKey) + { + return GetValueFromDictionary(locale, localeKey, customClientLocales); + } + + protected string? GetValueFromDictionary(string locale, string localeKey, + Dictionary> dictionaryToGetFrom) + { + return dictionaryToGetFrom.TryGetValue(locale, out var localeDictToGetFrom) + ? localeDictToGetFrom.GetValueOrDefault(localeKey) // Locale exists, look up value or return null + : null; // No locale (e.g. en/fr/de) at all } } diff --git a/Libraries/Core/Services/I18nService.cs b/Libraries/Core/Services/I18nService.cs index aa284964..751f9ded 100644 --- a/Libraries/Core/Services/I18nService.cs +++ b/Libraries/Core/Services/I18nService.cs @@ -11,6 +11,7 @@ public class I18nService private readonly Dictionary _fallbacks; private readonly FileUtil _fileUtil; private readonly JsonUtil _jsonUtil; + private readonly CustomLocaleService _customLocaleService; private readonly Dictionary>> _loadedLocales = new(); private HashSet _locales; @@ -19,6 +20,7 @@ public class I18nService public I18nService( FileUtil fileUtil, JsonUtil jsonUtil, + CustomLocaleService customLocaleService, HashSet locales, Dictionary fallbacks, string defaultLocale, @@ -30,6 +32,7 @@ public class I18nService _defaultLocale = defaultLocale; _directory = directory; _jsonUtil = jsonUtil; + _customLocaleService = customLocaleService; _fileUtil = fileUtil; Initialize(); @@ -96,7 +99,11 @@ public class I18nService if (!locales.Value.TryGetValue(key, out var value)) { _loadedLocales.TryGetValue(_defaultLocale, out var defaults); - defaults.Value.TryGetValue(key, out value); + if (!defaults.Value.TryGetValue(key, out value)) + { + value = _customLocaleService.GetClientValue(_defaultLocale, key); + } + return value ?? key; } diff --git a/Libraries/Core/Services/LocalisationService.cs b/Libraries/Core/Services/LocalisationService.cs index 811e8b29..4003b617 100644 --- a/Libraries/Core/Services/LocalisationService.cs +++ b/Libraries/Core/Services/LocalisationService.cs @@ -19,6 +19,7 @@ public class LocalisationService public LocalisationService( ISptLogger logger, RandomUtil randomUtil, + CustomLocaleService customLocaleService, DatabaseServer databaseServer, LocaleService localeService, JsonUtil jsonUtil, @@ -32,6 +33,7 @@ public class LocalisationService _i18nService = new I18nService( fileUtil, jsonUtil, + customLocaleService, localeService.GetServerSupportedLocales().ToHashSet(), localeService.GetLocaleFallbacks(), "en",