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

Add check to ensure translated string never modifies variables #7211

Closed
shasheene opened this issue Feb 6, 2022 · 5 comments
Closed

Add check to ensure translated string never modifies variables #7211

shasheene opened this issue Feb 6, 2022 · 5 comments

Comments

@shasheene
Copy link

shasheene commented Feb 6, 2022

Describe the problem

My open-source project receives a large number of translations thanks to Weblate.

The problem is my source strings contain variables using Python's format string syntax (enclosed by braces characters { and }), and even experienced translators sometimes accidentally translate the variable name.

Here's an example:

msgid "Successfully restored image partition {image} to {destination_partition}"
msgstr "Ripristinata con successo l'immagine della partizione {image} su {destinazione_partition}"

The destination_partition variable should not have been translated. The variable name needs to remain the same as the source string, otherwise in they'll be an error that may only be discovered at runtime when that variable is substituted:

>>> print("This is a {name}".format(name="test"))
This is a test

>>> print("This is a {name_wrong}".format(name="test"))
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
KeyError: 'name_wrong'

Until now I have been discovering these errors by manually examining the source code everytime I merge a new translation.

Describe the solution you'd like

Weblate should ideally have a "linter" to detect the mismatch based on some understanding of the variable delimiters.

The example I showed above is Python-specific, but other languages may suffer the same issue.

Describe alternatives you've considered

I have considered running a "linter" to try and detect this error in my build system. A cursory search reveals a linter that also doesn't detect this situation. As with most projects, my build system already runs msgfmt locally to "compile" the gettext source files into the .MO binary files. But the msgfmt "compilation" won't detect this error.

But I feel it's better to have this detection happen in Weblate itself, because then the translator can be informed of the error and fix it themselves.

Screenshots

No response

Additional context

No response

@shasheene shasheene changed the title Add check to ensure translated string correctly spells format variables Add check to ensure translated string never modifies variables Feb 6, 2022
@nijel
Copy link
Member

nijel commented Feb 6, 2022

There are already dozen of such checks, doesn't one of them fit your needs? https://docs.weblate.org/en/latest/user/checks.html#formatted-strings

@shasheene
Copy link
Author

Oh wow thanks! That should definitely be enough for what I need!!

I see now that another user was also unaware of the flag a few months ago: #6920

@github-actions
Copy link

github-actions bot commented Feb 6, 2022

The issue you have reported is now resolved. If you don’t feel it’s right, please follow its labels to get a clue for further steps.

  • In case you see a similar problem, please open a separate issue.
  • If you are happy with the outcome, don’t hesitate to support Weblate by making a donation.

@nijel
Copy link
Member

nijel commented Feb 7, 2022

With PO files and xgettext, these flags should be set automatically:

#: weblate/accounts/models.py:146
#, python-brace-format
msgid "Username changed from {old} to {new}."
msgstr "Uživatelské jméno změněno z {old} na {new}."

Maybe there is something wrong in the way you generate the PO files?

@shasheene
Copy link
Author

Probably. I do mistakenly use the 'ko' extension (not sure where I got that extension from), and my PO files have went through multiple iterations across different programming languages and so my templates have at this point largely been self-written rather than generated using xgettext.

Thanks for the insight! I appreciate Weblate a lot!

shasheene added a commit to shasheene/rescuezilla-dev that referenced this issue Mar 26, 2022
Adds a 'python-brace-format' line to every translation string that contains a
variable substitution. This gets added using xgettext, but Rescuezilla still
manually manages translation files right now.

This should allow Weblate to enforce that variables in translated strings
should not be translated, which cause runtime errors if missed.

[1] WeblateOrg/weblate#7211 (comment)
shasheene added a commit to rescuezilla/rescuezilla that referenced this issue Mar 26, 2022
Adds a 'python-brace-format' line to every translation string that contains a
variable substitution. This gets added using xgettext, but Rescuezilla still
manually manages translation files right now.

This should allow Weblate to enforce that variables in translated strings
should not be translated, which cause runtime errors if missed.

[1] WeblateOrg/weblate#7211 (comment)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants