-
Notifications
You must be signed in to change notification settings - Fork 12k
/
aot-i18n.ts
144 lines (128 loc) · 5.61 KB
/
aot-i18n.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
import * as express from 'express';
import { resolve } from 'path';
import { getGlobalVariable } from '../../../utils/env';
import { appendToFile, copyFile, expectFileToExist, expectFileToMatch, replaceInFile, writeFile } from '../../../utils/fs';
import { ng, npm } from '../../../utils/process';
import { updateJsonFile } from '../../../utils/project';
import { expectToFail } from '../../../utils/utils';
import { readNgVersion } from '../../../utils/version';
export default async function () {
const baseDir = 'dist/test-project';
const enDir = `${baseDir}/en`;
const frDist = `${baseDir}/fr`;
const deDir = `${baseDir}/de`;
// Set configurations for each locale.
const langTranslations = [
{ lang: 'en', translation: 'Hello i18n!', outputPath: enDir },
{ lang: 'fr', translation: 'Bonjour i18n!', outputPath: frDist },
{ lang: 'de', translation: 'Hallo i18n!', outputPath: deDir },
];
if (!getGlobalVariable('argv')['ve']) {
let localizeVersion = '@angular/localize@' + readNgVersion();
if (getGlobalVariable('argv')['ng-snapshots']) {
localizeVersion = require('../../../ng-snapshot/package.json').dependencies['@angular/localize'];
}
await npm('install', localizeVersion);
await appendToFile('src/polyfills.ts', `\nimport '@angular/localize/init';`);
}
await updateJsonFile('angular.json', workspaceJson => {
const appArchitect = workspaceJson.projects['test-project'].architect;
const browserConfigs = appArchitect['build'].configurations;
const serveConfigs = appArchitect['serve'].configurations;
const e2eConfigs = appArchitect['e2e'].configurations;
// Make default builds prod.
appArchitect['build'].options.optimization = true;
appArchitect['build'].options.buildOptimizer = true;
appArchitect['build'].options.aot = true;
appArchitect['build'].options.fileReplacements = [{
replace: 'src/environments/environment.ts',
with: 'src/environments/environment.prod.ts',
}];
for (const { lang, outputPath } of langTranslations) {
if (lang == 'en') {
browserConfigs[lang] = { outputPath };
} else {
browserConfigs[lang] = {
outputPath,
i18nFile: `src/locale/messages.${lang}.xlf`,
i18nFormat: `xlf`,
i18nLocale: lang,
};
}
serveConfigs[lang] = { browserTarget: `test-project:build:${lang}` };
e2eConfigs[lang] = {
specs: [`./src/app.${lang}.e2e-spec.ts`],
devServerTarget: `test-project:serve:${lang}`,
};
}
});
// Add e2e specs for each lang.
for (const { lang, translation } of langTranslations) {
await writeFile(`./src/app.${lang}.e2e-spec.ts`, `
import { browser, logging, element, by } from 'protractor';
describe('workspace-project App', () => {
it('should display welcome message', () => {
browser.get(browser.baseUrl);
expect(element(by.css('h1')).getText()).toEqual('${translation}');
});
afterEach(async () => {
// Assert that there are no errors emitted from the browser
const logs = await browser.manage().logs().get(logging.Type.BROWSER);
expect(logs).not.toContain(jasmine.objectContaining({
level: logging.Level.SEVERE,
} as logging.Entry));
});
});
`);
}
// Add a translatable element.
await writeFile('src/app/app.component.html',
'<h1 i18n="An introduction header for this sample">Hello i18n!</h1>');
// Extract the translation messages and copy them for each language.
await ng('xi18n', '--output-path=src/locale');
await expectFileToExist('src/locale/messages.xlf');
await expectFileToMatch('src/locale/messages.xlf', `source-language="en-US"`);
await expectFileToMatch('src/locale/messages.xlf', `An introduction header for this sample`);
for (const { lang, translation } of langTranslations) {
if (lang != 'en') {
await copyFile('src/locale/messages.xlf', `src/locale/messages.${lang}.xlf`);
await replaceInFile(
`src/locale/messages.${lang}.xlf`,
'source-language="en-US"',
`source-language="en-US" target-language="${lang}"`,
);
await replaceInFile(
`src/locale/messages.${lang}.xlf`,
'<source>Hello i18n!</source>',
`<source>Hello i18n!</source>\n<target>${translation}</target>`,
);
}
}
for (const { lang, translation, outputPath } of langTranslations) {
// Build each locale and verify the output.
await ng('build', `--configuration=${lang}`);
await expectFileToMatch(`${outputPath}/main-es5.js`, translation);
await expectFileToMatch(`${outputPath}/main-es2015.js`, translation);
// E2E to verify the output runs and is correct.
if (getGlobalVariable('argv')['ve']) {
await ng('e2e', `--configuration=${lang}`);
} else {
// Ivy i18n doesn't yet work with `ng serve` so we must use a separate server.
const app = express();
app.use(express.static(resolve(outputPath)));
const server = app.listen(4200, 'localhost');
try {
// Execute without a devserver.
await ng('e2e', `--configuration=${lang}`, '--devServerTarget=');
} finally {
server.close();
}
}
}
// Verify missing translation behaviour.
await appendToFile('src/app/app.component.html', '<p i18n>Other content</p>');
await ng('build', '--configuration=fr', '--i18n-missing-translation', 'ignore');
await expectFileToMatch(`${frDist}/main-es5.js`, /Other content/);
await expectFileToMatch(`${frDist}/main-es2015.js`, /Other content/);
await expectToFail(() => ng('build', '--configuration=fr', '--i18n-missing-translation', 'error'));
}