1
1
import { readFileSync , readdirSync } from 'node:fs' ;
2
2
import { getGlobalVariable } from '../../utils/env' ;
3
- import { copyFile , expectFileToMatch , replaceInFile , writeFile } from '../../utils/fs' ;
4
3
import { installWorkspacePackages , uninstallPackage } from '../../utils/packages' ;
5
4
import { ng } from '../../utils/process' ;
6
5
import { updateJsonFile , useSha } from '../../utils/project' ;
7
- import { readNgVersion } from '../../utils/version' ;
8
-
9
- const snapshots = require ( '../../ng-snapshot/package.json' ) ;
6
+ import { langTranslations , setupI18nConfig } from './setup' ;
7
+ import { expectFileToMatch } from '../../utils/fs' ;
10
8
11
9
export default async function ( ) {
12
10
const useWebpackBuilder = ! getGlobalVariable ( 'argv' ) [ 'esbuild' ] ;
@@ -15,12 +13,28 @@ export default async function () {
15
13
return ;
16
14
}
17
15
18
- const isSnapshotBuild = getGlobalVariable ( 'argv' ) [ 'ng-snapshots' ] ;
19
- await updateJsonFile ( 'package.json' , ( packageJson ) => {
20
- const dependencies = packageJson [ 'dependencies' ] ;
21
- dependencies [ '@angular/localize' ] = isSnapshotBuild
22
- ? snapshots . dependencies [ '@angular/localize' ]
23
- : readNgVersion ( ) ;
16
+ // Setup i18n tests and config.
17
+ await setupI18nConfig ( ) ;
18
+
19
+ // Update angular.json
20
+ await updateJsonFile ( 'angular.json' , ( workspaceJson ) => {
21
+ const appProject = workspaceJson . projects [ 'test-project' ] ;
22
+ // tslint:disable-next-line: no-any
23
+ const i18n : Record < string , any > = appProject . i18n ;
24
+
25
+ i18n . sourceLocale = {
26
+ baseHref : '' ,
27
+ } ;
28
+
29
+ i18n . locales [ 'fr' ] = {
30
+ translation : i18n . locales [ 'fr' ] ,
31
+ baseHref : '' ,
32
+ } ;
33
+
34
+ i18n . locales [ 'de' ] = {
35
+ translation : i18n . locales [ 'de' ] ,
36
+ baseHref : '' ,
37
+ } ;
24
38
} ) ;
25
39
26
40
// forcibly remove in case another test doesn't clean itself up
@@ -31,65 +45,12 @@ export default async function () {
31
45
await useSha ( ) ;
32
46
await installWorkspacePackages ( ) ;
33
47
34
- // Set configurations for each locale.
35
- const langTranslations = [
36
- { lang : 'en-US' , translation : 'Hello i18n!' } ,
37
- { lang : 'fr' , translation : 'Bonjour i18n!' } ,
38
- ] ;
39
-
40
- await updateJsonFile ( 'angular.json' , ( workspaceJson ) => {
41
- const appProject = workspaceJson . projects [ 'test-project' ] ;
42
- const appArchitect = appProject . architect || appProject . targets ;
43
- const buildOptions = appArchitect [ 'build' ] . options ;
44
-
45
- // Enable localization for all locales
46
- buildOptions . localize = true ;
47
-
48
- // Add locale definitions to the project
49
- const i18n : Record < string , any > = ( appProject . i18n = { locales : { } } ) ;
50
- for ( const { lang } of langTranslations ) {
51
- if ( lang == 'en-US' ) {
52
- i18n . sourceLocale = lang ;
53
- } else {
54
- i18n . locales [ lang ] = `src/locale/messages.${ lang } .xlf` ;
55
- }
56
- }
57
- } ) ;
58
-
59
- // Add a translatable element
60
- // Extraction of i18n only works on browser targets.
61
- // Let's add the same translation that there is in the app-shell
62
- await writeFile (
63
- 'src/app/app.component.html' ,
64
- '<h1 i18n="An introduction header for this sample">Hello i18n!</h1>' ,
65
- ) ;
66
-
67
- // Extract the translation messages and copy them for each language.
68
- await ng ( 'extract-i18n' , '--output-path=src/locale' ) ;
69
- await expectFileToMatch ( 'src/locale/messages.xlf' , `source-language="en-US"` ) ;
70
- await expectFileToMatch ( 'src/locale/messages.xlf' , `An introduction header for this sample` ) ;
71
-
72
- for ( const { lang, translation } of langTranslations ) {
73
- if ( lang != 'en-US' ) {
74
- await copyFile ( 'src/locale/messages.xlf' , `src/locale/messages.${ lang } .xlf` ) ;
75
- await replaceInFile (
76
- `src/locale/messages.${ lang } .xlf` ,
77
- 'source-language="en-US"' ,
78
- `source-language="en-US" target-language="${ lang } "` ,
79
- ) ;
80
- await replaceInFile (
81
- `src/locale/messages.${ lang } .xlf` ,
82
- '<source>Hello i18n!</source>' ,
83
- `<source>Hello i18n!</source>\n<target>${ translation } </target>` ,
84
- ) ;
85
- }
86
- }
87
-
88
48
// Build each locale and verify the output.
89
49
await ng ( 'build' , '--output-hashing=none' ) ;
90
50
91
51
for ( const { lang, translation } of langTranslations ) {
92
52
let foundTranslation = false ;
53
+ let foundLocaleData = false ;
93
54
94
55
// The translation may be in any of the lazy-loaded generated chunks
95
56
for ( const entry of readdirSync ( `dist/test-project/server/${ lang } /` ) ) {
@@ -98,14 +59,23 @@ export default async function () {
98
59
}
99
60
100
61
const contents = readFileSync ( `dist/test-project/server/${ lang } /${ entry } ` , 'utf-8' ) ;
101
- foundTranslation ||= contents . includes ( translation ) ;
102
- if ( foundTranslation ) {
62
+
63
+ // Check for translated content
64
+ foundTranslation ||= contents . includes ( translation . helloPartial ) ;
65
+ // Check for the locale data month name to be present
66
+ foundLocaleData ||= contents . includes ( translation . date ) ;
67
+
68
+ if ( foundTranslation && foundLocaleData ) {
103
69
break ;
104
70
}
105
71
}
106
72
107
73
if ( ! foundTranslation ) {
108
74
throw new Error ( `Translation not found in 'dist/test-project/server/${ lang } /'` ) ;
109
75
}
76
+
77
+ if ( ! foundLocaleData ) {
78
+ throw new Error ( `Locale data not found in 'dist/test-project/server/${ lang } /'` ) ;
79
+ }
110
80
}
111
81
}
0 commit comments