-
-
Notifications
You must be signed in to change notification settings - Fork 356
[Translator] Message key syntax and object const for javascript UX Translator #2619
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
Comments
Thanks for your suggestion, I understand having different cases for the same translation key is a problem, it makes code updates or refactoring harder than it should be. However, translations keys were generated like that ( Before implementing something like About |
Interesting, I'm doing some refactoring in the direction I proposed and see what I end up with, so the context of tree-shaking and bundling is very useful to inform what I'm doing. |
I just noticed the generated translation objects actually have an "id" attribute with the key: export const OTP_EXCEPTIONS_USERNAME_NOT_FOUND = {"id":"otp.exceptions.username_not_found","translations":{"messages+intl-icu":{"en":"...","fr":"..."}}}; |
Yes, it is used when the message were not able to be translated https://github.com/symfony/ux/blob/2.x/src/Translator/assets/src/translator.ts#L136-L138 & https://github.com/symfony/ux/blob/2.x/src/Translator/assets/src/translator.ts#L174 But we can't use it to find the message, since the Translator is not aware about all messages. |
I am delegating translations to the front-end by sending the message through REST API requests to a React component.
The problem I'm facing is that I am forced to create a decoder ring that seems like boilerplate code: and pretty tedious:
That decoder ring is
const messages
.While reading the documentation, I noticed two things:
https://symfony.com/doc/current/translation.html#using-real-or-keyword-messages
The first is that I could refer to this translation in the backend as "otp.exceptions.username_not_found".
The second is that, I wrote the translations in YAML... had I written it in PHP I could send the whole translation file as a JSON object (not efficent obviously.
While importing consts from the translator is usable, it is also an error prone process, and more than once I've found myself using a const that I didn't import.
The feature request is to either or both be able to:
trans()
with the key notation and not having to import consts:Why I think this is important:
I could be switching around the text and even the variables inside the text (I'm using the ICU format) and as long as the message id / structure remains the same, the tests don't break
One of the goals of Symfony UX Translator (I assume) is to reuse translations in the front-end to reduce redundant work. The object const notation not only would make it look consistent with the key notation in PHP, but also reduce cognitive load and boilerplate code (multiple imports).
Boilerplate code not only makes the task longer, boring, but is also error prone.
Having the key notation available in Javascript would mean that one wouldn't even have to import the translation consts (could be imported in the background). This would make it super simple to understand the process, help improve API consistency when the translations are being delegated by the backend and make the whole process less error proof.
There is a term in the manufacturing world in quality management systems for "error-proofing" called poka-yoke. Error-proofing would essentially translate to Developer Experience :)
In summary
This feature would lead to more consistent, less error prone, less boilerplate and less cognitive load while working with front-end translation, and also make it very easy to delegate translations from the backend while making enabling more anti-fragile API testing.
I think this feature can be added without compatibility issues by enabling something like this for the key notation:
And for the const objects, just generating that code in addition to the uppercase consts:
The text was updated successfully, but these errors were encountered: