Implemented CustomLocaleService
This commit is contained in:
@@ -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
|
||||
*/
|
||||
/// <summary>
|
||||
/// Process insurance items of a single profile prior to being given back to the player through the mail service
|
||||
/// </summary>
|
||||
/// <param name="sessionId">Player id</param>
|
||||
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
|
||||
*/
|
||||
/// <summary>
|
||||
/// Remove an insurance package from a profile using the package's system data information.
|
||||
/// </summary>
|
||||
/// <param name="sessionId">The session ID of the profile to remove the package from.</param>
|
||||
/// <param name="insPackage">The array index of the insurance package to remove.</param>
|
||||
protected void RemoveInsurancePackageFromProfile(string sessionId, Insurance insPackage)
|
||||
{
|
||||
var profile = _saveServer.GetProfile(sessionId);
|
||||
|
||||
@@ -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<CustomLocaleService> logger,
|
||||
FileUtil fileUtil,
|
||||
DatabaseService databaseService
|
||||
ISptLogger<CustomLocaleService> logger
|
||||
)
|
||||
{
|
||||
protected Dictionary<string, Dictionary<string, string>> customServerLocales = new();
|
||||
protected Dictionary<string, Dictionary<string, string>> customClientLocales = new();
|
||||
|
||||
|
||||
/// <summary>
|
||||
/// 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
|
||||
/// </summary>
|
||||
/// <param name="pathToServerLocales">A path to a folder that contains locales to add to SPT</param>
|
||||
public void AddServerLocales(string pathToServerLocales)
|
||||
/// <param name="locale">en/fr/de</param>
|
||||
/// <param name="localeKey">locale key to store values against</param>
|
||||
/// <param name="localeValue">Localised string to store</param>
|
||||
public void AddServerLocales(string locale, string localeKey, string localeValue)
|
||||
{
|
||||
|
||||
AddToDictionary(locale, localeKey, localeValue, customServerLocales);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
@@ -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
|
||||
/// </summary>
|
||||
/// <param name="pathToGameLocales">A path to a folder that contains locales to add to SPT</param>
|
||||
public void AddGameLocales(string pathToGameLocales)
|
||||
/// <param name="locale">en/fr/de</param>
|
||||
/// <param name="localeKey">locale key to store values against</param>
|
||||
/// <param name="localeValue">Localised string to store</param>
|
||||
public void AddGameLocales(string locale, string localeKey, string localeValue)
|
||||
{
|
||||
AddToDictionary(locale, localeKey, localeValue, customClientLocales);
|
||||
}
|
||||
|
||||
protected void AddToDictionary(string locale, string localeKey, string localeValue,
|
||||
Dictionary<string, Dictionary<string, string>> dictionaryToAddTo)
|
||||
{
|
||||
dictionaryToAddTo.TryAdd(locale, new Dictionary<string, string>());
|
||||
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<string, Dictionary<string, string>> 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
|
||||
}
|
||||
}
|
||||
|
||||
@@ -11,6 +11,7 @@ public class I18nService
|
||||
private readonly Dictionary<string, string> _fallbacks;
|
||||
private readonly FileUtil _fileUtil;
|
||||
private readonly JsonUtil _jsonUtil;
|
||||
private readonly CustomLocaleService _customLocaleService;
|
||||
|
||||
private readonly Dictionary<string, LazyLoad<Dictionary<string, string>>> _loadedLocales = new();
|
||||
private HashSet<string> _locales;
|
||||
@@ -19,6 +20,7 @@ public class I18nService
|
||||
public I18nService(
|
||||
FileUtil fileUtil,
|
||||
JsonUtil jsonUtil,
|
||||
CustomLocaleService customLocaleService,
|
||||
HashSet<string> locales,
|
||||
Dictionary<string, string> 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;
|
||||
}
|
||||
|
||||
|
||||
@@ -19,6 +19,7 @@ public class LocalisationService
|
||||
public LocalisationService(
|
||||
ISptLogger<LocalisationService> 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",
|
||||
|
||||
Reference in New Issue
Block a user