@@ -28,7 +28,6 @@ export const langTranslations = [
2828 date : 'janvier' ,
2929 } ,
3030 translationReplacements : [
31- [ / s o u r c e / 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- [ / s o u r c e / 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+ [ / s o u r c e / g, 'target' ] ,
73+ ] ,
74+ } ,
75+ 'xlf2' : {
76+ ext : 'xlf' ,
77+ sourceCheck : 'srcLang="en-US"' ,
78+ replacements : [
79+ [ / s o u r c e / g, 'target' ] ,
80+ ] ,
81+ } ,
82+ 'xmb' : {
83+ ext : 'xmb' ,
84+ sourceCheck : '<!DOCTYPE messagebundle' ,
85+ replacements : [
86+ [ / m e s s a g e b u n d l e / g, 'translationbundle' ] ,
87+ [ / m s g / g, 'translation' ] ,
88+ [ / < s o u r c e > .* ?< \/ s o u r c e > / 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