2023-03-03 16:23:46 +01:00
|
|
|
import { inject, injectable } from "tsyringe";
|
|
|
|
|
2023-10-19 19:21:17 +02:00
|
|
|
import { ConfigTypes } from "@spt-aki/models/enums/ConfigTypes";
|
|
|
|
import { ILocaleConfig } from "@spt-aki/models/spt/config/ILocaleConfig";
|
|
|
|
import { ILogger } from "@spt-aki/models/spt/utils/ILogger";
|
|
|
|
import { ConfigServer } from "@spt-aki/servers/ConfigServer";
|
|
|
|
import { DatabaseServer } from "@spt-aki/servers/DatabaseServer";
|
2023-03-03 16:23:46 +01:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Handles getting locales from config or users machine
|
|
|
|
*/
|
|
|
|
@injectable()
|
|
|
|
export class LocaleService
|
|
|
|
{
|
|
|
|
protected localeConfig: ILocaleConfig;
|
|
|
|
|
|
|
|
constructor(
|
|
|
|
@inject("WinstonLogger") protected logger: ILogger,
|
|
|
|
@inject("DatabaseServer") protected databaseServer: DatabaseServer,
|
2023-11-16 22:42:06 +01:00
|
|
|
@inject("ConfigServer") protected configServer: ConfigServer,
|
2023-03-03 16:23:46 +01:00
|
|
|
)
|
|
|
|
{
|
|
|
|
this.localeConfig = this.configServer.getConfig(ConfigTypes.LOCALE);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Get the eft globals db file based on the configured locale in config/locale.json, if not found, fall back to 'en'
|
|
|
|
* @returns dictionary
|
|
|
|
*/
|
|
|
|
public getLocaleDb(): Record<string, string>
|
|
|
|
{
|
|
|
|
const desiredLocale = this.databaseServer.getTables().locales.global[this.getDesiredGameLocale()];
|
|
|
|
if (desiredLocale)
|
|
|
|
{
|
|
|
|
return desiredLocale;
|
|
|
|
}
|
|
|
|
|
2023-11-16 22:42:06 +01:00
|
|
|
this.logger.warning(
|
|
|
|
`Unable to find desired locale file using locale ${this.getDesiredGameLocale()} from config/locale.json, falling back to 'en'`,
|
|
|
|
);
|
2023-03-03 16:23:46 +01:00
|
|
|
|
2023-11-16 22:42:06 +01:00
|
|
|
return this.databaseServer.getTables().locales.global.en;
|
2023-03-03 16:23:46 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Gets the game locale key from the locale.json file,
|
|
|
|
* if value is 'system' get system locale
|
|
|
|
* @returns locale e.g en/ge/cz/cn
|
|
|
|
*/
|
|
|
|
public getDesiredGameLocale(): string
|
|
|
|
{
|
|
|
|
if (this.localeConfig.gameLocale.toLowerCase() === "system")
|
|
|
|
{
|
|
|
|
return this.getPlatformLocale();
|
|
|
|
}
|
|
|
|
|
|
|
|
return this.localeConfig.gameLocale.toLowerCase();
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Gets the game locale key from the locale.json file,
|
|
|
|
* if value is 'system' get system locale
|
|
|
|
* @returns locale e.g en/ge/cz/cn
|
|
|
|
*/
|
|
|
|
public getDesiredServerLocale(): string
|
|
|
|
{
|
|
|
|
if (this.localeConfig.serverLocale.toLowerCase() === "system")
|
|
|
|
{
|
|
|
|
return this.getPlatformLocale();
|
|
|
|
}
|
|
|
|
|
|
|
|
return this.localeConfig.serverLocale.toLowerCase();
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Get array of languages supported for localisation
|
|
|
|
* @returns array of locales e.g. en/fr/cn
|
|
|
|
*/
|
|
|
|
public getServerSupportedLocales(): string[]
|
|
|
|
{
|
|
|
|
return this.localeConfig.serverSupportedLocales;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Get the locale of the computer running the server
|
|
|
|
* @returns langage part of locale e.g. 'en' part of 'en-US'
|
|
|
|
*/
|
|
|
|
protected getPlatformLocale(): string
|
|
|
|
{
|
|
|
|
const platformLocale = new Intl.Locale(Intl.DateTimeFormat().resolvedOptions().locale);
|
|
|
|
|
|
|
|
if (!platformLocale)
|
|
|
|
{
|
|
|
|
this.logger.warning("System langauge could not be found, falling back to english");
|
|
|
|
return "en";
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!this.localeConfig.serverSupportedLocales.includes(platformLocale.language))
|
|
|
|
{
|
2023-11-16 22:42:06 +01:00
|
|
|
this.logger.warning(
|
|
|
|
`Unsupported system langauge found ${platformLocale.baseName}, falling back to english`,
|
|
|
|
);
|
2023-03-03 16:23:46 +01:00
|
|
|
return "en";
|
|
|
|
}
|
|
|
|
|
|
|
|
return platformLocale.language;
|
|
|
|
}
|
2023-11-16 22:42:06 +01:00
|
|
|
}
|