Skip to content

[Translator] Create valid constant names for keys starting with numeric chars #1809

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
May 1, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion src/Translator/src/TranslationsDumper.php
Original file line number Diff line number Diff line change
Expand Up @@ -166,7 +166,7 @@ private function generateConstantName(string $translationId): string

$prefix = 0;
do {
$constantName = s($translationId)->ascii()->snake()->upper()->toString().($prefix > 0 ? '_'.$prefix : '');
$constantName = s($translationId)->ascii()->snake()->upper()->replaceMatches('/^(\d)/', '_$1')->toString().($prefix > 0 ? '_'.$prefix : '');
++$prefix;
} while (\in_array($constantName, $alreadyGenerated, true));

Expand Down
4 changes: 4 additions & 0 deletions src/Translator/tests/TranslationsDumperTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,7 @@ public function testDump(): void
'what.count.4' => 'one: There is one %what%|more: There are more than one %what%',
'animal.dog-cat' => 'Dog and cat',
'animal.dog_cat' => 'Dog and cat (should not conflict with the previous one)',
'0starts.with.numeric' => 'Key starts with numeric char',
],
'foobar' => [
'post.num_comments' => 'There is 1 comment|There are %count% comments',
Expand Down Expand Up @@ -95,6 +96,7 @@ public function testDump(): void
'what.count.4' => 'one: Il y a une %what%|more: Il y a more than one %what%',
'animal.dog-cat' => 'Chien et chat',
'animal.dog_cat' => 'Chien et chat (ne doit pas rentrer en conflit avec la traduction précédente)',
'0starts.with.numeric' => 'La touche commence par un caractère numérique',
],
'foobar' => [
'post.num_comments' => 'Il y a 1 comment|Il y a %count% comments',
Expand Down Expand Up @@ -125,6 +127,7 @@ public function testDump(): void
export const WHAT_COUNT4 = {"id":"what.count.4","translations":{"messages":{"en":"one: There is one %what%|more: There are more than one %what%","fr":"one: Il y a une %what%|more: Il y a more than one %what%"}}};
export const ANIMAL_DOG_CAT = {"id":"animal.dog-cat","translations":{"messages":{"en":"Dog and cat","fr":"Chien et chat"}}};
export const ANIMAL_DOG_CAT_1 = {"id":"animal.dog_cat","translations":{"messages":{"en":"Dog and cat (should not conflict with the previous one)","fr":"Chien et chat (ne doit pas rentrer en conflit avec la traduction pr\u00e9c\u00e9dente)"}}};
export const _0STARTS_WITH_NUMERIC = {"id":"0starts.with.numeric","translations":{"messages":{"en":"Key starts with numeric char","fr":"La touche commence par un caract\u00e8re num\u00e9rique"}}};

JAVASCRIPT);

Expand All @@ -150,6 +153,7 @@ public function testDump(): void
export declare const WHAT_COUNT4: Message<{ 'messages': { parameters: { '%what%': string } } }, 'en'|'fr'>;
export declare const ANIMAL_DOG_CAT: Message<{ 'messages': { parameters: NoParametersType } }, 'en'|'fr'>;
export declare const ANIMAL_DOG_CAT_1: Message<{ 'messages': { parameters: NoParametersType } }, 'en'|'fr'>;
export declare const _0STARTS_WITH_NUMERIC: Message<{ 'messages': { parameters: NoParametersType } }, 'en'|'fr'>;

TYPESCRIPT);
}
Expand Down