Skip to content

Commit

Permalink
fix: Add support for classes.
Browse files Browse the repository at this point in the history
  • Loading branch information
xiCO2k committed Oct 25, 2024
1 parent 269f004 commit 9d042d2
Show file tree
Hide file tree
Showing 3 changed files with 31 additions and 5 deletions.
18 changes: 13 additions & 5 deletions src/loader.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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)
}

Expand Down
11 changes: 11 additions & 0 deletions test/fixtures/lang/en/classnames.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
<?php

class SomeClass
{
const NAME = 'name';
}

return [
SomeClass::class => 'Some Class',
SomeClass::NAME => 'Name',
];
7 changes: 7 additions & 0 deletions test/loader.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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());

Expand Down

0 comments on commit 9d042d2

Please sign in to comment.