-
Notifications
You must be signed in to change notification settings - Fork 103
/
i18n.service.ts
361 lines (312 loc) · 10.2 KB
/
i18n.service.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
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
import { Inject, Injectable, Logger, OnModuleDestroy } from '@nestjs/common';
import {
I18N_OPTIONS,
I18N_TRANSLATIONS,
I18N_LANGUAGES,
I18N_LANGUAGES_SUBJECT,
I18N_TRANSLATIONS_SUBJECT,
} from '../i18n.constants';
import { I18nOptions, I18nValidationError } from '..';
import { I18nTranslation } from '../interfaces/i18n-translation.interface';
import { Observable, BehaviorSubject, lastValueFrom, Subject } from 'rxjs';
import { I18nLoader } from '../loaders/i18n.loader';
import { take, takeUntil } from 'rxjs/operators';
import { I18nPluralObject } from 'src/interfaces/i18n-plural.interface';
import { validate } from 'class-validator';
import { formatI18nErrors } from '../utils/util';
import { IfAnyOrNever, Path, PathValue } from '../types';
import { I18nTranslator } from '../interfaces/i18n-translator.interface';
const pluralKeys = ['zero', 'one', 'two', 'few', 'many', 'other'];
export type TranslateOptions = {
lang?: string;
args?: ({ [k: string]: any } | string)[] | { [k: string]: any };
defaultValue?: string;
debug?: boolean;
};
@Injectable()
export class I18nService<K = Record<string, unknown>>
implements I18nTranslator<K>, OnModuleDestroy {
private supportedLanguages: string[];
private translations: I18nTranslation;
private pluralRules = new Map<string, Intl.PluralRules>();
private unsubscribe = new Subject<void>();
constructor(
@Inject(I18N_OPTIONS)
protected readonly i18nOptions: I18nOptions,
@Inject(I18N_TRANSLATIONS)
translations: Observable<I18nTranslation>,
@Inject(I18N_LANGUAGES)
supportedLanguages: Observable<string[]>,
private readonly logger: Logger,
private readonly loader: I18nLoader,
@Inject(I18N_LANGUAGES_SUBJECT)
private readonly languagesSubject: BehaviorSubject<string[]>,
@Inject(I18N_TRANSLATIONS_SUBJECT)
private readonly translationsSubject: BehaviorSubject<I18nTranslation>,
) {
supportedLanguages
.pipe(takeUntil(this.unsubscribe))
.subscribe((languages) => {
this.supportedLanguages = languages;
});
translations.pipe(takeUntil(this.unsubscribe)).subscribe((t) => {
this.translations = t;
});
}
public onModuleDestroy(): void {
this.unsubscribe.next(null);
this.unsubscribe.complete();
}
public translate<P extends Path<K> = any, R = PathValue<K, P>>(
key: P,
options?: TranslateOptions,
): IfAnyOrNever<R, string, R> {
options = {
lang: this.i18nOptions.fallbackLanguage,
...options,
};
const { defaultValue } = options;
let { lang } = options;
if (lang === 'debug') {
return key as unknown as IfAnyOrNever<R, string, R>;
}
const previousFallbackLang = lang;
lang =
lang === undefined || lang === null
? this.i18nOptions.fallbackLanguage
: lang;
lang = this.resolveLanguage(lang);
const translationsByLanguage = this.translations[lang];
const translation = this.translateObject(
key as string,
(translationsByLanguage ? translationsByLanguage : key) as string,
lang,
options,
translationsByLanguage ? translationsByLanguage : undefined,
);
if (
translationsByLanguage === undefined ||
translationsByLanguage === null ||
!translation
) {
if (lang !== this.i18nOptions.fallbackLanguage || !!defaultValue) {
if (this.i18nOptions.logging) {
const message = `Translation "${key as string
}" in "${lang}" does not exist.`;
this.logger.error(message);
}
const nextFallbackLanguage = this.getFallbackLanguage(lang);
if (previousFallbackLang !== nextFallbackLanguage) {
return this.translate(key, {
...options,
lang: nextFallbackLanguage,
});
}
}
}
return (translation ?? key) as unknown as IfAnyOrNever<R, string, R>;
}
private getFallbackLanguage(lang: string) {
let regionSepIndex =-1
if(lang.includes("-")){
regionSepIndex = lang.lastIndexOf('-');
}
if (lang.includes("_")) {
regionSepIndex = lang.lastIndexOf('_');
}
return regionSepIndex !== -1
? lang.slice(0, regionSepIndex)
: this.i18nOptions.fallbackLanguage;
}
public t<P extends Path<K> = any, R = PathValue<K, P>>(
key: P,
options?: TranslateOptions,
): IfAnyOrNever<R, string, R> {
return this.translate(key, options);
}
public getSupportedLanguages() {
return this.supportedLanguages;
}
public getTranslations() {
return this.translations;
}
public async refresh(
translations?: I18nTranslation | Observable<I18nTranslation>,
languages?: string[] | Observable<string[]>,
) {
if (!translations) {
translations = await this.loader.load();
}
if (translations instanceof Observable) {
this.translationsSubject.next(
await lastValueFrom(translations.pipe(take(1))),
);
} else {
this.translationsSubject.next(translations);
}
if (!languages) {
languages = await this.loader.languages();
}
if (languages instanceof Observable) {
this.languagesSubject.next(await lastValueFrom(languages.pipe(take(1))));
} else {
this.languagesSubject.next(languages);
}
}
public hbsHelper = <P extends Path<K> = any>(
key: P,
args: any,
options: any,
) => {
if (!options) {
options = args;
}
const lang = options.lookupProperty(options.data.root, 'i18nLang');
return this.t<P>(key, { lang, args });
};
private translateObject(
key: string,
translations: I18nTranslation | string,
lang: string,
options?: TranslateOptions,
rootTranslations?: I18nTranslation | string,
): I18nTranslation | string {
const keys = key.split('.');
const [firstKey] = keys;
const args = options?.args;
if (keys.length > 1 && !translations[key]) {
const newKey = keys.slice(1, keys.length).join('.');
if (translations && translations[firstKey]) {
return this.translateObject(
newKey,
translations[firstKey],
lang,
options,
rootTranslations,
);
}
}
let translation = translations[key] ?? options?.defaultValue;
if (translation && (args || (args instanceof Array && args.length > 0))) {
const pluralObject = this.getPluralObject(translation);
if (pluralObject && args && args['count'] !== undefined) {
const count = Number(args['count']);
if (!this.pluralRules.has(lang)) {
this.pluralRules.set(lang, new Intl.PluralRules(lang));
}
const pluralRules = this.pluralRules.get(lang);
const pluralCategory = pluralRules.select(count);
if (count === 0 && pluralObject['zero']) {
translation = pluralObject['zero'];
} else if (pluralObject[pluralCategory]) {
translation = pluralObject[pluralCategory];
}
} else if (translation instanceof Object) {
const result = Object.keys(translation).reduce((obj, nestedKey) => {
return {
...obj,
[nestedKey]: this.translateObject(
nestedKey,
translation,
lang,
options,
rootTranslations,
),
};
}, {});
if (translation instanceof Array) {
return Object.values(result) as unknown as I18nTranslation;
}
return result;
}
translation = this.i18nOptions.formatter(
translation,
...(args instanceof Array ? args || [] : [args]),
);
const nestedTranslations = this.getNestedTranslations(translation);
if (nestedTranslations && nestedTranslations.length > 0) {
let offset = 0;
for (const nestedTranslation of nestedTranslations) {
const result = rootTranslations
? (this.translateObject(
nestedTranslation.key,
rootTranslations,
lang,
{
...options,
args: { parent: options.args, ...nestedTranslation.args },
},
) as string) ?? ''
: '';
translation =
translation.substring(0, nestedTranslation.index - offset) +
result +
translation.substring(
nestedTranslation.index + nestedTranslation.length - offset,
);
offset = offset + (nestedTranslation.length - result.length);
}
}
}
return translation;
}
public resolveLanguage(lang: string) {
if (this.i18nOptions.fallbacks && !this.supportedLanguages.includes(lang)) {
const sanitizedLang = lang.includes('-')
? lang.substring(0, lang.indexOf('-')).concat('-*')
: lang;
for (const key in this.i18nOptions.fallbacks) {
if (key === lang || key === sanitizedLang) {
lang = this.i18nOptions.fallbacks[key];
break;
}
}
}
return lang;
}
private getPluralObject(translation: any): I18nPluralObject | undefined {
for (const k of pluralKeys) {
if (translation[k]) {
return translation as I18nPluralObject;
}
}
return undefined;
}
private getNestedTranslations(
translation: string,
): { index: number; length: number; key: string; args: any }[] | undefined {
const list = [];
const regex = /\$t\((.*?)(,(.*?))?\)/g;
let result: RegExpExecArray;
while ((result = regex.exec(translation))) {
let key = undefined;
let args = {};
let index = undefined;
let length = undefined;
if (result && result.length > 0) {
key = result[1].trim();
index = result.index;
length = result[0].length;
if (result.length >= 3 && result[3]) {
try {
args = JSON.parse(result[3]);
} catch (e) {
this.logger.error(`Error while parsing JSON`, e);
}
}
}
if (key) {
list.push({ index, length, key, args });
}
result = undefined;
}
return list.length > 0 ? list : undefined;
}
public async validate(
value: any,
options?: TranslateOptions,
): Promise<I18nValidationError[]> {
const errors = await validate(value, this.i18nOptions.validatorOptions);
return formatI18nErrors(errors, this, options);
}
}