File tree Expand file tree Collapse file tree 3 files changed +31
-5
lines changed
Expand file tree Collapse file tree 3 files changed +31
-5
lines changed Original file line number Diff line number Diff line change @@ -3,7 +3,13 @@ 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 ( ) )
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+ }
713
814export const hasPhpTranslations = ( folderPath : string ) : boolean => {
915 folderPath = folderPath . replace ( / [ \\ / ] $ / , '' ) + path . sep
@@ -130,10 +136,12 @@ const parseItem = (expr) => {
130136 let key = expr . key . value
131137
132138 if ( expr . key . kind === 'staticlookup' ) {
133- key = toCamelCase ( expr . key . what . name )
134- }
135-
136- if ( expr . key . kind === 'propertylookup' ) {
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' ) {
137145 key = toCamelCase ( expr . key . what . offset . name )
138146 }
139147
Original file line number Diff line number Diff line change 1+ <?php
2+
3+ class SomeClass
4+ {
5+ const NAME = 'name ' ;
6+ }
7+
8+ return [
9+ SomeClass::class => 'Some Class ' ,
10+ SomeClass::NAME => 'Name ' ,
11+ ];
Original file line number Diff line number Diff line change @@ -134,6 +134,13 @@ it('transforms enum values to .json', () => {
134134 expect ( lang [ 'status.finished' ] ) . toBe ( 'Finished' ) ;
135135} ) ;
136136
137+ it ( 'transforms class names and consts to .json' , ( ) => {
138+ const lang = parse ( fs . readFileSync ( isolatedFixtures + '/lang/en/classnames.php' ) . toString ( ) ) ;
139+
140+ expect ( lang [ 'someClass' ] ) . toBe ( 'Some Class' ) ;
141+ expect ( lang [ 'name' ] ) . toBe ( 'Name' ) ;
142+ } ) ;
143+
137144it ( 'ignores empty `array` or `null` translations' , ( ) => {
138145 const lang = parse ( fs . readFileSync ( isolatedFixtures + '/lang/en/ignore.php' ) . toString ( ) ) ;
139146
You can’t perform that action at this time.
0 commit comments