/
source_file_translation_handler_spec.ts
130 lines (116 loc) 路 5.6 KB
/
source_file_translation_handler_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
/**
* @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 {Diagnostics} from '../../../src/diagnostics';
import {FileUtils} from '../../../src/file_utils';
import {SourceFileTranslationHandler} from '../../../src/translate/source_files/source_file_translation_handler';
import {TranslationBundle} from '../../../src/translate/translator';
describe('SourceFileTranslationHandler', () => {
describe('canTranslate()', () => {
it('should return true if the path ends in ".js"', () => {
const handler = new SourceFileTranslationHandler();
expect(handler.canTranslate('relative/path', Buffer.from('contents'))).toBe(false);
expect(handler.canTranslate('relative/path.js', Buffer.from('contents'))).toBe(true);
});
});
describe('translate()', () => {
beforeEach(() => { spyOn(FileUtils, 'writeFile'); });
it('should copy files for each translation locale if they contain no reference to `$localize`',
() => {
const diagnostics = new Diagnostics();
const handler = new SourceFileTranslationHandler();
const translations = [
{locale: 'en', translations: {}},
{locale: 'fr', translations: {}},
];
const contents = Buffer.from('contents');
handler.translate(
diagnostics, '/root/path', 'relative/path', contents, mockOutputPathFn, translations);
expect(FileUtils.writeFile)
.toHaveBeenCalledWith('/translations/en/relative/path', contents);
expect(FileUtils.writeFile)
.toHaveBeenCalledWith('/translations/fr/relative/path', contents);
});
it('should copy files to the source locale if they contain no reference to `$localize` and `sourceLocale` is provided',
() => {
const diagnostics = new Diagnostics();
const handler = new SourceFileTranslationHandler();
const translations: TranslationBundle[] = [];
const contents = Buffer.from('contents');
handler.translate(
diagnostics, '/root/path', 'relative/path', contents, mockOutputPathFn, translations,
'en-US');
expect(FileUtils.writeFile)
.toHaveBeenCalledWith('/translations/en-US/relative/path', contents);
});
it('should transform each $localize template tag', () => {
const diagnostics = new Diagnostics();
const handler = new SourceFileTranslationHandler();
const translations = [
{locale: 'en', translations: {}},
{locale: 'fr', translations: {}},
];
const contents = Buffer.from(
'$localize`a${1}b${2}c`;\n' +
'$localize(__makeTemplateObject(["a", "b", "c"], ["a", "b", "c"]), 1, 2);');
const output = '"a"+1+"b"+2+"c";"a"+1+"b"+2+"c";';
handler.translate(
diagnostics, '/root/path', 'relative/path.js', contents, mockOutputPathFn, translations);
expect(FileUtils.writeFile).toHaveBeenCalledWith('/translations/en/relative/path.js', output);
expect(FileUtils.writeFile).toHaveBeenCalledWith('/translations/fr/relative/path.js', output);
});
it('should transform each $localize template tag and write it to the source locale if provided',
() => {
const diagnostics = new Diagnostics();
const handler = new SourceFileTranslationHandler();
const translations: TranslationBundle[] = [];
const contents = Buffer.from(
'$localize`a${1}b${2}c`;\n' +
'$localize(__makeTemplateObject(["a", "b", "c"], ["a", "b", "c"]), 1, 2);');
const output = '"a"+1+"b"+2+"c";"a"+1+"b"+2+"c";';
handler.translate(
diagnostics, '/root/path', 'relative/path.js', contents, mockOutputPathFn,
translations, 'en-US');
expect(FileUtils.writeFile)
.toHaveBeenCalledWith('/translations/en-US/relative/path.js', output);
});
it('should transform `$localize.locale` identifiers', () => {
const diagnostics = new Diagnostics();
const handler = new SourceFileTranslationHandler();
const translations: TranslationBundle[] = [
{locale: 'fr', translations: {}},
];
const contents = Buffer.from(
'const x = $localize.locale;\n' +
'const y = typeof $localize !== "undefined" && $localize.locale;\n' +
'const z = "undefined" !== typeof $localize && $localize.locale || "default";');
const getOutput = (locale: string) =>
`const x="${locale}";const y="${locale}";const z="${locale}"||"default";`;
handler.translate(
diagnostics, '/root/path', 'relative/path.js', contents, mockOutputPathFn, translations,
'en-US');
expect(FileUtils.writeFile)
.toHaveBeenCalledWith('/translations/fr/relative/path.js', getOutput('fr'));
expect(FileUtils.writeFile)
.toHaveBeenCalledWith('/translations/en-US/relative/path.js', getOutput('en-US'));
});
it('should error if the file is not valid JS', () => {
const diagnostics = new Diagnostics();
const handler = new SourceFileTranslationHandler();
const translations = [{locale: 'en', translations: {}}];
const contents = Buffer.from('this is not a valid $localize file.');
expect(
() => handler.translate(
diagnostics, '/root/path', 'relative/path.js', contents, mockOutputPathFn,
translations))
.toThrowError();
});
});
});
function mockOutputPathFn(locale: string, relativePath: string) {
return `/translations/${locale}/${relativePath}`;
}