2
0
mirror of https://github.com/frappe/books.git synced 2024-11-10 07:40:55 +00:00
books/fyo/utils/translation.ts

Ignoring revisions in .git-blame-ignore-revs. Click here to bypass and see the normal blame view.

136 lines
2.9 KiB
TypeScript
Raw Normal View History

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,
} from '../../utils/translationHelpers';
import { ValueError } from './errors';
2022-04-11 07:00:21 +00:00
type TranslationArgs = boolean | number | string;
type TranslationLiteral = TemplateStringsArray | TranslationArgs;
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[]) {
this.args = args;
}
get s() {
return this.toString();
}
2022-04-08 06:59:42 +00:00
ctx(context?: string) {
this.context = context;
return this;
}
2022-04-11 07:00:21 +00:00
#formatArg(arg: string | number | boolean) {
2022-02-07 08:41:10 +00:00
return arg ?? '';
}
2022-02-16 14:10:08 +00:00
#translate() {
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);
}
#stitch() {
2022-04-08 06:59:42 +00:00
if (!((this.args[0] as any) instanceof Array)) {
throw new ValueError(
`invalid args passed to TranslationString ${
this.args
} of type ${typeof this.args[0]}`
);
}
2022-04-08 06:59:42 +00:00
this.strList = this.args[0] as any 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();
}
2022-04-08 06:59:42 +00:00
return this.strList!.map((s, i) => s + this.#formatArg(this.argList![i]))
.join('')
.replace(/\s+/g, ' ')
.trim();
}
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 {
return new TranslationString(...args);
}
2022-04-11 07:00:21 +00:00
export function t(...args: TranslationLiteral[]): string {
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
);
}
}