Skip to content

Commit 1dad2e6

Browse files
committed
bug #1809 [Translator] Create valid constant names for keys starting with numeric chars (wolfgangweintritt)
This PR was merged into the 2.x branch. Discussion ---------- [Translator] Create valid constant names for keys starting with numeric chars If the key of a translation started with a numeric character, the constant's name would also start with a numeric character, thus resulting in a JS syntax error. Prefixing the constant's name with an underscore makes it valid. | Q | A | ------------- | --- | Bug fix? | yes | New feature? | no | Issues | - | License | MIT Commits ------- 440b5ab [Translator] Create valid constant names for keys starting with numeric chars
2 parents 3956b48 + 440b5ab commit 1dad2e6

File tree

2 files changed

+5
-1
lines changed

2 files changed

+5
-1
lines changed

src/Translator/src/TranslationsDumper.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -166,7 +166,7 @@ private function generateConstantName(string $translationId): string
166166

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

src/Translator/tests/TranslationsDumperTest.php

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -67,6 +67,7 @@ public function testDump(): void
6767
'what.count.4' => 'one: There is one %what%|more: There are more than one %what%',
6868
'animal.dog-cat' => 'Dog and cat',
6969
'animal.dog_cat' => 'Dog and cat (should not conflict with the previous one)',
70+
'0starts.with.numeric' => 'Key starts with numeric char',
7071
],
7172
'foobar' => [
7273
'post.num_comments' => 'There is 1 comment|There are %count% comments',
@@ -95,6 +96,7 @@ public function testDump(): void
9596
'what.count.4' => 'one: Il y a une %what%|more: Il y a more than one %what%',
9697
'animal.dog-cat' => 'Chien et chat',
9798
'animal.dog_cat' => 'Chien et chat (ne doit pas rentrer en conflit avec la traduction précédente)',
99+
'0starts.with.numeric' => 'La touche commence par un caractère numérique',
98100
],
99101
'foobar' => [
100102
'post.num_comments' => 'Il y a 1 comment|Il y a %count% comments',
@@ -125,6 +127,7 @@ public function testDump(): void
125127
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%"}}};
126128
export const ANIMAL_DOG_CAT = {"id":"animal.dog-cat","translations":{"messages":{"en":"Dog and cat","fr":"Chien et chat"}}};
127129
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)"}}};
130+
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"}}};
128131

129132
JAVASCRIPT);
130133

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

154158
TYPESCRIPT);
155159
}

0 commit comments

Comments
 (0)