-
Notifications
You must be signed in to change notification settings - Fork 12k
/
Copy pathi18n-options.ts
101 lines (87 loc) · 3.06 KB
/
i18n-options.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
/**
* @license
* Copyright Google Inc. All Rights Reserved.
*
* Use of this source code is governed by an MIT-style license that can be
* found in the LICENSE file at https://angular.io/license
*/
import { json } from '@angular-devkit/core';
export interface I18nOptions {
inlineLocales: Set<string>;
sourceLocale: string;
locales: Record<string, { file: string; format?: string; translation?: unknown }>;
flatOutput?: boolean;
readonly shouldInline: boolean;
}
export function createI18nOptions(
metadata: json.JsonObject,
inline?: boolean | string[],
): I18nOptions {
if (
metadata.i18n !== undefined &&
(typeof metadata.i18n !== 'object' || !metadata.i18n || Array.isArray(metadata.i18n))
) {
throw new Error('Project i18n field is malformed. Expected an object.');
}
metadata = metadata.i18n || {};
if (metadata.sourceLocale !== undefined && typeof metadata.sourceLocale !== 'string') {
throw new Error('Project i18n sourceLocale field is malformed. Expected a string.');
}
const i18n: I18nOptions = {
inlineLocales: new Set<string>(),
// en-US is the default locale added to Angular applications (https://angular.io/guide/i18n#i18n-pipes)
sourceLocale: metadata.sourceLocale || 'en-US',
locales: {},
get shouldInline() {
return this.inlineLocales.size > 0;
},
};
if (
metadata.locales !== undefined &&
(!metadata.locales || typeof metadata.locales !== 'object' || Array.isArray(metadata.locales))
) {
throw new Error('Project i18n locales field is malformed. Expected an object.');
} else if (metadata.locales) {
for (const [locale, translationFile] of Object.entries(metadata.locales)) {
if (typeof translationFile !== 'string') {
throw new Error(
`Project i18n locales field value for '${locale}' is malformed. Expected a string.`,
);
}
if (locale === i18n.sourceLocale) {
throw new Error(
`An i18n locale identifier ('${locale}') cannot both be a source locale and provide a translation.`,
);
}
i18n.locales[locale] = {
file: translationFile,
};
}
}
if (inline === true) {
i18n.inlineLocales.add(i18n.sourceLocale);
Object.keys(i18n.locales).forEach(locale => i18n.inlineLocales.add(locale));
} else if (inline) {
for (const locale of inline) {
if (!i18n.locales[locale]) {
throw new Error(`Requested inline locale '${locale}' is not defined for the project.`);
}
i18n.inlineLocales.add(locale);
}
}
return i18n;
}
export function mergeDeprecatedI18nOptions(i18n: I18nOptions, i18nLocale: string | undefined, i18nFile: string | undefined): I18nOptions {
if (i18nFile !== undefined && i18nLocale === undefined) {
throw new Error(`Option 'i18nFile' cannot be used without the 'i18nLocale' option.`);
}
if (i18nLocale !== undefined) {
i18n.inlineLocales.clear();
i18n.inlineLocales.add(i18nLocale);
if (i18nFile !== undefined) {
i18n.locales[i18nLocale] = { file: i18nFile };
i18n.flatOutput = true;
}
}
return i18n;
}