Server/project/src/services/LocalisationService.ts
TheSparta 723f8db572 Updated dependencies + A few other things (!150)
Initially this was going to be an update to dependencies but it seems i got a little carried away!

Anyways this PR removes 2 unused dependencies (`jshint` and `utf-8-validate`), and 2 other, `del` and `fs-extra` that were replaced by the built-in `fs/promises`.

It also renames all `tsconfig` and `Dockerfile` files, in a way that when viewed in a file tree sorted alphabetically they will be next to each other.

It also updates the typescript target to `ES2022`, and changes moduleResolution from `Node` to `Node10` (this isn't an update, they are the same thing but `Node` is now deprecated).

It also adds the `node:` discriminator to every import from built-in modules.

It also has major changes to the build script, `del` and `fs-extra` were only being used in the build script, it's now using `fs/promises` instead, cleaned up the code from some functions, adds better documentation to a few functions, and renames some gulp tasks and npm scripts to better represent what they actually do.

And finally it updates dependencies, except for `atomically` which can't be updated unless the project switches to ESM.

Reviewed-on: https://dev.sp-tarkov.com/SPT-AKI/Server/pulls/150
Co-authored-by: TheSparta <thesparta@noreply.dev.sp-tarkov.com>
Co-committed-by: TheSparta <thesparta@noreply.dev.sp-tarkov.com>
2023-10-14 09:01:41 +00:00

72 lines
2.4 KiB
TypeScript

import { I18n } from "i18n";
import { inject, injectable } from "tsyringe";
import path from "node:path";
import { ILocaleConfig } from "../models/spt/config/ILocaleConfig";
import { ILogger } from "../models/spt/utils/ILogger";
import { DatabaseServer } from "../servers/DatabaseServer";
import { RandomUtil } from "../utils/RandomUtil";
import { LocaleService } from "./LocaleService";
/**
* Handles translating server text into different langauges
*/
@injectable()
export class LocalisationService
{
protected localeConfig: ILocaleConfig;
protected i18n: I18n;
constructor(
@inject("WinstonLogger") protected logger: ILogger,
@inject("RandomUtil") protected randomUtil: RandomUtil,
@inject("DatabaseServer") protected databaseServer: DatabaseServer,
@inject("LocaleService") protected localeService: LocaleService
)
{
const localeFileDirectory = path.join(process.cwd(), globalThis.G_RELEASE_CONFIGURATION ? "Aki_Data/Server/database/locales/server" : "./assets/database/locales/server");
this.i18n = new I18n(
{
locales: this.localeService.getServerSupportedLocales(),
defaultLocale: "en",
directory: localeFileDirectory,
retryInDefaultLocale: true
}
);
this.i18n.setLocale(this.localeService.getDesiredServerLocale());
}
/**
* Get a localised value using the passed in key
* @param key Key to loop up locale for
* @param args optional arguments
* @returns Localised string
*/
public getText(key: string, args = undefined): string
{
return this.i18n.__(key.toLowerCase(), args);
}
/**
* Get all locale keys
* @returns string array of keys
*/
public getKeys(): string[]
{
return Object.keys(this.databaseServer.getTables().locales.server["en"]);
}
/**
* From the provided partial key, find all keys that start with text and choose a random match
* @param partialKey Key to match locale keys on
* @returns locale text
*/
public getRandomTextThatMatchesPartialKey(partialKey: string): string
{
const filteredKeys = Object.keys(this.databaseServer.getTables().locales.server["en"]).filter(x => x.startsWith(partialKey));
const chosenKey = this.randomUtil.getArrayValue(filteredKeys);
return this.getText(chosenKey);
}
}