diff --git a/src/filters/dates.rs b/src/filters/dates.rs index 73a5cfc..6ad32b7 100644 --- a/src/filters/dates.rs +++ b/src/filters/dates.rs @@ -477,14 +477,14 @@ fn render_program(items: &[Piece], f: &Fields) -> String { /// no key allocation on a hit. struct Cache { lang: String, - i18n: bool, + real: u8, programs: HashMap>, } thread_local! { static CACHE: RefCell = RefCell::new(Cache { lang: String::new(), - i18n: false, + real: 0, programs: HashMap::new(), }); } @@ -582,13 +582,13 @@ fn run_program(py: Python<'_>, fmt: &str, fields: &Fields) -> PyResult(py: Python<'_>, e: &PyErr) -> bool { #[derive(Clone, PartialEq, Eq)] pub(crate) struct TransState { pub lang: String, - pub i18n: bool, + /// Bit 0: `gettext` resolved to `trans_real`; bit 1: `pgettext` did. + /// `_trans` resolves each function independently (on first use, by the + /// `USE_I18N` of that moment), so they can disagree after + /// `override_settings(USE_I18N=...)`; month/weekday tables mix both. + pub real: u8, } /// Live `(language, USE_I18N)` snapshot. @@ -184,14 +188,16 @@ pub(crate) struct TransState { /// covers `trans_null`, which ignores `_active` entirely; see `trans_impl`). /// Any unexpected shape → `None`; callers then fall back to the Python path. pub(crate) fn trans_state(py: Python<'_>) -> Option { - let i18n = match trans_impl(py, "gettext")? { - TransImpl::Real => true, - TransImpl::Null => false, - }; - if !i18n { + let mut real = 0u8; + for (bit, name) in [(1u8, "gettext"), (2u8, "pgettext")] { + if let TransImpl::Real = trans_impl(py, name)? { + real |= bit; + } + } + if real == 0 { return Some(TransState { lang: String::new(), - i18n: false, + real, }); } // The catalog `trans_real.gettext` uses: the active translation, else @@ -204,7 +210,7 @@ pub(crate) fn trans_state(py: Python<'_>) -> Option { CvarLang::Unset => setting(py, "LANGUAGE_CODE")?.extract().ok()?, CvarLang::Unknown => return None, }; - Some(TransState { lang, i18n: true }) + Some(TransState { lang, real }) } /// Live `settings.`. diff --git a/tests/test_realpage.py b/tests/test_realpage.py index 2a1e194..6501681 100644 --- a/tests/test_realpage.py +++ b/tests/test_realpage.py @@ -204,3 +204,23 @@ def test_dates_after_deactivate_all(): with translation.override(None, deactivate=True): translation.deactivate_all() assert_parity(src, {"d": AWARE}) + + +def test_dates_mixed_trans_resolution(): + """`_trans` resolves each function once, by `USE_I18N` at first use, and + never re-resolves. With `gettext` pinned to `trans_null` but `pgettext` + still unresolved, a later `USE_I18N=True` run mixes both (`N` uses + `pgettext_lazy`); cached programs must not leak across languages.""" + from django.utils.translation import _trans + + saved = dict(_trans.__dict__) + try: + _trans.__dict__.clear() + with override_settings(USE_I18N=False): + translation.gettext("x") # pins gettext to trans_null + with override_settings(USE_I18N=True): + for lang in (None, "de", "ja", "de"): + assert_parity('{{ d|date:"N F" }}', {"d": AWARE}, lang=lang) + finally: + _trans.__dict__.clear() + _trans.__dict__.update(saved)