2022-05-19 09:05:19 +00:00
|
|
|
import { LanguageMap, UnknownMap } from 'utils/types';
|
2022-02-16 14:10:08 +00:00
|
|
|
import {
|
|
|
|
getIndexFormat,
|
|
|
|
getIndexList,
|
|
|
|
getSnippets,
|
|
|
|
getWhitespaceSanitized,
|
2022-04-17 05:49:18 +00:00
|
|
|
} from '../../utils/translationHelpers';
|
2022-03-21 06:49:19 +00:00
|
|
|
import { ValueError } from './errors';
|
2022-01-20 09:03:41 +00:00
|
|
|
|
2022-10-17 07:07:38 +00:00
|
|
|
export type TranslationArgs = boolean | number | string;
|
|
|
|
export type TranslationLiteral = TemplateStringsArray | TranslationArgs;
|
2022-04-11 07:00:21 +00:00
|
|
|
|
2023-07-14 05:17:52 +00:00
|
|
|
export class TranslationString {
|
2022-04-11 07:00:21 +00:00
|
|
|
args: TranslationLiteral[];
|
|
|
|
argList?: TranslationArgs[];
|
2022-04-08 06:59:42 +00:00
|
|
|
strList?: string[];
|
|
|
|
context?: string;
|
|
|
|
languageMap?: LanguageMap;
|
|
|
|
|
2022-04-11 07:00:21 +00:00
|
|
|
constructor(...args: TranslationLiteral[]) {
|
2022-01-19 08:24:54 +00:00
|
|
|
this.args = args;
|
|
|
|
}
|
|
|
|
|
|
|
|
get s() {
|
|
|
|
return this.toString();
|
|
|
|
}
|
|
|
|
|
2022-04-08 06:59:42 +00:00
|
|
|
ctx(context?: string) {
|
2022-01-19 08:24:54 +00:00
|
|
|
this.context = context;
|
|
|
|
return this;
|
|
|
|
}
|
|
|
|
|
2022-04-11 07:00:21 +00:00
|
|
|
#formatArg(arg: string | number | boolean) {
|
2023-06-22 08:52:54 +00:00
|
|
|
return String(arg ?? '');
|
2022-01-24 09:25:52 +00:00
|
|
|
}
|
|
|
|
|
2022-02-16 14:10:08 +00:00
|
|
|
#translate() {
|
2022-04-17 05:49:18 +00:00
|
|
|
let indexFormat = getIndexFormat(this.args[0] as string);
|
2022-02-16 14:10:08 +00:00
|
|
|
indexFormat = getWhitespaceSanitized(indexFormat);
|
|
|
|
|
|
|
|
const translatedIndexFormat =
|
2022-04-08 06:59:42 +00:00
|
|
|
this.languageMap![indexFormat]?.translation ?? indexFormat;
|
2022-02-16 14:10:08 +00:00
|
|
|
|
|
|
|
this.argList = getIndexList(translatedIndexFormat).map(
|
2022-04-08 06:59:42 +00:00
|
|
|
(i) => this.argList![i]
|
2022-02-16 14:10:08 +00:00
|
|
|
);
|
|
|
|
this.strList = getSnippets(translatedIndexFormat);
|
|
|
|
}
|
|
|
|
|
2022-01-19 08:24:54 +00:00
|
|
|
#stitch() {
|
2023-06-22 08:52:54 +00:00
|
|
|
if (!((this.args[0] as unknown) instanceof Array)) {
|
2022-01-20 09:03:41 +00:00
|
|
|
throw new ValueError(
|
2023-06-22 08:52:54 +00:00
|
|
|
`invalid args passed to TranslationString ${String(
|
2022-01-20 09:03:41 +00:00
|
|
|
this.args
|
2023-06-22 08:52:54 +00:00
|
|
|
)} of type ${typeof this.args[0]}`
|
2022-01-20 09:03:41 +00:00
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2023-06-22 08:52:54 +00:00
|
|
|
this.strList = this.args[0] as unknown as string[];
|
2022-04-11 07:00:21 +00:00
|
|
|
this.argList = this.args.slice(1) as TranslationArgs[];
|
2022-02-16 14:10:08 +00:00
|
|
|
|
|
|
|
if (this.languageMap) {
|
|
|
|
this.#translate();
|
|
|
|
}
|
|
|
|
|
2023-06-22 08:52:54 +00:00
|
|
|
return this.strList
|
|
|
|
.map((s, i) => s + this.#formatArg(this.argList![i]))
|
2022-01-24 09:25:52 +00:00
|
|
|
.join('')
|
2022-02-07 09:01:28 +00:00
|
|
|
.replace(/\s+/g, ' ')
|
2022-01-24 09:25:52 +00:00
|
|
|
.trim();
|
2022-01-19 08:24:54 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
toString() {
|
|
|
|
return this.#stitch();
|
|
|
|
}
|
|
|
|
|
|
|
|
toJSON() {
|
|
|
|
return this.#stitch();
|
|
|
|
}
|
|
|
|
|
|
|
|
valueOf() {
|
|
|
|
return this.#stitch();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-04-11 07:00:21 +00:00
|
|
|
export function T(...args: string[]): TranslationString {
|
2022-01-19 08:24:54 +00:00
|
|
|
return new TranslationString(...args);
|
|
|
|
}
|
2022-01-20 09:03:41 +00:00
|
|
|
|
2022-04-11 07:00:21 +00:00
|
|
|
export function t(...args: TranslationLiteral[]): string {
|
2022-01-20 09:03:41 +00:00
|
|
|
return new TranslationString(...args).s;
|
|
|
|
}
|
2022-02-16 14:10:08 +00:00
|
|
|
|
2022-04-20 06:38:47 +00:00
|
|
|
export function setLanguageMapOnTranslationString(
|
|
|
|
languageMap: LanguageMap | undefined
|
|
|
|
) {
|
2022-02-16 14:10:08 +00:00
|
|
|
TranslationString.prototype.languageMap = languageMap;
|
|
|
|
}
|
2022-05-19 09:05:19 +00:00
|
|
|
|
|
|
|
export function translateSchema(
|
|
|
|
map: UnknownMap | UnknownMap[],
|
|
|
|
languageMap: LanguageMap,
|
|
|
|
translateables: string[]
|
|
|
|
) {
|
|
|
|
if (Array.isArray(map)) {
|
|
|
|
for (const item of map) {
|
|
|
|
translateSchema(item, languageMap, translateables);
|
|
|
|
}
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (typeof map !== 'object') {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
for (const key of Object.keys(map)) {
|
|
|
|
const value = map[key];
|
|
|
|
if (
|
|
|
|
typeof value === 'string' &&
|
|
|
|
translateables.includes(key) &&
|
|
|
|
languageMap[value]?.translation
|
|
|
|
) {
|
|
|
|
map[key] = languageMap[value].translation;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (typeof value !== 'object') {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
translateSchema(
|
|
|
|
value as UnknownMap | UnknownMap[],
|
|
|
|
languageMap,
|
|
|
|
translateables
|
|
|
|
);
|
|
|
|
}
|
|
|
|
}
|