@@ -3,6 +3,8 @@ import path from 'path'
33import { Engine } from 'php-parser'
44import { ParsedLangFileInterface } from './interfaces/parsed-lang-file'
55
6+ const toCamelCase = ( str : string ) : string => str . replace ( / ^ \w / , ( c ) => c . toLowerCase ( ) )
7+
68export const hasPhpTranslations = ( folderPath : string ) : boolean => {
79 folderPath = folderPath . replace ( / [ \\ / ] $ / , '' ) + path . sep
810
@@ -51,14 +53,16 @@ export const parseAll = (folderPath: string): ParsedLangFileInterface[] => {
5153 }
5254
5355 // If data contains an object with folder name 'vendor'
54- const vendorIndex = data . findIndex ( ( { folder } ) => folder === 'vendor' ) ;
56+ const vendorIndex = data . findIndex ( ( { folder } ) => folder === 'vendor' )
5557
5658 if ( vendorIndex !== - 1 ) {
57- const vendorTranslations = data [ vendorIndex ] . translations ;
58- data . splice ( vendorIndex , 1 ) ;
59+ const vendorTranslations = data [ vendorIndex ] . translations
60+ data . splice ( vendorIndex , 1 )
5961
60- data . forEach ( langFile =>
61- langFile . translations = mergeVendorTranslations ( langFile . folder , langFile . translations , vendorTranslations ) ) ;
62+ data . forEach (
63+ ( langFile ) =>
64+ ( langFile . translations = mergeVendorTranslations ( langFile . folder , langFile . translations , vendorTranslations ) )
65+ )
6266 }
6367
6468 return data
@@ -75,16 +79,18 @@ export const parseAll = (folderPath: string): ParsedLangFileInterface[] => {
7579
7680function mergeVendorTranslations ( folder : string , translations : any , vendorTranslations : any ) {
7781 // Filter the translations from the vendor file that match the current folder
78- const langTranslationsFromVendor = Object
79- . entries ( vendorTranslations )
82+ const langTranslationsFromVendor = Object . entries ( vendorTranslations )
8083 . filter ( ( [ key ] ) => key . includes ( `.${ folder } .` ) )
81- . reduce ( ( acc , [ key , value ] ) => ( {
82- ...acc ,
83- [ key . replace ( `.${ folder } .` , '::' ) ] : value ,
84- } ) , { } ) ;
84+ . reduce (
85+ ( acc , [ key , value ] ) => ( {
86+ ...acc ,
87+ [ key . replace ( `.${ folder } .` , '::' ) ] : value
88+ } ) ,
89+ { }
90+ )
8591
8692 // Merge the vendor translations that matched the folder with the current translations
87- return { ...translations , ...langTranslationsFromVendor } ;
93+ return { ...translations , ...langTranslationsFromVendor }
8894}
8995
9096export const parse = ( content : string ) => {
@@ -121,7 +127,17 @@ const parseItem = (expr) => {
121127 }
122128
123129 if ( expr . key ) {
124- return { [ expr . key . value ] : parseItem ( expr . value ) }
130+ let key = expr . key . value
131+
132+ if ( expr . key . kind === 'staticlookup' ) {
133+ key = toCamelCase ( expr . key . what . name )
134+ }
135+
136+ if ( expr . key . kind === 'propertylookup' ) {
137+ key = toCamelCase ( expr . key . what . offset . name )
138+ }
139+
140+ return { [ key ] : parseItem ( expr . value ) }
125141 }
126142
127143 return parseItem ( expr . value )
@@ -181,7 +197,7 @@ export const readThroughDir = (dir) => {
181197}
182198
183199export const prepareExtendedParsedLangFiles = ( langPaths : string [ ] ) : ParsedLangFileInterface [ ] =>
184- langPaths . flatMap ( langPath => parseAll ( langPath ) ) ;
200+ langPaths . flatMap ( ( langPath ) => parseAll ( langPath ) )
185201
186202export const generateFiles = ( langPath : string , data : ParsedLangFileInterface [ ] ) : ParsedLangFileInterface [ ] => {
187203 data = mergeData ( data )
0 commit comments