mirror of
https://github.com/storytold/texturecraft.git
synced 2026-11-26 17:30:45 +01:00
Isolate TextureCraft UI language on each thread (#797)
Co-authored-by: ViktorBystrov72 <18964918+ViktorBystrov72@users.noreply.github.com>
This commit is contained in:
1 parent
d422c821f2
commit
1f78ba3578
1 file changed
+31
-7
@@ -1,8 +1,8 @@
|
||||
//! UI text lookup over the optional shared craft-i18n catalogs.
|
||||
//! Missing translations and builds without catalogs use the English source text.
|
||||
|
||||
use std::cell::Cell;
|
||||
use std::fmt::Display;
|
||||
use std::sync::atomic::{AtomicUsize, Ordering};
|
||||
use std::sync::{LazyLock, OnceLock};
|
||||
|
||||
include!(concat!(env!("OUT_DIR"), "/craft_i18n.rs"));
|
||||
@@ -52,7 +52,10 @@ pub static LANGUAGES: [Language; COUNT] = [
|
||||
Language { code: "ar", name: "العربية", english: "Arabic" },
|
||||
];
|
||||
|
||||
static CURRENT: AtomicUsize = AtomicUsize::new(0);
|
||||
// Rendering and preferences use the UI thread, including independent test harnesses.
|
||||
thread_local! {
|
||||
static CURRENT: Cell<usize> = const { Cell::new(0) };
|
||||
}
|
||||
|
||||
/// How a fill reads its images, as the Filtering combo names it
|
||||
pub(crate) fn filtering_label(filtering: texturecraft_engine::core::Filtering) -> &'static str {
|
||||
@@ -96,20 +99,20 @@ pub(crate) fn baked_map_label(map: texturecraft_engine::core::BakedMap) -> &'sta
|
||||
/// The saved language that follows the operating system's (Settings > Language > Default (System Language)).
|
||||
pub const SYSTEM_LANGUAGE: &str = "";
|
||||
|
||||
/// Switch the UI language ([`SYSTEM_LANGUAGE`] follows the system; unknown codes fall back to English).
|
||||
/// Switch the calling UI thread's language. [`SYSTEM_LANGUAGE`] follows the system.
|
||||
pub fn set_language(code: &str) {
|
||||
let code = if code == SYSTEM_LANGUAGE { system_language() } else { code };
|
||||
CURRENT.store(LANGUAGES.iter().position(|l| l.code.eq_ignore_ascii_case(code) && available(l.code)).unwrap_or(0), Ordering::Relaxed);
|
||||
CURRENT.set(LANGUAGES.iter().position(|l| l.code.eq_ignore_ascii_case(code) && available(l.code)).unwrap_or(0));
|
||||
}
|
||||
|
||||
/// The current UI language.
|
||||
pub fn current() -> &'static Language {
|
||||
LANGUAGES.get(CURRENT.load(Ordering::Relaxed)).unwrap_or(&LANGUAGES[0])
|
||||
LANGUAGES.get(CURRENT.get()).unwrap_or(&LANGUAGES[0])
|
||||
}
|
||||
|
||||
/// `text` in the current language (unchanged when it has no translation).
|
||||
pub fn tr(text: &str) -> &str {
|
||||
let lang = shared_languages().get(CURRENT.load(Ordering::Relaxed)).copied().unwrap_or(craft_i18n::Lang::EN);
|
||||
let lang = shared_languages().get(CURRENT.get()).copied().unwrap_or(craft_i18n::Lang::EN);
|
||||
I18N.t(lang, text)
|
||||
}
|
||||
|
||||
@@ -182,7 +185,7 @@ fn system_language() -> &'static str {
|
||||
SYSTEM.get_or_init(os_language)
|
||||
}
|
||||
|
||||
/// Tests that switch the global UI language, or draw the UI under it, run one at a time.
|
||||
/// Shared guard used by language fixture tests.
|
||||
#[cfg(test)]
|
||||
pub(crate) static TEST_LANGUAGE: std::sync::Mutex<()> = std::sync::Mutex::new(());
|
||||
|
||||
@@ -192,6 +195,27 @@ mod tests {
|
||||
use super::*;
|
||||
use std::collections::{BTreeSet, HashMap};
|
||||
|
||||
#[test]
|
||||
fn a_parallel_ui_language_context_keeps_its_own_language() {
|
||||
set_language("en");
|
||||
let french = LANGUAGES.iter().position(|language| language.code == "fr").unwrap();
|
||||
let barrier = std::sync::Barrier::new(2);
|
||||
let (first, second) = std::thread::scope(|scope| {
|
||||
let other = scope.spawn(|| {
|
||||
// Exercise the resolved language selection even when the French catalog is absent.
|
||||
CURRENT.set(french);
|
||||
barrier.wait();
|
||||
barrier.wait();
|
||||
current().code
|
||||
});
|
||||
barrier.wait();
|
||||
let first = current().code;
|
||||
barrier.wait();
|
||||
(first, other.join().unwrap())
|
||||
});
|
||||
assert_eq!((first, second), ("en", "fr"));
|
||||
}
|
||||
|
||||
/// Every key the UI looks up: `tr("…")` / `trf("…"` literals in the sources, plus the
|
||||
/// static catalogs whose labels are translated where they are shown.
|
||||
fn keys() -> Vec<String> {
|
||||
|
||||
Reference in new issue
Block a user