-
Notifications
You must be signed in to change notification settings - Fork 24.8k
/
translation_loader_spec.ts
239 lines (215 loc) · 11.1 KB
/
translation_loader_spec.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
/**
* @license
* Copyright Google LLC 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 {absoluteFrom, AbsoluteFsPath, FileSystem, getFileSystem} from '@angular/compiler-cli/src/ngtsc/file_system';
import {runInEachFileSystem} from '@angular/compiler-cli/src/ngtsc/file_system/testing';
import {ɵParsedTranslation, ɵparseTranslation} from '@angular/localize';
import {DiagnosticHandlingStrategy, Diagnostics} from '../../../src/diagnostics';
import {TranslationLoader} from '../../../src/translate/translation_files/translation_loader';
import {SimpleJsonTranslationParser} from '../../../src/translate/translation_files/translation_parsers/simple_json_translation_parser';
import {ParseAnalysis, TranslationParser} from '../../../src/translate/translation_files/translation_parsers/translation_parser';
runInEachFileSystem(() => {
describe('TranslationLoader', () => {
describe('loadBundles()', () => {
const alwaysCanParse = () => true;
const neverCanParse = () => false;
let fs: FileSystem;
let enTranslationPath: AbsoluteFsPath;
const enTranslationContent = '{"locale": "en", "translations": {"a": "A"}}';
let frTranslationPath: AbsoluteFsPath;
const frTranslationContent = '{"locale": "fr", "translations": {"a": "A"}}';
let frExtraTranslationPath: AbsoluteFsPath;
const frExtraTranslationContent = '{"locale": "fr", "translations": {"b": "B"}}';
let jsonParser: SimpleJsonTranslationParser;
beforeEach(() => {
fs = getFileSystem();
enTranslationPath = absoluteFrom('/src/locale/messages.en.json');
frTranslationPath = absoluteFrom('/src/locale/messages.fr.json');
frExtraTranslationPath = absoluteFrom('/src/locale/extra.fr.json');
fs.ensureDir(absoluteFrom('/src/locale'));
fs.writeFile(enTranslationPath, enTranslationContent);
fs.writeFile(frTranslationPath, frTranslationContent);
fs.writeFile(frExtraTranslationPath, frExtraTranslationContent);
jsonParser = new SimpleJsonTranslationParser();
});
it('should call `analyze()` and `parse()` for each file', () => {
const diagnostics = new Diagnostics();
const parser = new MockTranslationParser(alwaysCanParse, 'fr');
const loader = new TranslationLoader(fs, [parser], 'error', diagnostics);
loader.loadBundles([[enTranslationPath], [frTranslationPath]], []);
expect(parser.log).toEqual([
`canParse(${enTranslationPath}, ${enTranslationContent})`,
`parse(${enTranslationPath}, ${enTranslationContent})`,
`canParse(${frTranslationPath}, ${frTranslationContent})`,
`parse(${frTranslationPath}, ${frTranslationContent})`,
]);
});
it('should stop at the first parser that can parse each file', () => {
const diagnostics = new Diagnostics();
const parser1 = new MockTranslationParser(neverCanParse);
const parser2 = new MockTranslationParser(alwaysCanParse, 'fr');
const parser3 = new MockTranslationParser(alwaysCanParse, 'en');
const loader = new TranslationLoader(fs, [parser1, parser2, parser3], 'error', diagnostics);
loader.loadBundles([[enTranslationPath], [frTranslationPath]], []);
expect(parser1.log).toEqual([
`canParse(${enTranslationPath}, ${enTranslationContent})`,
`canParse(${frTranslationPath}, ${frTranslationContent})`,
]);
expect(parser2.log).toEqual([
`canParse(${enTranslationPath}, ${enTranslationContent})`,
`parse(${enTranslationPath}, ${enTranslationContent})`,
`canParse(${frTranslationPath}, ${frTranslationContent})`,
`parse(${frTranslationPath}, ${frTranslationContent})`,
]);
});
it('should return locale and translations parsed from each file', () => {
const translations = {};
const diagnostics = new Diagnostics();
const parser = new MockTranslationParser(alwaysCanParse, 'pl', translations);
const loader = new TranslationLoader(fs, [parser], 'error', diagnostics);
const result = loader.loadBundles([[enTranslationPath], [frTranslationPath]], []);
expect(result).toEqual([
{locale: 'pl', translations, diagnostics: new Diagnostics()},
{locale: 'pl', translations, diagnostics: new Diagnostics()},
]);
});
it('should return the provided locale if there is no parsed locale', () => {
const translations = {};
const diagnostics = new Diagnostics();
const parser = new MockTranslationParser(alwaysCanParse, undefined, translations);
const loader = new TranslationLoader(fs, [parser], 'error', diagnostics);
const result = loader.loadBundles([[enTranslationPath], [frTranslationPath]], ['en', 'fr']);
expect(result).toEqual([
{locale: 'en', translations, diagnostics: new Diagnostics()},
{locale: 'fr', translations, diagnostics: new Diagnostics()},
]);
});
it('should merge multiple translation files, if given, for a each locale', () => {
const diagnostics = new Diagnostics();
const loader = new TranslationLoader(fs, [jsonParser], 'error', diagnostics);
const result = loader.loadBundles([[frTranslationPath, frExtraTranslationPath]], []);
expect(result).toEqual([
{
locale: 'fr',
translations: {'a': ɵparseTranslation('A'), 'b': ɵparseTranslation('B')},
diagnostics: new Diagnostics(),
},
]);
});
const allDiagnosticModes: DiagnosticHandlingStrategy[] = ['ignore', 'warning', 'error'];
allDiagnosticModes.forEach(
mode =>
it(`should ${mode} on duplicate messages when merging multiple translation files`,
() => {
const diagnostics = new Diagnostics();
const loader = new TranslationLoader(fs, [jsonParser], mode, diagnostics);
// Change the fs-extra file to have the same translations as fr.
fs.writeFile(frExtraTranslationPath, frTranslationContent);
const result =
loader.loadBundles([[frTranslationPath, frExtraTranslationPath]], []);
expect(result).toEqual([
{
locale: 'fr',
translations: {'a': ɵparseTranslation('A')},
diagnostics: jasmine.any(Diagnostics),
},
]);
if (mode === 'error' || mode === 'warning') {
expect(diagnostics.messages).toEqual([{
type: mode,
message: `Duplicate translations for message "a" when merging "${
frExtraTranslationPath}".`
}]);
}
}));
it('should warn if the provided locales do not match the parsed locales', () => {
const diagnostics = new Diagnostics();
const loader = new TranslationLoader(fs, [jsonParser], 'error', diagnostics);
loader.loadBundles([[enTranslationPath], [frTranslationPath]], [undefined, 'es']);
expect(diagnostics.messages.length).toEqual(1);
expect(diagnostics.messages)
.toContain(
{
type: 'warning',
message:
`The provided locale "es" does not match the target locale "fr" found in the translation file "${
frTranslationPath}".`,
},
);
});
it('should warn on differing target locales when merging multiple translation files', () => {
const diagnostics = new Diagnostics();
const fr1 = absoluteFrom('/src/locale/messages-1.fr.json');
fs.writeFile(fr1, '{"locale":"fr", "translations": {"a": "A"}}');
const fr2 = absoluteFrom('/src/locale/messages-2.fr.json');
fs.writeFile(fr2, '{"locale":"fr", "translations": {"b": "B"}}');
const de = absoluteFrom('/src/locale/messages.de.json');
fs.writeFile(de, '{"locale":"de", "translations": {"c": "C"}}');
const loader = new TranslationLoader(fs, [jsonParser], 'error', diagnostics);
const result = loader.loadBundles([[fr1, fr2, de]], []);
expect(result).toEqual([
{
locale: 'fr',
translations: {
'a': ɵparseTranslation('A'),
'b': ɵparseTranslation('B'),
'c': ɵparseTranslation('C')
},
diagnostics: jasmine.any(Diagnostics),
},
]);
expect(diagnostics.messages).toEqual([{
type: 'warning',
message:
`When merging multiple translation files, the target locale "de" found in "${de}" ` +
`does not match the target locale "fr" found in earlier files ["${fr1}", "${fr2}"].`
}]);
});
it('should throw an error if there is no provided nor parsed target locale', () => {
const translations = {};
const diagnostics = new Diagnostics();
const parser = new MockTranslationParser(alwaysCanParse, undefined, translations);
const loader = new TranslationLoader(fs, [parser], 'error', diagnostics);
expect(() => loader.loadBundles([[enTranslationPath]], []))
.toThrowError(`The translation file "${
enTranslationPath}" does not contain a target locale and no explicit locale was provided for this file.`);
});
it('should error if none of the parsers can parse the file', () => {
const diagnostics = new Diagnostics();
const parser = new MockTranslationParser(neverCanParse);
const loader = new TranslationLoader(fs, [parser], 'error', diagnostics);
expect(() => loader.loadBundles([[enTranslationPath], [frTranslationPath]], []))
.toThrowError(
`There is no "TranslationParser" that can parse this translation file: ${
enTranslationPath}.\n` +
`MockTranslationParser cannot parse translation file.\n` +
`WARNINGS:\n - This is a mock failure warning.`);
});
});
});
class MockTranslationParser implements TranslationParser {
log: string[] = [];
constructor(
private _canParse: (filePath: string) => boolean, private _locale?: string,
private _translations: Record<string, ɵParsedTranslation> = {}) {}
analyze(filePath: string, fileContents: string): ParseAnalysis<true> {
const diagnostics = new Diagnostics();
diagnostics.warn('This is a mock failure warning.');
this.log.push(`canParse(${filePath}, ${fileContents})`);
return this._canParse(filePath) ? {canParse: true, hint: true, diagnostics} :
{canParse: false, diagnostics};
}
parse(filePath: string, fileContents: string) {
this.log.push(`parse(${filePath}, ${fileContents})`);
return {
locale: this._locale,
translations: this._translations,
diagnostics: new Diagnostics()
};
}
}
});