translation-fstring-interpolation (ODW8303)
Preview (since 0.16.2.9) · Related issues · View source
Derived from the odoo linter.
Fix is sometimes available.
This rule is unstable and in preview. The --preview flag is required for use.
What it does
Checks for translation calls whose term is an f-string, e.g. _(f"Hello {name}").
The rule only applies from Odoo 14.0 on, the version whose translation functions take
the interpolation arguments themselves; up to 13.0 translation-contains-variable
covers the eager interpolation instead. Configure the version with the odoo-version
setting; without it the rule stays enabled.
Why is this bad?
An f-string interpolates its values before the translation call runs, so the
looked-up term contains the runtime values and never matches the exported
translation entry. Since Odoo 14.0 the translation functions (_, self.env._)
interpolate the values themselves: _("Hello %s", name).
Example
Use instead:
Fix safety
A fix is offered when the f-string is the call's only argument and every interpolation
is a plain expression — no conversion flag (!r), format spec, or = debug form — and
no literal piece spells a character through an escape sequence: each interpolation
becomes %s and its expression moves into the translation call. The fix is marked
unsafe because the term the translation machinery looks up changes, so the exported
translation entries have to be regenerated.