Skip to content

Commit 37912fc

Browse files
clydinvikerman
authored andcommitted
test: add E2E tests for supported i18n translation file formats
1 parent b8e8fb6 commit 37912fc

File tree

4 files changed

+77
-14
lines changed

4 files changed

+77
-14
lines changed
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
import { executeTest } from './ivy-localize-dl-xliff2';
2+
import { setupI18nConfig } from './legacy';
3+
4+
export default async function() {
5+
// Setup i18n tests and config.
6+
await setupI18nConfig(true, 'xlf');
7+
8+
// Execute the tests
9+
await executeTest();
10+
}

tests/legacy-cli/e2e/tests/i18n/ivy-localize-dl.ts renamed to tests/legacy-cli/e2e/tests/i18n/ivy-localize-dl-xliff2.ts

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,13 @@ import { baseDir, externalServer, langTranslations, setupI18nConfig } from './le
66

77
export default async function() {
88
// Setup i18n tests and config.
9-
await setupI18nConfig();
9+
await setupI18nConfig(true, 'xlf2');
1010

11+
// Execute the tests
12+
await executeTest();
13+
}
14+
15+
export async function executeTest() {
1116
// Ensure a DL build is used.
1217
await updateJsonFile('tsconfig.json', config => {
1318
config.compilerOptions.target = 'es2015';
@@ -45,7 +50,9 @@ export default async function() {
4550
// Verify deprecated locale data registration is not present
4651
await ng('build', '--configuration=fr', '--optimization=false');
4752
await expectToFail(() => expectFileToMatch(`${baseDir}/fr/main-es5.js`, 'registerLocaleData('));
48-
await expectToFail(() => expectFileToMatch(`${baseDir}/fr/main-es2015.js`, 'registerLocaleData('));
53+
await expectToFail(() =>
54+
expectFileToMatch(`${baseDir}/fr/main-es2015.js`, 'registerLocaleData('),
55+
);
4956

5057
// Verify missing translation behaviour.
5158
await appendToFile('src/app/app.component.html', '<p i18n>Other content</p>');
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
import { executeTest } from './ivy-localize-dl-xliff2';
2+
import { setupI18nConfig } from './legacy';
3+
4+
export default async function() {
5+
// Setup i18n tests and config.
6+
await setupI18nConfig(true, 'xmb');
7+
8+
// Execute the tests
9+
await executeTest();
10+
}

tests/legacy-cli/e2e/tests/i18n/legacy.ts

Lines changed: 48 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,6 @@ export const langTranslations = [
2828
date: 'janvier',
2929
},
3030
translationReplacements: [
31-
[/source/g, 'target'],
3231
['Hello', 'Bonjour'],
3332
['Updated', 'Mis à jour'],
3433
['just now', 'juste maintenant'],
@@ -46,7 +45,6 @@ export const langTranslations = [
4645
date: 'Januar',
4746
},
4847
translationReplacements: [
49-
[/source/g, 'target'],
5048
['Hello', 'Hallo'],
5149
['Updated', 'Aktualisiert'],
5250
['just now', 'gerade jetzt'],
@@ -66,7 +64,33 @@ export const externalServer = (outputPath: string) => {
6664
return app.listen(4200, 'localhost');
6765
};
6866

69-
export async function setupI18nConfig(useLocalize = true) {
67+
export const formats = {
68+
'xlf': {
69+
ext: 'xlf',
70+
sourceCheck: 'source-language="en-US"',
71+
replacements: [
72+
[/source/g, 'target'],
73+
],
74+
},
75+
'xlf2': {
76+
ext: 'xlf',
77+
sourceCheck: 'srcLang="en-US"',
78+
replacements: [
79+
[/source/g, 'target'],
80+
],
81+
},
82+
'xmb': {
83+
ext: 'xmb',
84+
sourceCheck: '<!DOCTYPE messagebundle',
85+
replacements: [
86+
[/messagebundle/g, 'translationbundle'],
87+
[/msg/g, 'translation'],
88+
[/<source>.*?<\/source>/g, ''],
89+
],
90+
},
91+
};
92+
93+
export async function setupI18nConfig(useLocalize = true, format: keyof typeof formats = 'xlf') {
7094
// Add component with i18n content, both translations and localeData (plural, dates).
7195
await writeFile('src/app/app.component.ts', `
7296
import { Component, Inject, LOCALE_ID } from '@angular/core';
@@ -161,16 +185,16 @@ export async function setupI18nConfig(useLocalize = true) {
161185
} else {
162186
buildConfigs[lang] = {
163187
outputPath,
164-
i18nFile: `src/locale/messages.${lang}.xlf`,
165-
i18nFormat: `xlf`,
188+
i18nFile: `src/locale/messages.${lang}.${formats[format].ext}`,
189+
i18nFormat: format,
166190
i18nLocale: lang,
167191
};
168192
}
169193
} else {
170194
if (lang == sourceLocale) {
171195
i18n.sourceLocale = lang;
172196
} else {
173-
i18n.locales[lang] = `src/locale/messages.${lang}.xlf`;
197+
i18n.locales[lang] = `src/locale/messages.${lang}.${formats[format].ext}`;
174198
}
175199
buildConfigs[lang] = { localize: [lang] };
176200
}
@@ -184,17 +208,29 @@ export async function setupI18nConfig(useLocalize = true) {
184208
});
185209

186210
// Extract the translation messages.
187-
await ng('xi18n', '--output-path=src/locale');
188-
await expectFileToExist('src/locale/messages.xlf');
189-
await expectFileToMatch('src/locale/messages.xlf', `source-language="en-US"`);
190-
await expectFileToMatch('src/locale/messages.xlf', `An introduction header for this sample`);
211+
await ng('xi18n', '--output-path=src/locale', `--format=${format}`);
212+
const translationFile = `src/locale/messages.${formats[format].ext}`;
213+
await expectFileToExist(translationFile);
214+
await expectFileToMatch(translationFile, formats[format].sourceCheck);
215+
await expectFileToMatch(translationFile, `An introduction header for this sample`);
191216

192217
// Make translations for each language.
193218
for (const { lang, translationReplacements } of langTranslations) {
194219
if (lang != sourceLocale) {
195-
await copyFile('src/locale/messages.xlf', `src/locale/messages.${lang}.xlf`);
220+
await copyFile(translationFile, `src/locale/messages.${lang}.${formats[format].ext}`);
196221
for (const replacements of translationReplacements) {
197-
await replaceInFile(`src/locale/messages.${lang}.xlf`, replacements[0], replacements[1] as string);
222+
await replaceInFile(
223+
`src/locale/messages.${lang}.${formats[format].ext}`,
224+
replacements[0],
225+
replacements[1] as string,
226+
);
227+
}
228+
for (const replacement of formats[format].replacements) {
229+
await replaceInFile(
230+
`src/locale/messages.${lang}.${formats[format].ext}`,
231+
replacement[0],
232+
replacement[1] as string,
233+
);
198234
}
199235
}
200236
}

0 commit comments

Comments
 (0)