Skip to content

Commit 7fe7ee2

Browse files
committed
bug #945 fix(translator): generate unique constants name (fix #938) (Kocal)
This PR was merged into the 2.x branch. Discussion ---------- fix(translator): generate unique constants name (fix #938) | Q | A | ------------- | --- | Bug fix? | yes | New feature? | no <!-- please update src/**/CHANGELOG.md files --> | Tickets | Fix #938 <!-- prefix each issue number with "Fix #", no need to create an issue if none exist, explain below instead --> | License | MIT <!-- Replace this notice by a short README for your feature/bugfix. This will help people understand your PR and can be used as a start for the documentation. Additionally (see https://symfony.com/releases): - Always add tests and ensure they pass. - Never break backward compatibility (see https://symfony.com/bc). - Features and deprecations must be submitted against branch main. --> Generating constants name with a automatic-prefix, in order to prevent conflicts when they are dumped in JS. Commits ------- 7f12e9c fix(translator): generate unique constants name (fix #938)
2 parents 979938e + 7f12e9c commit 7fe7ee2

File tree

2 files changed

+36
-1
lines changed

2 files changed

+36
-1
lines changed

src/Translator/src/TranslationsDumper.php

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,7 @@ public function dump(MessageCatalogueInterface ...$catalogues): void
5353
$translationsTs = "import { Message, NoParametersType } from '@symfony/ux-translator';\n\n";
5454

5555
foreach ($this->getTranslations(...$catalogues) as $translationId => $translationsByDomainAndLocale) {
56-
$constantName = s($translationId)->ascii()->snake()->upper()->toString();
56+
$constantName = $this->generateConstantName($translationId);
5757

5858
$translationsJs .= sprintf(
5959
"export const %s = %s;\n",
@@ -159,4 +159,19 @@ private function getLocaleFallbacks(MessageCatalogueInterface ...$catalogues): a
159159

160160
return $localesFallbacks;
161161
}
162+
163+
private function generateConstantName(string $translationId): string
164+
{
165+
static $alreadyGenerated = [];
166+
167+
$prefix = 0;
168+
do {
169+
$constantName = s($translationId)->ascii()->snake()->upper()->toString().($prefix > 0 ? '_'.$prefix : '');
170+
++$prefix;
171+
} while (\in_array($constantName, $alreadyGenerated, true));
172+
173+
$alreadyGenerated[] = $constantName;
174+
175+
return $constantName;
176+
}
162177
}

src/Translator/tests/TranslationsDumperTest.php

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,10 +40,13 @@ public function testDump(): void
4040
'notification.comment_created' => 'Your post received a comment!',
4141
'notification.comment_created.description' => 'Your post "{title}" has received a new comment. You can read the comment by following <a href="{link}">this link</a>',
4242
'post.num_comments' => '{count, plural, one {# comment} other {# comments}}',
43+
'post.num_comments.' => '{count, plural, one {# comment} other {# comments}} (should not conflict with the previous one.)',
4344
],
4445
'messages' => [
4546
'symfony.great' => 'Symfony is awesome!',
4647
'symfony.what' => 'Symfony is %what%!',
48+
'symfony.what!' => 'Symfony is %what%! (should not conflict with the previous one.)',
49+
'symfony.what.' => 'Symfony is %what%. (should also not conflict with the previous one.)',
4750
'apples.count.0' => 'There is 1 apple|There are %count% apples',
4851
'apples.count.1' => '{1} There is one apple|]1,Inf] There are %count% apples',
4952
'apples.count.2' => '{0} There are no apples|{1} There is one apple|]1,Inf] There are %count% apples',
@@ -53,6 +56,8 @@ public function testDump(): void
5356
'what.count.2' => '{0} There are no %what%|{1} There is one %what%|]1,Inf] There are %count% %what%',
5457
'what.count.3' => 'one: There is one %what%|more: There are %count% %what%',
5558
'what.count.4' => 'one: There is one %what%|more: There are more than one %what%',
59+
'animal.dog-cat' => 'Dog and cat',
60+
'animal.dog_cat' => 'Dog and cat (should not conflict with the previous one)',
5661
],
5762
'foobar' => [
5863
'post.num_comments' => 'There is 1 comment|There are %count% comments',
@@ -63,10 +68,13 @@ public function testDump(): void
6368
'notification.comment_created' => 'Votre article a reçu un commentaire !',
6469
'notification.comment_created.description' => 'Votre article "{title}" a reçu un nouveau commentaire. Vous pouvez lire le commentaire en suivant <a href="{link}">ce lien</a>',
6570
'post.num_comments' => '{count, plural, one {# commentaire} other {# commentaires}}',
71+
'post.num_comments.' => '{count, plural, one {# commentaire} other {# commentaires}} (ne doit pas rentrer en conflit avec la traduction précédente)',
6672
],
6773
'messages' => [
6874
'symfony.great' => 'Symfony est génial !',
6975
'symfony.what' => 'Symfony est %what%!',
76+
'symfony.what!' => 'Symfony est %what%! (ne doit pas rentrer en conflit avec la traduction précédente)',
77+
'symfony.what.' => 'Symfony est %what%. (ne doit pas non plus rentrer en conflit avec la traduction précédente)',
7078
'apples.count.0' => 'Il y a 1 pomme|Il y a %count% pommes',
7179
'apples.count.1' => '{1} Il y a une pomme|]1,Inf] Il y a %count% pommes',
7280
'apples.count.2' => '{0} Il n\'y a pas de pommes|{1} Il y a une pomme|]1,Inf] Il y a %count% pommes',
@@ -76,6 +84,8 @@ public function testDump(): void
7684
'what.count.2' => '{0} Il n\'y a pas de %what%|{1} Il y a une %what%|]1,Inf] Il y a %count% %what%',
7785
'what.count.3' => 'one: Il y a une %what%|more: Il y a %count% %what%',
7886
'what.count.4' => 'one: Il y a une %what%|more: Il y a more than one %what%',
87+
'animal.dog-cat' => 'Chien et chat',
88+
'animal.dog_cat' => 'Chien et chat (ne doit pas rentrer en conflit avec la traduction précédente)',
7989
],
8090
'foobar' => [
8191
'post.num_comments' => 'Il y a 1 comment|Il y a %count% comments',
@@ -90,8 +100,11 @@ public function testDump(): void
90100
export const NOTIFICATION_COMMENT_CREATED = {"id":"notification.comment_created","translations":{"messages+intl-icu":{"en":"Your post received a comment!","fr":"Votre article a re\u00e7u un commentaire !"}}};
91101
export const NOTIFICATION_COMMENT_CREATED_DESCRIPTION = {"id":"notification.comment_created.description","translations":{"messages+intl-icu":{"en":"Your post \"{title}\" has received a new comment. You can read the comment by following <a href=\"{link}\">this link<\/a>","fr":"Votre article \"{title}\" a re\u00e7u un nouveau commentaire. Vous pouvez lire le commentaire en suivant <a href=\"{link}\">ce lien<\/a>"}}};
92102
export const POST_NUM_COMMENTS = {"id":"post.num_comments","translations":{"messages+intl-icu":{"en":"{count, plural, one {# comment} other {# comments}}","fr":"{count, plural, one {# commentaire} other {# commentaires}}"},"foobar":{"en":"There is 1 comment|There are %count% comments","fr":"Il y a 1 comment|Il y a %count% comments"}}};
103+
export const POST_NUM_COMMENTS_1 = {"id":"post.num_comments.","translations":{"messages+intl-icu":{"en":"{count, plural, one {# comment} other {# comments}} (should not conflict with the previous one.)","fr":"{count, plural, one {# commentaire} other {# commentaires}} (ne doit pas rentrer en conflit avec la traduction pr\u00e9c\u00e9dente)"}}};
93104
export const SYMFONY_GREAT = {"id":"symfony.great","translations":{"messages":{"en":"Symfony is awesome!","fr":"Symfony est g\u00e9nial !"}}};
94105
export const SYMFONY_WHAT = {"id":"symfony.what","translations":{"messages":{"en":"Symfony is %what%!","fr":"Symfony est %what%!"}}};
106+
export const SYMFONY_WHAT_1 = {"id":"symfony.what!","translations":{"messages":{"en":"Symfony is %what%! (should not conflict with the previous one.)","fr":"Symfony est %what%! (ne doit pas rentrer en conflit avec la traduction pr\u00e9c\u00e9dente)"}}};
107+
export const SYMFONY_WHAT_2 = {"id":"symfony.what.","translations":{"messages":{"en":"Symfony is %what%. (should also not conflict with the previous one.)","fr":"Symfony est %what%. (ne doit pas non plus rentrer en conflit avec la traduction pr\u00e9c\u00e9dente)"}}};
95108
export const APPLES_COUNT0 = {"id":"apples.count.0","translations":{"messages":{"en":"There is 1 apple|There are %count% apples","fr":"Il y a 1 pomme|Il y a %count% pommes"}}};
96109
export const APPLES_COUNT1 = {"id":"apples.count.1","translations":{"messages":{"en":"{1} There is one apple|]1,Inf] There are %count% apples","fr":"{1} Il y a une pomme|]1,Inf] Il y a %count% pommes"}}};
97110
export const APPLES_COUNT2 = {"id":"apples.count.2","translations":{"messages":{"en":"{0} There are no apples|{1} There is one apple|]1,Inf] There are %count% apples","fr":"{0} Il n'y a pas de pommes|{1} Il y a une pomme|]1,Inf] Il y a %count% pommes"}}};
@@ -101,6 +114,8 @@ public function testDump(): void
101114
export const WHAT_COUNT2 = {"id":"what.count.2","translations":{"messages":{"en":"{0} There are no %what%|{1} There is one %what%|]1,Inf] There are %count% %what%","fr":"{0} Il n'y a pas de %what%|{1} Il y a une %what%|]1,Inf] Il y a %count% %what%"}}};
102115
export const WHAT_COUNT3 = {"id":"what.count.3","translations":{"messages":{"en":"one: There is one %what%|more: There are %count% %what%","fr":"one: Il y a une %what%|more: Il y a %count% %what%"}}};
103116
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%"}}};
117+
export const ANIMAL_DOG_CAT = {"id":"animal.dog-cat","translations":{"messages":{"en":"Dog and cat","fr":"Chien et chat"}}};
118+
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)"}}};
104119

105120
JAVASCRIPT);
106121

@@ -110,8 +125,11 @@ public function testDump(): void
110125
export declare const NOTIFICATION_COMMENT_CREATED: Message<{ 'messages+intl-icu': { parameters: NoParametersType } }, 'en'|'fr'>;
111126
export declare const NOTIFICATION_COMMENT_CREATED_DESCRIPTION: Message<{ 'messages+intl-icu': { parameters: { 'title': string, 'link': string } } }, 'en'|'fr'>;
112127
export declare const POST_NUM_COMMENTS: Message<{ 'messages+intl-icu': { parameters: { 'count': number } }, 'foobar': { parameters: { '%count%': number } } }, 'en'|'fr'>;
128+
export declare const POST_NUM_COMMENTS_1: Message<{ 'messages+intl-icu': { parameters: { 'count': number } } }, 'en'|'fr'>;
113129
export declare const SYMFONY_GREAT: Message<{ 'messages': { parameters: NoParametersType } }, 'en'|'fr'>;
114130
export declare const SYMFONY_WHAT: Message<{ 'messages': { parameters: { '%what%': string } } }, 'en'|'fr'>;
131+
export declare const SYMFONY_WHAT_1: Message<{ 'messages': { parameters: { '%what%': string } } }, 'en'|'fr'>;
132+
export declare const SYMFONY_WHAT_2: Message<{ 'messages': { parameters: { '%what%': string } } }, 'en'|'fr'>;
115133
export declare const APPLES_COUNT0: Message<{ 'messages': { parameters: { '%count%': number } } }, 'en'|'fr'>;
116134
export declare const APPLES_COUNT1: Message<{ 'messages': { parameters: { '%count%': number } } }, 'en'|'fr'>;
117135
export declare const APPLES_COUNT2: Message<{ 'messages': { parameters: { '%count%': number } } }, 'en'|'fr'>;
@@ -121,6 +139,8 @@ public function testDump(): void
121139
export declare const WHAT_COUNT2: Message<{ 'messages': { parameters: { '%what%': string, '%count%': number } } }, 'en'|'fr'>;
122140
export declare const WHAT_COUNT3: Message<{ 'messages': { parameters: { '%what%': string, '%count%': number } } }, 'en'|'fr'>;
123141
export declare const WHAT_COUNT4: Message<{ 'messages': { parameters: { '%what%': string } } }, 'en'|'fr'>;
142+
export declare const ANIMAL_DOG_CAT: Message<{ 'messages': { parameters: NoParametersType } }, 'en'|'fr'>;
143+
export declare const ANIMAL_DOG_CAT_1: Message<{ 'messages': { parameters: NoParametersType } }, 'en'|'fr'>;
124144

125145
TYPESCRIPT);
126146
}

0 commit comments

Comments
 (0)