1
1
import fs from 'fs' ;
2
2
import { generateFiles , parseAll , parse , hasPhpTranslations , reset , prepareExtendedParsedLangFiles } from '../src/loader' ;
3
+ import { isolateFolder , removeIsolatedFolder } from './folderIsolationUtil'
3
4
4
- beforeEach ( ( ) => reset ( __dirname + '/fixtures/lang/' ) ) ;
5
+ const isolatedFixtures = isolateFolder ( __dirname + '/fixtures' , 'loader' ) ;
6
+ afterAll ( ( ) => removeIsolatedFolder ( isolatedFixtures ) ) ;
7
+
8
+ beforeEach ( ( ) => reset ( isolatedFixtures + '/lang/' ) ) ;
5
9
6
10
it ( 'creates a file for each lang' , ( ) => {
7
- const langPath = __dirname + '/fixtures /lang/' ;
11
+ const langPath = isolatedFixtures + '/lang/' ;
8
12
const files = generateFiles ( langPath , parseAll ( langPath ) ) ;
9
13
10
14
expect ( files . length ) . toBe ( 3 ) ;
@@ -22,8 +26,32 @@ it('creates a file for each lang', () => {
22
26
expect ( langPt [ 'auth.foo.level1.level2' ] ) . toBe ( 'barpt' ) ;
23
27
} ) ;
24
28
29
+ it ( 'merges published package translations into each lang .json' , ( ) => {
30
+ const langPath = isolatedFixtures + '/lang/' ;
31
+ const files = generateFiles ( langPath , parseAll ( langPath ) ) ;
32
+
33
+ expect ( files . length ) . toBe ( 3 ) ;
34
+ expect ( files [ 0 ] . name ) . toBe ( 'php_en.json' ) ;
35
+ expect ( files [ 1 ] . name ) . toBe ( 'php_fr.json' ) ;
36
+ expect ( files [ 2 ] . name ) . toBe ( 'php_pt.json' ) ;
37
+
38
+ const langEn = JSON . parse ( fs . readFileSync ( langPath + files [ 0 ] . name ) . toString ( ) ) ;
39
+ expect ( langEn [ 'package-example::messages.welcome' ] ) . toBe ( 'Welcome to the example package.' ) ;
40
+ expect ( langEn [ 'package-example::messages.foo.level1.level2' ] ) . toBe ( 'package' ) ;
41
+ expect ( langEn [ 'package-example::messages.multiline' ] ) . toBe ( 'Lorem ipsum dolor sit amet.' ) ;
42
+
43
+ const langFr = JSON . parse ( fs . readFileSync ( langPath + files [ 1 ] . name ) . toString ( ) ) ;
44
+ expect ( langFr [ 'package-example::messages.welcome' ] ) . toBeUndefined ( ) ;
45
+ expect ( langFr [ 'package-example::messages.foo.level1.level2' ] ) . toBeUndefined ( ) ;
46
+ expect ( langFr [ 'package-example::messages.multiline' ] ) . toBeUndefined ( ) ;
47
+
48
+ const langPt = JSON . parse ( fs . readFileSync ( langPath + files [ 2 ] . name ) . toString ( ) ) ;
49
+ expect ( langPt [ 'package-example::messages.welcome' ] ) . toBe ( 'Bem-vindo ao exemplo do pacote.' ) ;
50
+ expect ( langPt [ 'package-example::messages.foo.level1.level2' ] ) . toBe ( 'pacote' ) ;
51
+ } ) ;
52
+
25
53
it ( 'includes .php lang file in subdirectory in .json' , ( ) => {
26
- const langPath = __dirname + '/fixtures /lang/' ;
54
+ const langPath = isolatedFixtures + '/lang/' ;
27
55
const files = generateFiles ( langPath , parseAll ( langPath ) ) ;
28
56
const langEn = JSON . parse ( fs . readFileSync ( langPath + files [ 0 ] . name ) . toString ( ) ) ;
29
57
@@ -33,7 +61,7 @@ it('includes .php lang file in subdirectory in .json', () => {
33
61
} ) ;
34
62
35
63
it ( 'includes .php lang file in nested subdirectory in .json' , ( ) => {
36
- const langPath = __dirname + '/fixtures /lang/' ;
64
+ const langPath = isolatedFixtures + '/lang/' ;
37
65
const files = generateFiles ( langPath , parseAll ( langPath ) ) ;
38
66
const langEn = JSON . parse ( fs . readFileSync ( langPath + files [ 0 ] . name ) . toString ( ) )
39
67
@@ -42,9 +70,9 @@ it('includes .php lang file in nested subdirectory in .json', () => {
42
70
} )
43
71
44
72
it ( 'inclues additional lang paths to load from' , ( ) => {
45
- const langPath = __dirname + '/fixtures /lang/' ;
73
+ const langPath = isolatedFixtures + '/lang/' ;
46
74
const additionalLangPaths = [
47
- __dirname + '/fixtures /locales/'
75
+ isolatedFixtures + '/locales/'
48
76
] ;
49
77
50
78
const langPaths = prepareExtendedParsedLangFiles ( [
@@ -60,9 +88,9 @@ it('inclues additional lang paths to load from', () => {
60
88
} ) ;
61
89
62
90
it ( 'overwrites translations from additional lang paths' , ( ) => {
63
- const langPath = __dirname + '/fixtures /lang/' ;
91
+ const langPath = isolatedFixtures + '/lang/' ;
64
92
const additionalLangPaths = [
65
- __dirname + '/fixtures /locales/'
93
+ isolatedFixtures + '/locales/'
66
94
] ;
67
95
68
96
const langPaths = prepareExtendedParsedLangFiles ( [
@@ -79,33 +107,33 @@ it('overwrites translations from additional lang paths', () => {
79
107
} ) ;
80
108
81
109
it ( 'transforms .php lang to .json' , ( ) => {
82
- const lang = parse ( fs . readFileSync ( __dirname + '/fixtures /lang/en/auth.php' ) . toString ( ) ) ;
110
+ const lang = parse ( fs . readFileSync ( isolatedFixtures + '/lang/en/auth.php' ) . toString ( ) ) ;
83
111
84
112
expect ( lang [ 'failed' ] ) . toBe ( 'These credentials do not match our records.' ) ;
85
113
} ) ;
86
114
87
115
it ( 'transform nested .php lang files to .json' , ( ) => {
88
- const langPt = parse ( fs . readFileSync ( __dirname + '/fixtures /lang/pt/auth.php' ) . toString ( ) ) ;
116
+ const langPt = parse ( fs . readFileSync ( isolatedFixtures + '/lang/pt/auth.php' ) . toString ( ) ) ;
89
117
expect ( langPt [ 'foo.level1.level2' ] ) . toBe ( 'barpt' ) ;
90
118
91
- const langEn = parse ( fs . readFileSync ( __dirname + '/fixtures /lang/en/auth.php' ) . toString ( ) ) ;
119
+ const langEn = parse ( fs . readFileSync ( isolatedFixtures + '/lang/en/auth.php' ) . toString ( ) ) ;
92
120
expect ( langEn [ 'foo.level1.level2' ] ) . toBe ( 'baren' ) ;
93
121
} ) ;
94
122
95
123
it ( 'transforms simple index array to .json' , ( ) => {
96
- const lang = parse ( fs . readFileSync ( __dirname + '/fixtures /lang/en/auth.php' ) . toString ( ) ) ;
124
+ const lang = parse ( fs . readFileSync ( isolatedFixtures + '/lang/en/auth.php' ) . toString ( ) ) ;
97
125
expect ( lang [ 'arr.0' ] ) . toBe ( 'foo' ) ;
98
126
expect ( lang [ 'arr.1' ] ) . toBe ( 'bar' ) ;
99
127
} ) ;
100
128
101
129
it ( 'ignores empty `array` or `null` translations' , ( ) => {
102
- const lang = parse ( fs . readFileSync ( __dirname + '/fixtures /lang/en/ignore.php' ) . toString ( ) ) ;
130
+ const lang = parse ( fs . readFileSync ( isolatedFixtures + '/lang/en/ignore.php' ) . toString ( ) ) ;
103
131
104
132
expect ( lang [ 'empty_array' ] ) . toBe ( undefined ) ;
105
133
expect ( lang [ 'null' ] ) . toBe ( undefined ) ;
106
134
} ) ;
107
135
108
136
it ( 'checks if there is .php translations' , ( ) => {
109
- expect ( hasPhpTranslations ( __dirname + '/fixtures /lang/' ) ) . toBe ( true ) ;
110
- expect ( hasPhpTranslations ( __dirname + '/fixtures /wronglangfolder/' ) ) . toBe ( false ) ;
137
+ expect ( hasPhpTranslations ( isolatedFixtures + '/lang/' ) ) . toBe ( true ) ;
138
+ expect ( hasPhpTranslations ( isolatedFixtures + '/wronglangfolder/' ) ) . toBe ( false ) ;
111
139
} ) ;
0 commit comments