From f4cbe7a80bdc4af7b7e489bf6bfaf7979b00ebdb Mon Sep 17 00:00:00 2001 From: Fabian Boehm Date: Tue, 5 Mar 2024 17:28:21 +0100 Subject: [PATCH] Clear gettext cache on locale switch This fixes the issue where if you do `_ file` (which prints "file" in english) and then switch locale to e.g. de_DE.UTF-8 and do `_ file` again, it will still print "file" instead of the german "Datei". CACHE INVALIDATION!!!111eleven!!! *shakes fist* --- src/env_dispatch.rs | 1 + src/wutil/gettext.rs | 9 +++++++-- 2 files changed, 8 insertions(+), 2 deletions(-) diff --git a/src/env_dispatch.rs b/src/env_dispatch.rs index e9bfd550c190..8edb25829174 100644 --- a/src/env_dispatch.rs +++ b/src/env_dispatch.rs @@ -800,6 +800,7 @@ fn init_locale(vars: &EnvStack) { _nl_msg_cat_cntr += 1; } } + crate::wutil::gettext::wgettext_clear_cache(); } } diff --git a/src/wutil/gettext.rs b/src/wutil/gettext.rs index b537ee42fac5..70c2158683fc 100644 --- a/src/wutil/gettext.rs +++ b/src/wutil/gettext.rs @@ -64,6 +64,13 @@ enum MaybeStatic<'a> { Local(&'a wstr), } +static WGETTEXT_MAP: Lazy>> = + Lazy::new(|| Mutex::new(HashMap::new())); + +pub fn wgettext_clear_cache() { + WGETTEXT_MAP.lock().unwrap().clear(); +} + /// Implementation detail for wgettext!. /// Wide character wrapper around the gettext function. For historic reasons, unlike the real /// gettext function, wgettext takes care of setting the correct domain, etc. using the textdomain @@ -86,8 +93,6 @@ fn wgettext_impl(text: MaybeStatic) -> &'static wstr { ); // Note that because entries are immortal, we simply leak non-static keys, and all values. - static WGETTEXT_MAP: Lazy>> = - Lazy::new(|| Mutex::new(HashMap::new())); let mut wmap = WGETTEXT_MAP.lock().unwrap(); let res = match wmap.get(key) { Some(v) => *v,