using SPTarkov.Server.Core.Models.Utils; using SPTarkov.Common.Annotations; namespace SPTarkov.Server.Core.Services; [Injectable(InjectionType.Singleton)] public class CustomLocaleService( 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 ///
/// 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); } /// /// Path should link to a folder containing every locale that should be added to the game 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 ///
/// 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 } }