diff --git a/src/loader.ts b/src/loader.ts index fe16a10..4e4bd06 100644 --- a/src/loader.ts +++ b/src/loader.ts @@ -3,7 +3,13 @@ import path from 'path' import { Engine } from 'php-parser' import { ParsedLangFileInterface } from './interfaces/parsed-lang-file' -const toCamelCase = (str: string): string => str.replace(/^\w/, (c) => c.toLowerCase()) +const toCamelCase = (str: string): string => { + if (str === str.toUpperCase()) { + return str.toLowerCase() + } + + return str.replace(/^\w/, (c) => c.toLowerCase()) +} export const hasPhpTranslations = (folderPath: string): boolean => { folderPath = folderPath.replace(/[\\/]$/, '') + path.sep @@ -130,10 +136,12 @@ const parseItem = (expr) => { let key = expr.key.value if (expr.key.kind === 'staticlookup') { - key = toCamelCase(expr.key.what.name) - } - - if (expr.key.kind === 'propertylookup') { + if (expr.key.offset.name === 'class') { + key = toCamelCase(expr.key.what.name) + } else { + key = toCamelCase(expr.key.offset.name) + } + } else if (expr.key.kind === 'propertylookup') { key = toCamelCase(expr.key.what.offset.name) } diff --git a/test/fixtures/lang/en/classnames.php b/test/fixtures/lang/en/classnames.php new file mode 100644 index 0000000..fdb373d --- /dev/null +++ b/test/fixtures/lang/en/classnames.php @@ -0,0 +1,11 @@ + 'Some Class', + SomeClass::NAME => 'Name', +]; diff --git a/test/loader.test.ts b/test/loader.test.ts index 6945457..38cf1fe 100644 --- a/test/loader.test.ts +++ b/test/loader.test.ts @@ -134,6 +134,13 @@ it('transforms enum values to .json', () => { expect(lang['status.finished']).toBe('Finished'); }); +it('transforms class names and consts to .json', () => { + const lang = parse(fs.readFileSync(isolatedFixtures + '/lang/en/classnames.php').toString()); + + expect(lang['someClass']).toBe('Some Class'); + expect(lang['name']).toBe('Name'); +}); + it('ignores empty `array` or `null` translations', () => { const lang = parse(fs.readFileSync(isolatedFixtures + '/lang/en/ignore.php').toString());