[IMP] tools: allow to use Markup and gettext
Before this commit escape was needed to use a Markup object as a
parameter, hence loosing the fallback mechanism in translations
>>> escape(_("Order %s has been confirmed")) % Markup("<a>%s</a>") % order.name
Markup("Order <a>SO42</a> has been confirmed")
Now it is possible to explictly give a Markup object to the gettext call
>>> _("Order %s has been confirmed", Markup("<a>%s</a>") % order.name)
Markup("Order <a>SO42</a> has been confirmed")
Part-of: odoo/odoo#139316
This commit is contained in:
@@ -2,6 +2,7 @@
|
||||
|
||||
import base64
|
||||
import io
|
||||
from markupsafe import Markup
|
||||
|
||||
from odoo.tests import common, tagged
|
||||
from odoo.tools.misc import file_open, mute_logger
|
||||
@@ -219,6 +220,13 @@ class TestImport(common.TransactionCase):
|
||||
with self.assertRaises(KeyError):
|
||||
model_fr_BE.get_code_named_placeholder_translation(symbol="🧀"),
|
||||
|
||||
# correctly translate markup
|
||||
self.assertEqual(
|
||||
model_fr_BE.get_code_named_placeholder_translation(num=Markup(2), symbol="<🧀>"),
|
||||
Markup("Code, 2, <🧀>, Français, Belgium"),
|
||||
"Translation placeholders were not applied when using Markup"
|
||||
)
|
||||
|
||||
|
||||
@tagged('post_install', '-at_install')
|
||||
class TestTranslationFlow(common.TransactionCase):
|
||||
|
||||
@@ -24,6 +24,7 @@ from os.path import join
|
||||
from pathlib import Path
|
||||
from babel.messages import extract
|
||||
from lxml import etree, html
|
||||
from markupsafe import escape, Markup
|
||||
from psycopg2.extras import Json
|
||||
|
||||
import odoo
|
||||
@@ -460,6 +461,8 @@ class GettextAlias(object):
|
||||
translation = self._get_translation(source)
|
||||
assert not (args and kwargs)
|
||||
if args or kwargs:
|
||||
if any(isinstance(a, Markup) for a in itertools.chain(args, kwargs.values())):
|
||||
translation = escape(translation)
|
||||
try:
|
||||
return translation % (args or kwargs)
|
||||
except (TypeError, ValueError, KeyError):
|
||||
|
||||
Reference in New Issue
Block a user