Skip to content
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

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

Merged
merged 1 commit into from
May 1, 2024

Conversation

wolfgangweintritt
Copy link
Contributor

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

@carsonbot carsonbot added Bug Bug Fix Status: Needs Review Needs to be reviewed labels Apr 30, 2024
@kbond
Copy link
Member

kbond commented Apr 30, 2024

Thanks for this fix @wolfgangweintritt!

@Kocal, mind taking a look at this?

@@ -166,7 +166,7 @@ private function generateConstantName(string $translationId): string

$prefix = 0;
do {
$constantName = s($translationId)->ascii()->snake()->upper()->toString().($prefix > 0 ? '_'.$prefix : '');
$constantName = preg_replace('/^(\d)/', '_$1', s($translationId)->ascii()->snake()->upper()->toString().($prefix > 0 ? '_'.$prefix : ''));
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM, but I would be in favor to use ->replaceMatches from s() instead of calling preg_replace directly

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for the review! Yeah I agree - I've changed it.

@carsonbot carsonbot added Status: Reviewed Has been reviewed by a maintainer and removed Status: Needs Review Needs to be reviewed labels Apr 30, 2024
…ic 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.
@kbond
Copy link
Member

kbond commented May 1, 2024

Thank you Wolfgang, great first PR!

@kbond kbond merged commit 1dad2e6 into symfony:2.x May 1, 2024
36 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Bug Bug Fix Status: Reviewed Has been reviewed by a maintainer
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

4 participants