using SPTarkov.DI.Annotations;
using SPTarkov.Server.Core.Models.Utils;
using SPTarkov.Server.Core.Servers;
using SPTarkov.Server.Core.Utils;
namespace SPTarkov.Server.Core.Services;
///
/// Handles translating server text into different langauges
///
[Injectable(InjectionType.Singleton)]
public class LocalisationService
{
protected DatabaseServer _databaseServer;
protected I18nService _i18nService;
protected LocaleService _localeService;
protected ISptLogger _logger;
protected RandomUtil _randomUtil;
// TODO: turn into primary ctor
public LocalisationService(
ISptLogger logger,
RandomUtil randomUtil,
DatabaseServer databaseServer,
LocaleService localeService,
JsonUtil jsonUtil,
FileUtil fileUtil
)
{
_logger = logger;
_randomUtil = randomUtil;
_databaseServer = databaseServer;
_localeService = localeService;
_i18nService = new I18nService(
fileUtil,
jsonUtil,
localeService.GetServerSupportedLocales().ToHashSet(),
localeService.GetLocaleFallbacks(),
"en",
"./Assets/database/locales/server",
localeService
);
_i18nService.SetLocaleByKey(localeService.GetDesiredServerLocale());
}
///
/// Get a localised value using the passed in key
///
/// Key to look up locale for
/// optional arguments
/// Localised string
public string GetText(string key, object? args = null)
{
return args is null
? _i18nService.GetLocalisedValue(key)
: _i18nService.GetLocalised(key, args);
}
///
/// Get a localised value using the passed in key
///
/// Key to look up locale for
/// Value to localize
/// Localised string
public string GetText(string key, T value) where T : IConvertible?
{
return _i18nService.GetLocalised(key, value);
}
///
/// Get all locale keys
///
/// Generic collection of keys
public ICollection GetKeys()
{
return _i18nService.GetLocalisedKeys();
}
///
/// From the provided partial key, find all keys that start with text and choose a random match
///
/// Key to match locale keys on
/// Locale text
public string GetRandomTextThatMatchesPartialKey(string partialKey)
{
var matchingKeys = GetKeys().Where(x => x.Contains(partialKey)).ToList();
if (!matchingKeys.Any())
{
_logger.Warning($"No locale keys found for: {partialKey}");
return string.Empty;
}
return GetText(_randomUtil.GetArrayValue(matchingKeys));
}
}