2023-10-19 17:21:17 +00:00
|
|
|
import path from "node:path";
|
2023-11-13 11:43:37 -05:00
|
|
|
import { I18n } from "i18n";
|
2023-03-03 15:23:46 +00:00
|
|
|
import { inject, injectable } from "tsyringe";
|
2024-05-21 17:59:04 +00:00
|
|
|
import { ILogger } from "@spt/models/spt/utils/ILogger";
|
|
|
|
import { DatabaseServer } from "@spt/servers/DatabaseServer";
|
|
|
|
import { LocaleService } from "@spt/services/LocaleService";
|
|
|
|
import { RandomUtil } from "@spt/utils/RandomUtil";
|
2023-03-03 15:23:46 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Handles translating server text into different langauges
|
|
|
|
*/
|
|
|
|
@injectable()
|
|
|
|
export class LocalisationService
|
|
|
|
{
|
|
|
|
protected i18n: I18n;
|
|
|
|
|
|
|
|
constructor(
|
2024-05-28 14:04:20 +00:00
|
|
|
@inject("PrimaryLogger") protected logger: ILogger,
|
2023-08-01 12:51:40 +01:00
|
|
|
@inject("RandomUtil") protected randomUtil: RandomUtil,
|
2023-04-23 11:26:11 +01:00
|
|
|
@inject("DatabaseServer") protected databaseServer: DatabaseServer,
|
2023-11-13 11:13:25 -05:00
|
|
|
@inject("LocaleService") protected localeService: LocaleService,
|
2023-03-03 15:23:46 +00:00
|
|
|
)
|
|
|
|
{
|
2023-11-13 11:13:25 -05:00
|
|
|
const localeFileDirectory = path.join(
|
|
|
|
process.cwd(),
|
2023-11-13 12:29:16 -05:00
|
|
|
globalThis.G_RELEASE_CONFIGURATION
|
2024-05-21 17:59:04 +00:00
|
|
|
? "SPT_Data/Server/database/locales/server"
|
2023-11-13 12:29:16 -05:00
|
|
|
: "./assets/database/locales/server",
|
2023-11-13 11:13:25 -05:00
|
|
|
);
|
2023-11-13 12:31:52 -05:00
|
|
|
this.i18n = new I18n({
|
|
|
|
locales: this.localeService.getServerSupportedLocales(),
|
2024-01-23 20:49:06 +00:00
|
|
|
fallbacks: this.localeService.getLocaleFallbacks(),
|
2023-11-13 12:31:52 -05:00
|
|
|
defaultLocale: "en",
|
|
|
|
directory: localeFileDirectory,
|
|
|
|
retryInDefaultLocale: true,
|
|
|
|
});
|
2023-03-03 15:23:46 +00:00
|
|
|
|
|
|
|
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
|
|
|
|
*/
|
2024-05-27 16:05:16 +00:00
|
|
|
public getText(key: string, args?: any): string
|
2023-03-03 15:23:46 +00:00
|
|
|
{
|
2024-05-28 14:04:20 +00:00
|
|
|
return args === undefined ? this.i18n.__(key.toLowerCase()) : this.i18n.__(key.toLowerCase(), args);
|
2023-03-03 15:23:46 +00:00
|
|
|
}
|
2023-03-08 13:26:32 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Get all locale keys
|
|
|
|
* @returns string array of keys
|
|
|
|
*/
|
|
|
|
public getKeys(): string[]
|
|
|
|
{
|
2024-05-27 16:05:16 +00:00
|
|
|
return Object.keys(this.databaseServer.getTables().locales!.server.en);
|
2023-03-08 13:26:32 +00:00
|
|
|
}
|
2023-08-01 12:51:40 +01:00
|
|
|
|
|
|
|
/**
|
|
|
|
* 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
|
|
|
|
{
|
2024-05-27 16:05:16 +00:00
|
|
|
const filteredKeys = Object.keys(this.databaseServer.getTables().locales!.server.en).filter((x) =>
|
2024-05-07 23:57:08 -04:00
|
|
|
x.startsWith(partialKey),
|
2023-11-13 11:13:25 -05:00
|
|
|
);
|
2023-08-01 12:51:40 +01:00
|
|
|
const chosenKey = this.randomUtil.getArrayValue(filteredKeys);
|
|
|
|
|
|
|
|
return this.getText(chosenKey);
|
|
|
|
}
|
2023-11-13 11:13:25 -05:00
|
|
|
}
|