forked from heusalagroup/fi.hg.backend
-
Notifications
You must be signed in to change notification settings - Fork 0
/
BackendTranslationServiceImpl.ts
85 lines (71 loc) · 3.11 KB
/
BackendTranslationServiceImpl.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
// Copyright (c) 2021-2023. Heusala Group Oy <[email protected]>. All rights reserved.
import { init as i18nInit, changeLanguage, Resource } from "i18next";
import { Language } from "../core/types/Language";
import { ReadonlyJsonObject } from "../core/Json";
import { LogService } from "../core/LogService";
import { TranslationResourceObject } from "../core/types/TranslationResourceObject";
import { TranslatedObject } from "../core/types/TranslatedObject";
import { TranslationUtils } from "../core/TranslationUtils";
import { LogLevel } from "../core/types/LogLevel";
import { TranslationFunction } from "../core/types/TranslationFunction";
import { TranslationService } from "../core/i18n/TranslationService";
const LOG = LogService.createLogger('BackendTranslationService');
export class BackendTranslationServiceImpl implements TranslationService {
protected constructor () {
}
public static create () : TranslationService {
return BackendTranslationServiceImpl;
}
public static setLogLevel (level: LogLevel) {
LOG.setLogLevel(level);
}
public static async initialize (
defaultLanguage : Language,
resources : TranslationResourceObject
) : Promise<void> {
const languageResources : Resource = TranslationUtils.getConfig(resources);
return await new Promise((resolve, reject) => {
i18nInit(
{
resources: languageResources,
lng: defaultLanguage,
interpolation: {
escapeValue: true
}
}
).then(() => {
resolve();
}).catch(err => {
LOG.error(`Error in init: `, err);
reject(err);
});
});
}
public static async translateKeys (
lang : Language,
keys : string[],
translationParams : ReadonlyJsonObject
): Promise<TranslatedObject> {
const t = await changeLanguage(lang);
return TranslationUtils.translateKeys(t as TranslationFunction, keys, translationParams);
}
public static async translateJob<T> (
lang : Language,
callback : ((t: TranslationFunction) => T)
) : Promise<T> {
const t = await changeLanguage(lang);
return callback(t as TranslationFunction);
}
public async initialize (defaultLanguage: Language, resources: TranslationResourceObject): Promise<void> {
return await BackendTranslationServiceImpl.initialize(defaultLanguage, resources);
}
public setLogLevel (level: LogLevel): void {
BackendTranslationServiceImpl.setLogLevel(level);
}
public async translateJob<T> (lang: Language, callback: (t: TranslationFunction) => T): Promise<T> {
return await BackendTranslationServiceImpl.translateJob<T>(lang, callback);
}
public async translateKeys (lang: Language, keys: string[], translationParams: ReadonlyJsonObject): Promise<TranslatedObject> {
return await BackendTranslationServiceImpl.translateKeys(lang, keys, translationParams);
}
}