@@ -3,6 +3,14 @@ import path from 'path'
33import { Engine } from 'php-parser'
44import { ParsedLangFileInterface } from './interfaces/parsed-lang-file'
55
6+ const toCamelCase = ( str : string ) : string => {
7+ if ( str === str . toUpperCase ( ) ) {
8+ return str . toLowerCase ( )
9+ }
10+
11+ return str . replace ( / ^ \w / , ( c ) => c . toLowerCase ( ) )
12+ }
13+
614export const hasPhpTranslations = ( folderPath : string ) : boolean => {
715 folderPath = folderPath . replace ( / [ \\ / ] $ / , '' ) + path . sep
816
@@ -51,14 +59,16 @@ export const parseAll = (folderPath: string): ParsedLangFileInterface[] => {
5159 }
5260
5361 // If data contains an object with folder name 'vendor'
54- const vendorIndex = data . findIndex ( ( { folder } ) => folder === 'vendor' ) ;
62+ const vendorIndex = data . findIndex ( ( { folder } ) => folder === 'vendor' )
5563
5664 if ( vendorIndex !== - 1 ) {
57- const vendorTranslations = data [ vendorIndex ] . translations ;
58- data . splice ( vendorIndex , 1 ) ;
65+ const vendorTranslations = data [ vendorIndex ] . translations
66+ data . splice ( vendorIndex , 1 )
5967
60- data . forEach ( langFile =>
61- langFile . translations = mergeVendorTranslations ( langFile . folder , langFile . translations , vendorTranslations ) ) ;
68+ data . forEach (
69+ ( langFile ) =>
70+ ( langFile . translations = mergeVendorTranslations ( langFile . folder , langFile . translations , vendorTranslations ) )
71+ )
6272 }
6373
6474 return data
@@ -75,16 +85,18 @@ export const parseAll = (folderPath: string): ParsedLangFileInterface[] => {
7585
7686function mergeVendorTranslations ( folder : string , translations : any , vendorTranslations : any ) {
7787 // Filter the translations from the vendor file that match the current folder
78- const langTranslationsFromVendor = Object
79- . entries ( vendorTranslations )
88+ const langTranslationsFromVendor = Object . entries ( vendorTranslations )
8089 . filter ( ( [ key ] ) => key . includes ( `.${ folder } .` ) )
81- . reduce ( ( acc , [ key , value ] ) => ( {
82- ...acc ,
83- [ key . replace ( `.${ folder } .` , '::' ) ] : value ,
84- } ) , { } ) ;
90+ . reduce (
91+ ( acc , [ key , value ] ) => ( {
92+ ...acc ,
93+ [ key . replace ( `.${ folder } .` , '::' ) ] : value
94+ } ) ,
95+ { }
96+ )
8597
8698 // Merge the vendor translations that matched the folder with the current translations
87- return { ...translations , ...langTranslationsFromVendor } ;
99+ return { ...translations , ...langTranslationsFromVendor }
88100}
89101
90102export const parse = ( content : string ) => {
@@ -121,7 +133,19 @@ const parseItem = (expr) => {
121133 }
122134
123135 if ( expr . key ) {
124- return { [ expr . key . value ] : parseItem ( expr . value ) }
136+ let key = expr . key . value
137+
138+ if ( expr . key . kind === 'staticlookup' ) {
139+ if ( expr . key . offset . name === 'class' ) {
140+ key = toCamelCase ( expr . key . what . name )
141+ } else {
142+ key = toCamelCase ( expr . key . offset . name )
143+ }
144+ } else if ( expr . key . kind === 'propertylookup' ) {
145+ key = toCamelCase ( expr . key . what . offset . name )
146+ }
147+
148+ return { [ key ] : parseItem ( expr . value ) }
125149 }
126150
127151 return parseItem ( expr . value )
@@ -181,7 +205,7 @@ export const readThroughDir = (dir) => {
181205}
182206
183207export const prepareExtendedParsedLangFiles = ( langPaths : string [ ] ) : ParsedLangFileInterface [ ] =>
184- langPaths . flatMap ( langPath => parseAll ( langPath ) ) ;
208+ langPaths . flatMap ( ( langPath ) => parseAll ( langPath ) )
185209
186210export const generateFiles = ( langPath : string , data : ParsedLangFileInterface [ ] ) : ParsedLangFileInterface [ ] => {
187211 data = mergeData ( data )
0 commit comments