1
1
import express from 'express' ;
2
- import { resolve } from 'path' ;
2
+ import { dirname , resolve } from 'path' ;
3
3
import { getGlobalVariable } from '../../utils/env' ;
4
- import {
5
- appendToFile ,
6
- copyFile ,
7
- expectFileToExist ,
8
- expectFileToMatch ,
9
- replaceInFile ,
10
- writeFile ,
11
- } from '../../utils/fs' ;
4
+ import { appendToFile , copyFile , createDir , replaceInFile , writeFile } from '../../utils/fs' ;
12
5
import { installPackage } from '../../utils/packages' ;
13
- import { ng } from '../../utils/process' ;
14
6
import { updateJsonFile } from '../../utils/project' ;
15
7
import { readNgVersion } from '../../utils/version' ;
16
8
import { Server } from 'http' ;
17
9
import { AddressInfo } from 'net' ;
18
10
19
11
// Configurations for each locale.
12
+ const translationFile = 'src/locale/messages.xlf' ;
20
13
export const baseDir = 'dist/test-project' ;
21
14
export const langTranslations = [
22
15
{
@@ -96,45 +89,13 @@ export async function externalServer(outputPath: string, baseUrl = '/'): Promise
96
89
} ) ;
97
90
}
98
91
99
- export const formats = {
100
- 'xlf' : {
101
- ext : 'xlf' ,
102
- sourceCheck : 'source-language="en-US"' ,
103
- replacements : [ [ / s o u r c e / g, 'target' ] ] ,
104
- } ,
105
- 'xlf2' : {
106
- ext : 'xlf' ,
107
- sourceCheck : 'srcLang="en-US"' ,
108
- replacements : [ [ / s o u r c e / g, 'target' ] ] ,
109
- } ,
110
- 'xmb' : {
111
- ext : 'xmb' ,
112
- sourceCheck : '<!DOCTYPE messagebundle' ,
113
- replacements : [
114
- [ / m e s s a g e b u n d l e / g, 'translationbundle' ] ,
115
- [ / m s g / g, 'translation' ] ,
116
- [ / < s o u r c e > .* ?< \/ s o u r c e > / g, '' ] ,
117
- ] ,
118
- } ,
119
- 'json' : {
120
- ext : 'json' ,
121
- sourceCheck : '"locale": "en-US"' ,
122
- replacements : [ ] as RegExp [ ] [ ] ,
123
- } ,
124
- 'arb' : {
125
- ext : 'arb' ,
126
- sourceCheck : '"@@locale": "en-US"' ,
127
- replacements : [ ] as RegExp [ ] [ ] ,
128
- } ,
129
- } ;
130
-
131
92
export const baseHrefs : { [ l : string ] : string } = {
132
93
'en-US' : '/en/' ,
133
94
fr : '/fr-FR/' ,
134
95
de : '' ,
135
96
} ;
136
97
137
- export async function setupI18nConfig ( format : keyof typeof formats = 'xlf' ) {
98
+ export async function setupI18nConfig ( ) {
138
99
// Add component with i18n content, both translations and localeData (plural, dates).
139
100
await writeFile (
140
101
'src/app/app.component.ts' ,
@@ -162,6 +123,41 @@ export async function setupI18nConfig(format: keyof typeof formats = 'xlf') {
162
123
` ,
163
124
) ;
164
125
126
+ await createDir ( dirname ( translationFile ) ) ;
127
+ await writeFile (
128
+ translationFile ,
129
+ `
130
+ <?xml version="1.0" encoding="UTF-8" ?>
131
+ <xliff version="1.2" xmlns="urn:oasis:names:tc:xliff:document:1.2">
132
+ <file source-language="en-US" datatype="plaintext" original="ng2.template">
133
+ <body>
134
+ <trans-unit id="4286451273117902052" datatype="html">
135
+ <source>Hello <x id="INTERPOLATION" equiv-text="{{ title }}"/>! </source>
136
+ <context-group purpose="location">
137
+ <context context-type="sourcefile">src/app/app.component.html</context>
138
+ <context context-type="linenumber">2,3</context>
139
+ </context-group>
140
+ <note priority="1" from="description">An introduction header for this sample</note>
141
+ </trans-unit>
142
+ <trans-unit id="4606963464835766483" datatype="html">
143
+ <source>Updated <x id="ICU" equiv-text="{minutes, plural, =0 {just now} =1 {one minute ago} other {{{minutes}} minutes ago}}" xid="1887283401472369100"/></source>
144
+ <context-group purpose="location">
145
+ <context context-type="sourcefile">src/app/app.component.html</context>
146
+ <context context-type="linenumber">5,6</context>
147
+ </context-group>
148
+ </trans-unit>
149
+ <trans-unit id="2002272803511843863" datatype="html">
150
+ <source>{VAR_PLURAL, plural, =0 {just now} =1 {one minute ago} other {<x id="INTERPOLATION"/> minutes ago}}</source>
151
+ <context-group purpose="location">
152
+ <context context-type="sourcefile">src/app/app.component.html</context>
153
+ <context context-type="linenumber">5,6</context>
154
+ </context-group>
155
+ </trans-unit>
156
+ </body>
157
+ </file>
158
+ </xliff>` ,
159
+ ) ;
160
+
165
161
// Add a dynamic import to ensure syntax is supported
166
162
// ng serve support: https://github.com/angular/angular-cli/issues/16248
167
163
await writeFile ( 'src/app/dynamic.ts' , `export const abc = 5;` ) ;
@@ -241,11 +237,10 @@ export async function setupI18nConfig(format: keyof typeof formats = 'xlf') {
241
237
if ( lang === sourceLocale ) {
242
238
i18n . sourceLocale = lang ;
243
239
} else {
244
- i18n . locales [ lang ] = `src/locale/messages.${ lang } .${ formats [ format ] . ext } ` ;
240
+ i18n . locales [ lang ] = `src/locale/messages.${ lang } .xlf ` ;
245
241
}
246
242
247
243
buildConfigs [ lang ] = { localize : [ lang ] } ;
248
-
249
244
serveConfigs [ lang ] = { browserTarget : `test-project:build:${ lang } ` } ;
250
245
e2eConfigs [ lang ] = {
251
246
specs : [ `./src/app.${ lang } .e2e-spec.ts` ] ,
@@ -259,32 +254,24 @@ export async function setupI18nConfig(format: keyof typeof formats = 'xlf') {
259
254
if ( getGlobalVariable ( 'argv' ) [ 'ng-snapshots' ] ) {
260
255
localizeVersion = require ( '../../ng-snapshot/package.json' ) . dependencies [ '@angular/localize' ] ;
261
256
}
262
- await installPackage ( localizeVersion ) ;
263
257
264
- // Extract the translation messages.
265
- await ng ( 'extract-i18n' , '--output-path=src/locale' , `--format=${ format } ` ) ;
266
- const translationFile = `src/locale/messages.${ formats [ format ] . ext } ` ;
267
- await expectFileToExist ( translationFile ) ;
268
- await expectFileToMatch ( translationFile , formats [ format ] . sourceCheck ) ;
269
-
270
- if ( format !== 'json' ) {
271
- await expectFileToMatch ( translationFile , `An introduction header for this sample` ) ;
272
- }
258
+ await installPackage ( localizeVersion ) ;
273
259
274
260
// Make translations for each language.
275
261
for ( const { lang, translationReplacements } of langTranslations ) {
276
262
if ( lang != sourceLocale ) {
277
- await copyFile ( translationFile , `src/locale/messages.${ lang } .${ formats [ format ] . ext } ` ) ;
263
+ await copyFile ( translationFile , `src/locale/messages.${ lang } .xlf ` ) ;
278
264
for ( const replacements of translationReplacements ! ) {
279
265
await replaceInFile (
280
- `src/locale/messages.${ lang } .${ formats [ format ] . ext } ` ,
266
+ `src/locale/messages.${ lang } .xlf ` ,
281
267
new RegExp ( replacements [ 0 ] , 'g' ) ,
282
268
replacements [ 1 ] as string ,
283
269
) ;
284
270
}
285
- for ( const replacement of formats [ format ] . replacements ) {
271
+
272
+ for ( const replacement of [ [ / s o u r c e / g, 'target' ] ] ) {
286
273
await replaceInFile (
287
- `src/locale/messages.${ lang } .${ formats [ format ] . ext } ` ,
274
+ `src/locale/messages.${ lang } .xlf ` ,
288
275
new RegExp ( replacement [ 0 ] , 'g' ) ,
289
276
replacement [ 1 ] as string ,
290
277
) ;
0 commit comments