OCA / pylint-odoo

Odoo plugin for Pylint
http://www.pylint.org
143 stars 168 forks source link

New check: translated strings containing unordered placeholders #303

Closed yajo closed 3 years ago

yajo commented 3 years ago

As one can see in https://github.com/odoo/odoo/issues/29104 (not related to this proposal, but gives an example), there are some sentences which are hard to translate from English to other languages if you can't alter the order of words.

So, my proposal is that we add a linter that ensures that, when a translated string has more than 1 interpolation, it must be expanded with a dict and not a tuple.

Example:

_("Your %s %s is shipping") % ("red", "car")

It seems nice in English, but in Spanish you would have to use an unnatural sentence such as "Su envío, que es de color %s y es un/a %s, está de camino".

The original code should have been:

_("Your %(color)s %(item)s is shipping") % {"color": "red", "item": "car"}

So you can translate that into "Su %(item)s %(color)s está enviándose" in Spanish.

If there's just one %s or the string is not translatable, this is not an issue.

FernandoRomera commented 3 years ago

IIUC v14.0 supports passing arguments to translation lookup. https://www.odoo.com/documentation/14.0/reference/translations.html#variables

before 14 _("Your %s %s is shipping") % ("red", "car")

now is _("Your %s %s is shipping", "red", "car")

pedrobaeza commented 3 years ago

But the order question remains the same.

FernandoRomera commented 3 years ago

My note is for the PR #305, if it can adjust positional string formatting or passing arguments?

Test ok if: _("Your %(color)s %(product)s is shipping", color="red", product="car")

and ko _("Your %s %s is shipping", "red", "car")

yajo commented 3 years ago

Good, I didn't know of that new feature. Indeed we should fail if it's called with more than 1 *args and ask to use **kwargs instead.