11import fs from 'fs' ;
22import { generateFiles , parseAll , parse , hasPhpTranslations , reset , prepareExtendedParsedLangFiles } from '../src/loader' ;
3+ import { isolateFolder , removeIsolatedFolder } from './folderIsolationUtil'
34
4- beforeEach ( ( ) => reset ( __dirname + '/fixtures/lang/' ) ) ;
5+ const isolatedFixtures = isolateFolder ( __dirname + '/fixtures' , 'loader' ) ;
6+ afterAll ( ( ) => removeIsolatedFolder ( isolatedFixtures ) ) ;
7+
8+ beforeEach ( ( ) => reset ( isolatedFixtures + '/lang/' ) ) ;
59
610it ( 'creates a file for each lang' , ( ) => {
7- const langPath = __dirname + '/fixtures /lang/' ;
11+ const langPath = isolatedFixtures + '/lang/' ;
812 const files = generateFiles ( langPath , parseAll ( langPath ) ) ;
913
1014 expect ( files . length ) . toBe ( 3 ) ;
@@ -22,8 +26,32 @@ it('creates a file for each lang', () => {
2226 expect ( langPt [ 'auth.foo.level1.level2' ] ) . toBe ( 'barpt' ) ;
2327} ) ;
2428
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+
2553it ( 'includes .php lang file in subdirectory in .json' , ( ) => {
26- const langPath = __dirname + '/fixtures /lang/' ;
54+ const langPath = isolatedFixtures + '/lang/' ;
2755 const files = generateFiles ( langPath , parseAll ( langPath ) ) ;
2856 const langEn = JSON . parse ( fs . readFileSync ( langPath + files [ 0 ] . name ) . toString ( ) ) ;
2957
@@ -33,7 +61,7 @@ it('includes .php lang file in subdirectory in .json', () => {
3361} ) ;
3462
3563it ( 'includes .php lang file in nested subdirectory in .json' , ( ) => {
36- const langPath = __dirname + '/fixtures /lang/' ;
64+ const langPath = isolatedFixtures + '/lang/' ;
3765 const files = generateFiles ( langPath , parseAll ( langPath ) ) ;
3866 const langEn = JSON . parse ( fs . readFileSync ( langPath + files [ 0 ] . name ) . toString ( ) )
3967
@@ -42,9 +70,9 @@ it('includes .php lang file in nested subdirectory in .json', () => {
4270} )
4371
4472it ( 'inclues additional lang paths to load from' , ( ) => {
45- const langPath = __dirname + '/fixtures /lang/' ;
73+ const langPath = isolatedFixtures + '/lang/' ;
4674 const additionalLangPaths = [
47- __dirname + '/fixtures /locales/'
75+ isolatedFixtures + '/locales/'
4876 ] ;
4977
5078 const langPaths = prepareExtendedParsedLangFiles ( [
@@ -60,9 +88,9 @@ it('inclues additional lang paths to load from', () => {
6088} ) ;
6189
6290it ( 'overwrites translations from additional lang paths' , ( ) => {
63- const langPath = __dirname + '/fixtures /lang/' ;
91+ const langPath = isolatedFixtures + '/lang/' ;
6492 const additionalLangPaths = [
65- __dirname + '/fixtures /locales/'
93+ isolatedFixtures + '/locales/'
6694 ] ;
6795
6896 const langPaths = prepareExtendedParsedLangFiles ( [
@@ -79,33 +107,33 @@ it('overwrites translations from additional lang paths', () => {
79107} ) ;
80108
81109it ( '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 ( ) ) ;
83111
84112 expect ( lang [ 'failed' ] ) . toBe ( 'These credentials do not match our records.' ) ;
85113} ) ;
86114
87115it ( '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 ( ) ) ;
89117 expect ( langPt [ 'foo.level1.level2' ] ) . toBe ( 'barpt' ) ;
90118
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 ( ) ) ;
92120 expect ( langEn [ 'foo.level1.level2' ] ) . toBe ( 'baren' ) ;
93121} ) ;
94122
95123it ( '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 ( ) ) ;
97125 expect ( lang [ 'arr.0' ] ) . toBe ( 'foo' ) ;
98126 expect ( lang [ 'arr.1' ] ) . toBe ( 'bar' ) ;
99127} ) ;
100128
101129it ( '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 ( ) ) ;
103131
104132 expect ( lang [ 'empty_array' ] ) . toBe ( undefined ) ;
105133 expect ( lang [ 'null' ] ) . toBe ( undefined ) ;
106134} ) ;
107135
108136it ( '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 ) ;
111139} ) ;
0 commit comments