@@ -3,6 +3,14 @@ import path from 'path'
3
3
import { Engine } from 'php-parser'
4
4
import { ParsedLangFileInterface } from './interfaces/parsed-lang-file'
5
5
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
+
6
14
export const hasPhpTranslations = ( folderPath : string ) : boolean => {
7
15
folderPath = folderPath . replace ( / [ \\ / ] $ / , '' ) + path . sep
8
16
@@ -51,14 +59,16 @@ export const parseAll = (folderPath: string): ParsedLangFileInterface[] => {
51
59
}
52
60
53
61
// 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' )
55
63
56
64
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 )
59
67
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
+ )
62
72
}
63
73
64
74
return data
@@ -75,16 +85,18 @@ export const parseAll = (folderPath: string): ParsedLangFileInterface[] => {
75
85
76
86
function mergeVendorTranslations ( folder : string , translations : any , vendorTranslations : any ) {
77
87
// 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 )
80
89
. 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
+ )
85
97
86
98
// Merge the vendor translations that matched the folder with the current translations
87
- return { ...translations , ...langTranslationsFromVendor } ;
99
+ return { ...translations , ...langTranslationsFromVendor }
88
100
}
89
101
90
102
export const parse = ( content : string ) => {
@@ -121,7 +133,19 @@ const parseItem = (expr) => {
121
133
}
122
134
123
135
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 ) }
125
149
}
126
150
127
151
return parseItem ( expr . value )
@@ -181,7 +205,7 @@ export const readThroughDir = (dir) => {
181
205
}
182
206
183
207
export const prepareExtendedParsedLangFiles = ( langPaths : string [ ] ) : ParsedLangFileInterface [ ] =>
184
- langPaths . flatMap ( langPath => parseAll ( langPath ) ) ;
208
+ langPaths . flatMap ( ( langPath ) => parseAll ( langPath ) )
185
209
186
210
export const generateFiles = ( langPath : string , data : ParsedLangFileInterface [ ] ) : ParsedLangFileInterface [ ] => {
187
211
data = mergeData ( data )
0 commit comments