Skip to content

Commit

Permalink
Clear gettext cache on locale switch
Browse files Browse the repository at this point in the history
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*
  • Loading branch information
faho committed Mar 5, 2024
1 parent 965c407 commit f4cbe7a
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 2 deletions.
1 change: 1 addition & 0 deletions src/env_dispatch.rs
Expand Up @@ -800,6 +800,7 @@ fn init_locale(vars: &EnvStack) {
_nl_msg_cat_cntr += 1;
}
}
crate::wutil::gettext::wgettext_clear_cache();
}
}

Expand Down
9 changes: 7 additions & 2 deletions src/wutil/gettext.rs
Expand Up @@ -64,6 +64,13 @@ enum MaybeStatic<'a> {
Local(&'a wstr),
}

static WGETTEXT_MAP: Lazy<Mutex<HashMap<&'static wstr, &'static wstr>>> =
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
Expand All @@ -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<Mutex<HashMap<&'static wstr, &'static wstr>>> =
Lazy::new(|| Mutex::new(HashMap::new()));
let mut wmap = WGETTEXT_MAP.lock().unwrap();
let res = match wmap.get(key) {
Some(v) => *v,
Expand Down

0 comments on commit f4cbe7a

Please sign in to comment.