-
Notifications
You must be signed in to change notification settings - Fork 382
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
feat: collect and print placeholders in Po files for better translation context #1965
feat: collect and print placeholders in Po files for better translation context #1965
Conversation
The latest updates on your projects. Learn more about Vercel for Git ↗︎
|
size-limit report 📦
|
0f87485
to
39d8f1a
Compare
Nice, I really like this feature, thanks! What about renaming the Also, can we have a test case for the strings with more than one placeholder? |
No problem, can do. I'm also thinking about the case where few messages in the codebase with diffrent placeholders. Currently, it will take the only last or first (i didn't test) but probably it makes sense to gather an array of them and print it? t`Hello ${user.name}`
t`Hello ${author.name}`
t`Hello ${moderator.name}` #. ph: {0} = user.name | author.name | moderator.name
msgid "Hello {0}"
Btw the next improvement could be a flag which will completely opt-out from inline placeholder name populated from varaible names in favor of this feature for maximum translation reusability. |
The following For a few placeholders per string, we can do something like this: #. placeholder {0}: user.name
#. placeholder {1}: user.login |
i don't really like "|" symbol, because the same symbol could be in the placeholder value itself. So it should more clear way to distinguish them. t`Hello ${name || surname}`
t`Hello ${userName}` #. ph: {0} = name || surname | userName
msgid "Hello {0}" Also with line breaks with ternary: t`Hello ${user
? user.name
: 'User' }`
t`Hello ${userName}` It seems clearing line breaks is unavoidable, because Po format is not allowing multiline comments #. placeholder: {0} = user ? user.name : 'User' | userName
msgid "Hello {0}" Maybe every entry on a new line? #. ph: {0} = name || surname
#. ph: {0} = userName
#. ph: {1} = user.login
#. ph: {1} = user Verbose but for 100% is clear where one placeholder value starts and ends. |
Codecov ReportAttention: Patch coverage is
Additional details and impacted files@@ Coverage Diff @@
## next #1965 +/- ##
==========================================
+ Coverage 76.66% 77.26% +0.59%
==========================================
Files 81 77 -4
Lines 2083 2208 +125
Branches 532 579 +47
==========================================
+ Hits 1597 1706 +109
- Misses 375 384 +9
- Partials 111 118 +7 ☔ View full report in Codecov by Sentry. |
I agree, the |
I'm also thinking that to give an overall context it's enough to show 2 - 3 placeholder values, so we can limit it to avoid bloating the file. From the other side, that could be a tool to quickly spot an issue with a message: t`from ${myTrip.date}`
t`from ${myTrip.startCity}` #. placeholder {0}: myTrip.date
#. placeholder {0}: myTrip.startCity <-- probably it should be different message created by different context
msgid "from {0}" |
Yes, I think limiting the output would also be ok. As for spotting the messages that should probably be used in different contexts, that would also be a nice feature. This is a common i18n problem. I think it could be presented in the CLI extract output, or it could be a completely separate CLI command, e.g. |
@andrii-bodnar this one is ready |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thank you!
Description
This feature serves similar purpose as in this PR #1874.
If the message contains unnamed placeholders, such as
{0}
print theirs values into PO comments, so translators got an idea what this placeholder is about.This will be extracted as
Before:
After:
This benefit of this solution is that developers can reuse more translations, because different placeholder name will not create different translations keys.
Also now placeholder metadata is available for formatters, so users can implement theirs own logic for placeholders (for example replace them to values)
Types of changes
Fixes # (issue)
Checklist