|
1 |
| -import * as express from 'express'; |
2 |
| -import { resolve } from 'path'; |
3 |
| -import { getGlobalVariable } from '../../utils/env'; |
4 |
| -import { |
5 |
| - appendToFile, |
6 |
| - copyFile, |
7 |
| - expectFileToExist, |
8 |
| - expectFileToMatch, |
9 |
| - replaceInFile, |
10 |
| - writeFile, |
11 |
| -} from '../../utils/fs'; |
12 |
| -import { ng, npm } from '../../utils/process'; |
| 1 | +import { appendToFile, expectFileToMatch } from '../../utils/fs'; |
| 2 | +import { ng } from '../../utils/process'; |
13 | 3 | import { updateJsonFile } from '../../utils/project';
|
14 | 4 | import { expectToFail } from '../../utils/utils';
|
15 |
| -import { readNgVersion } from '../../utils/version'; |
| 5 | +import { baseDir, externalServer, langTranslations, setupI18nConfig } from './legacy'; |
16 | 6 |
|
17 |
| -export default async function() { |
18 |
| - let localizeVersion = '@angular/localize@' + readNgVersion(); |
19 |
| - if (getGlobalVariable('argv')['ng-snapshots']) { |
20 |
| - localizeVersion = require('../../ng-snapshot/package.json').dependencies['@angular/localize']; |
21 |
| - } |
22 |
| - await npm('install', `${localizeVersion}`); |
23 | 7 |
|
| 8 | +export default async function () { |
| 9 | + // Setup i18n tests and config. |
| 10 | + await setupI18nConfig(); |
| 11 | + |
| 12 | + // Ensure a DL build is used. |
24 | 13 | await updateJsonFile('tsconfig.json', config => {
|
25 | 14 | config.compilerOptions.target = 'es2015';
|
26 | 15 | config.angularCompilerOptions.disableTypeScriptVersionCheck = true;
|
27 | 16 | });
|
28 | 17 |
|
29 |
| - const baseDir = 'dist/test-project'; |
30 |
| - |
31 |
| - // Set configurations for each locale. |
32 |
| - const langTranslations = [ |
33 |
| - // TODO: re-enable all locales once localeData support is added. |
34 |
| - // { lang: 'en-US', translation: 'Hello i18n!' }, |
35 |
| - { lang: 'fr', translation: 'Bonjour i18n!' }, |
36 |
| - ]; |
37 |
| - |
38 |
| - await updateJsonFile('angular.json', workspaceJson => { |
39 |
| - const appProject = workspaceJson.projects['test-project']; |
40 |
| - const appArchitect = appProject.architect || appProject.targets; |
41 |
| - const serveConfigs = appArchitect['serve'].configurations; |
42 |
| - const e2eConfigs = appArchitect['e2e'].configurations; |
43 |
| - const buildConfigs = appArchitect['build'].configurations; |
44 |
| - |
45 |
| - // Make default builds prod. |
46 |
| - appArchitect['build'].options.optimization = true; |
47 |
| - appArchitect['build'].options.buildOptimizer = true; |
48 |
| - appArchitect['build'].options.aot = true; |
49 |
| - appArchitect['build'].options.fileReplacements = [ |
50 |
| - { |
51 |
| - replace: 'src/environments/environment.ts', |
52 |
| - with: 'src/environments/environment.prod.ts', |
53 |
| - }, |
54 |
| - ]; |
55 |
| - |
56 |
| - // Enable localization for all locales |
57 |
| - // TODO: re-enable all locales once localeData support is added. |
58 |
| - // appArchitect['build'].options.localize = true; |
59 |
| - appArchitect['build'].options.localize = ['fr']; |
60 |
| - // Always error on missing translations. |
61 |
| - appArchitect['build'].options.i18nMissingTranslation = 'error'; |
62 |
| - |
63 |
| - // Add locale definitions to the project |
64 |
| - // tslint:disable-next-line: no-any |
65 |
| - const i18n: Record<string, any> = (appProject.i18n = { locales: {} }); |
66 |
| - for (const { lang } of langTranslations) { |
67 |
| - if (lang == 'en-US') { |
68 |
| - i18n.sourceLocale = lang; |
69 |
| - } else { |
70 |
| - i18n.locales[lang] = `src/locale/messages.${lang}.xlf`; |
71 |
| - } |
72 |
| - |
73 |
| - buildConfigs[lang] = { localize: [lang] }; |
74 |
| - serveConfigs[lang] = { browserTarget: `test-project:build:${lang}` }; |
75 |
| - e2eConfigs[lang] = { |
76 |
| - specs: [`./src/app.${lang}.e2e-spec.ts`], |
77 |
| - devServerTarget: `test-project:serve:${lang}`, |
78 |
| - }; |
79 |
| - } |
80 |
| - }); |
81 |
| - |
82 |
| - // Add a translatable element. |
83 |
| - await writeFile( |
84 |
| - 'src/app/app.component.html', |
85 |
| - '<h1 i18n="An introduction header for this sample">Hello i18n!</h1>', |
86 |
| - ); |
87 |
| - |
88 |
| - // Extract the translation messages and copy them for each language. |
89 |
| - await ng('xi18n', '--output-path=src/locale'); |
90 |
| - await expectFileToExist('src/locale/messages.xlf'); |
91 |
| - await expectFileToMatch('src/locale/messages.xlf', `source-language="en-US"`); |
92 |
| - await expectFileToMatch('src/locale/messages.xlf', `An introduction header for this sample`); |
93 |
| - |
94 |
| - for (const { lang, translation } of langTranslations) { |
95 |
| - if (lang != 'en-US') { |
96 |
| - await copyFile('src/locale/messages.xlf', `src/locale/messages.${lang}.xlf`); |
97 |
| - await replaceInFile( |
98 |
| - `src/locale/messages.${lang}.xlf`, |
99 |
| - 'source-language="en-US"', |
100 |
| - `source-language="en-US" target-language="${lang}"`, |
101 |
| - ); |
102 |
| - await replaceInFile( |
103 |
| - `src/locale/messages.${lang}.xlf`, |
104 |
| - '<source>Hello i18n!</source>', |
105 |
| - `<source>Hello i18n!</source>\n<target>${translation}</target>`, |
106 |
| - ); |
107 |
| - } |
108 |
| - } |
| 18 | + // TODO: re-enable all locales once localeData support is added. |
| 19 | + const tempLangTranslations = langTranslations.filter(l => l.lang == 'fr'); |
109 | 20 |
|
110 | 21 | // Build each locale and verify the output.
|
111 | 22 | // NOTE: this should not fail in general, but multi-locale translation is currently disabled.
|
112 | 23 | // TODO: remove expectToFail once localeData support is added.
|
113 | 24 | await expectToFail(() => ng('build', '--localize', 'true'));
|
114 | 25 | await ng('build');
|
115 |
| - for (const { lang, translation } of langTranslations) { |
116 |
| - await expectFileToMatch(`${baseDir}/${lang}/main-es5.js`, translation); |
117 |
| - await expectFileToMatch(`${baseDir}/${lang}/main-es2015.js`, translation); |
118 |
| - await expectToFail(() => expectFileToMatch(`${baseDir}/${lang}/main-es5.js`, '$localize`')); |
119 |
| - await expectToFail(() => expectFileToMatch(`${baseDir}/${lang}/main-es2015.js`, '$localize`')); |
120 |
| - await expectFileToMatch(`${baseDir}/${lang}/main-es5.js`, lang); |
121 |
| - await expectFileToMatch(`${baseDir}/${lang}/main-es2015.js`, lang); |
122 |
| - |
123 |
| - // Ivy i18n doesn't yet work with `ng serve` so we must use a separate server. |
124 |
| - const app = express(); |
125 |
| - app.use(express.static(resolve(baseDir, lang))); |
126 |
| - const server = app.listen(4200, 'localhost'); |
| 26 | + for (const { lang, outputPath, translation } of tempLangTranslations) { |
| 27 | + await expectFileToMatch(`${outputPath}/main-es5.js`, translation.helloPartial); |
| 28 | + await expectFileToMatch(`${outputPath}/main-es2015.js`, translation.helloPartial); |
| 29 | + await expectToFail(() => expectFileToMatch(`${outputPath}/main-es5.js`, '$localize`')); |
| 30 | + await expectToFail(() => expectFileToMatch(`${outputPath}/main-es2015.js`, '$localize`')); |
| 31 | + await expectFileToMatch(`${outputPath}/main-es5.js`, lang); |
| 32 | + await expectFileToMatch(`${outputPath}/main-es2015.js`, lang); |
| 33 | + |
| 34 | + const server = externalServer(outputPath); |
127 | 35 | try {
|
128 |
| - // Add E2E test for locale |
129 |
| - await writeFile( |
130 |
| - 'e2e/src/app.e2e-spec.ts', |
131 |
| - ` |
132 |
| - import { browser, logging, element, by } from 'protractor'; |
133 |
| - describe('workspace-project App', () => { |
134 |
| - it('should display welcome message', () => { |
135 |
| - browser.get(browser.baseUrl); |
136 |
| - expect(element(by.css('h1')).getText()).toEqual('${translation}'); |
137 |
| - }); |
138 |
| - afterEach(async () => { |
139 |
| - // Assert that there are no errors emitted from the browser |
140 |
| - const logs = await browser.manage().logs().get(logging.Type.BROWSER); |
141 |
| - expect(logs).not.toContain(jasmine.objectContaining({ |
142 |
| - level: logging.Level.SEVERE, |
143 |
| - } as logging.Entry)); |
144 |
| - }); |
145 |
| - }); |
146 |
| - `, |
147 |
| - ); |
148 |
| - |
149 | 36 | // Execute without a devserver.
|
150 |
| - await ng('e2e', '--devServerTarget='); |
| 37 | + await ng('e2e', `--configuration=${lang}`, '--devServerTarget='); |
151 | 38 | } finally {
|
152 | 39 | server.close();
|
153 | 40 | }
|
154 | 41 | }
|
155 | 42 |
|
156 |
| - // Verify locale data registration (currently only for single locale builds) |
157 |
| - await ng('build', '--optimization', 'false', '-c', 'fr', '--i18n-missing-translation', 'error'); |
158 |
| - await expectFileToMatch(`${baseDir}/fr/main-es5.js`, 'registerLocaleData'); |
159 |
| - await expectFileToMatch(`${baseDir}/fr/main-es2015.js`, 'registerLocaleData'); |
160 |
| - |
161 | 43 | // Verify missing translation behaviour.
|
162 | 44 | await appendToFile('src/app/app.component.html', '<p i18n>Other content</p>');
|
163 | 45 | await ng('build', '--i18n-missing-translation', 'ignore');
|
|
0 commit comments