Server/project/src/services/LocaleService.ts
Refringe 4ac12ef70a Formatting/Linting Changes (!168)
These are the formatting & linting configuration changes from the `3.8.0` branch and the changes that they make to the overall project.

The majority of these changes are from running two commands:

`npm run lint:fix`
`npm run style:fix`

This has already been run on the `3.8.0` branch and this PR should make `master` play nicer when it comes to merges going forward.

There are now four VSCode plugins recommended for server development. They've been added to the workspace file and a user should get a UI notification when the workspace is opened if they're not installed.

The four plugins are:
https://marketplace.visualstudio.com/items?itemName=EditorConfig.EditorConfig
https://marketplace.visualstudio.com/items?itemName=dprint.dprint
https://marketplace.visualstudio.com/items?itemName=dbaeumer.vscode-eslint
https://marketplace.visualstudio.com/items?itemName=biomejs.biome

Once installed they should just work within the workspace.

Also, be sure to `npm i` to get the new dprint application.

Co-authored-by: Refringe <brownelltyler@gmail.com>
Reviewed-on: https://dev.sp-tarkov.com/SPT-AKI/Server/pulls/168
2023-11-16 21:42:06 +00:00

109 lines
3.3 KiB
TypeScript

import { inject, injectable } from "tsyringe";
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";
/**
* 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,
@inject("ConfigServer") protected configServer: ConfigServer,
)
{
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;
}
this.logger.warning(
`Unable to find desired locale file using locale ${this.getDesiredGameLocale()} from config/locale.json, falling back to 'en'`,
);
return this.databaseServer.getTables().locales.global.en;
}
/**
* 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))
{
this.logger.warning(
`Unsupported system langauge found ${platformLocale.baseName}, falling back to english`,
);
return "en";
}
return platformLocale.language;
}
}