{# ========================================================================= Translation helpers for the package's own templates. Package templates must render byte-identically in an app that never calls coffeehouse_ui.i18n.install (a plain Jinja environment, where {% trans %} does not parse and `_` is undefined). So they never use `_` or trans; they use these macros: {% from "macros/i18n.html" import ch_t, ch_nt %} {{ ch_t("Logout") }} {{ ch_t("from dept: %(dept)s", dept=slug) }} {{ ch_nt("%(num)d role", "%(num)d roles", n) }} - Without install (`ch_i18n` undefined) a macro outputs the English msgid. - With install, `ch_i18n` (coffeehouse_ui.i18n.TemplateBridge) translates for the request's locale and returns a plain str. It is NOT newstyle `_()`, which returns Markup: the translation would skip autoescape, and an `&` in a string would render differently from the literal it replaced. - Either way the message is escaped like the old literal, so a msgid must not contain markup or an entity; keep `→`, `×`, tags outside. Named placeholders are filled after escaping: a plain value is escaped, a Markup value (a `{% set %}` block holding a link) is inserted as is. - ch_data emits ` name="..."` only when the current locale translates the msgid, so markup at `en` is unchanged; the script keeps English inside. Babel extracts them through the keywords in babel.cfg (ch_t ch_nt:1,2 ch_data:2). ========================================================================= #} {%- macro ch_t(msgid) -%} {%- set _s = ch_i18n.gettext(msgid) if ch_i18n is defined else msgid -%} {{ (_s | e) % kwargs if kwargs else _s }} {%- endmacro %} {%- macro ch_nt(singular, plural, n) -%} {%- set _s = ch_i18n.ngettext(singular, plural, n) if ch_i18n is defined else (singular if n == 1 else plural) -%} {{ (_s | e) % dict(kwargs, num=n) }} {%- endmacro %} {%- macro ch_data(name, msgid) -%} {%- if ch_i18n is defined and ch_i18n.translates(msgid) %} {{ name }}="{{ ch_i18n.gettext(msgid) }}"{% endif -%} {%- endmacro %}