ditch fe jinja2 filter and use Jinja2 newstyle gettext instead

RESOLVED FIXED

Status

Mozilla Developer Network
Code Cleanup
RESOLVED FIXED
2 years ago
2 years ago

People

(Reporter: robhudson, Assigned: robhudson)

Tracking

Details

(Assignee)

Description

2 years ago
+++ This bug was initially created as a clone of Bug #1222040 +++

Fjord has an fe (format escaped) filter that we inherited from Tower that allows us to do the following in templates:

    {{ _('Response: {id}')|fe(id=response.id) }}


In this expression, gettext is called with the msgid "Response: {id}" and it returns a translated form of that string. The resulting string is marked as Jinja2-safe by Fjord's adjusted gettext, so nothing is escaped in this string.

Then the string is passed to the fe filter which evaluates response.id in the context, escapes that value and then interpolates it into the string using Python's .format().

Yay! This is what we want!

However, there are two problems.

1. fe requires that we use python-brace-format (e.g. {}) variables which
   we want to get rid of (bug #1220657)

2. Jinja2's newstyle gettext already does this:

   {{ _('Response: %(id)s', id=response.id) }}


Here's a more complex example.

With fe:

   <h2>{{ _('<b>Response id: {id}</b>')|fe(id='<i>102</i>') }}</h2>

This comes out as:

   <b>Response id: &lt;i&gt;102&lt;/i&gt;</b>

where the <b> </b> is not escaped and the <i> </i> are.

The equivalent with newstyle gettext:

   <h2>{{ _('<b>Response id: %(id)s</b>', id='<i>102</i>') }}</h2>

produces the same thing.


This bug covers getting rid of our fe filter in favor of Jinja2's newstyle gettext.
(Assignee)

Updated

2 years ago
Assignee: nobody → robhudson
No longer blocks: 1220657
No longer depends on: 1222040, 1218381
(Assignee)

Updated

2 years ago
Blocks: 1209185

Comment 1

2 years ago
Commits pushed to master at https://github.com/mozilla/kuma

https://github.com/mozilla/kuma/commit/a9c7acd289f8d8d0b0e6702e473b1fae94ba852a
Fix bug 1225161 - Use newstyle gettext in favor of |f and |fe

https://github.com/mozilla/kuma/commit/4838c29847fa6f58935ceefb2a23a641df4e0ad2
Merge pull request #3654 from robhudson/1225161-no-jingo-filters

Fix bug 1225161 - Use newstyle gettext in favor of |f and |fe

Updated

2 years ago
Status: NEW → RESOLVED
Last Resolved: 2 years ago
Resolution: --- → FIXED

Comment 2

2 years ago
Commit pushed to master at https://github.com/mozilla/kuma

https://github.com/mozilla/kuma/commit/d571998b78c444ed73d93bf294f66bc768bd2066
Bug 1225161 - Missed a few uses of "|f"
You need to log in before you can comment on or make changes to this bug.