Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -133,6 +133,11 @@ For a quantity, put both complete messages under `one` and `other` and pass
`count = quantity` before the other arguments. English uses `one` for exactly
1 and `other` for zero and all other quantities.

Translations sit next to it with the same keys and arguments:
[zh_CN.json](assets/i18n/zh_CN.json) (Simplified Chinese, always `other`) and
[pt_BR.json](assets/i18n/pt_BR.json) (Brazilian Portuguese, `one` for exactly 1,
like English). A new key added to `en.json` needs an entry in each translation.

Rust embeds English at build time and the browser startup screen reads the
same catalog. Keep storage keys, shortcuts, file-format tokens and wire codes
separate from display messages. Release-note content lives in its own feed.
Expand Down
1 change: 1 addition & 0 deletions assets/i18n/en.json
Original file line number Diff line number Diff line change
Expand Up @@ -2064,6 +2064,7 @@
"settings.invert_tilt_and_twist": "Invert tilt and twist",
"settings.invert_zoom": "Invert zoom",
"settings.language_simplified_chinese": "Simplified Chinese",
"settings.language_brazilian_portuguese": "Brazilian Portuguese",
"settings.look_sensitivity": "Look sensitivity",
"settings.mouse_trackpad": "Mouse/Trackpad",
"settings.navigation_type": "Navigation type",
Expand Down
2,105 changes: 2,105 additions & 0 deletions assets/i18n/pt_BR.json

Large diffs are not rendered by default.

1 change: 1 addition & 0 deletions assets/i18n/zh_CN.json
Original file line number Diff line number Diff line change
Expand Up @@ -1972,6 +1972,7 @@
"settings.invert_tilt_and_twist": "反转倾斜和扭转",
"settings.invert_zoom": "反转缩放",
"settings.language_simplified_chinese": "简体中文",
"settings.language_brazilian_portuguese": "Português (Brasil)",
"settings.look_sensitivity": "视角灵敏度",
"settings.mouse_trackpad": "鼠标/触控板",
"settings.navigation_type": "导航类型",
Expand Down
13 changes: 13 additions & 0 deletions crates/benchup-app/src/language.rs
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,14 @@ fn supported_language(locale: &str) -> Option<&'static str> {
.iter()
.find_map(|&(code, _)| (code == "zh_CN").then_some(code));
}

// Brazilian Portuguese is the only Portuguese catalog, so it also serves
// bare pt and other Portuguese regions instead of falling back to English.
if parts.first() == Some(&"pt") {
return supported
.iter()
.find_map(|&(code, _)| (code == "pt_BR").then_some(code));
}
None
}

Expand All @@ -72,6 +80,9 @@ mod tests {
] {
assert_eq!(preferred_language([locale]), "zh_CN", "{locale}");
}
for locale in ["pt", "pt_BR", "pt-BR", "pt_BR.UTF-8", "pt-PT", "pt-AO"] {
assert_eq!(preferred_language([locale]), "pt_BR", "{locale}");
}
}

#[test]
Expand All @@ -97,5 +108,7 @@ mod tests {
assert_eq!(preferred_language(["fr-CA", "zh-Hans", "en-US"]), "zh_CN");
assert_eq!(preferred_language(["en-CA", "zh-CN"]), "en");
assert_eq!(preferred_language(["zh-Hant", "zh-Hans"]), "zh_CN");
assert_eq!(preferred_language(["fr-CA", "pt-BR", "en-US"]), "pt_BR");
assert_eq!(preferred_language(["en-US", "pt-BR"]), "en");
}
}
13 changes: 12 additions & 1 deletion crates/benchup-app/src/settings.rs
Original file line number Diff line number Diff line change
Expand Up @@ -159,10 +159,11 @@ pub(super) fn detected_language() -> &'static str {
language::detected_language()
}

fn languages() -> [(&'static str, &'static str); 2] {
fn languages() -> [(&'static str, &'static str); 3] {
[
(DEFAULT_LANGUAGE, tr!("settings.language_english")),
("zh_CN", tr!("settings.language_simplified_chinese")),
("pt_BR", tr!("settings.language_brazilian_portuguese")),
]
}

Expand Down Expand Up @@ -1134,6 +1135,15 @@ mod tests {
assert_eq!(settings.language, "zh_CN");
}

#[test]
fn supported_languages_include_brazilian_portuguese() {
assert!(languages().contains(&("pt_BR", "Brazilian Portuguese")));
let mut settings = GeneralSettings::default();
settings.language = "pt_BR".into();
settings.normalize();
assert_eq!(settings.language, "pt_BR");
}

#[test]
fn settings_labels_switch_languages_in_the_same_window() {
struct Reset;
Expand All @@ -1147,6 +1157,7 @@ mod tests {
for (language, title, group) in [
("en", "Settings", "Workspace"),
("zh_CN", "设置", "工作区"),
("pt_BR", "Configurações", "Área de trabalho"),
("en", "Settings", "Workspace"),
] {
app.general_settings.language = language.into();
Expand Down
2 changes: 1 addition & 1 deletion crates/benchup-app/src/sidebar.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3046,7 +3046,7 @@ mod preference_tests {

#[test]
fn first_launch_language_is_saved_without_a_model_edit_and_not_redetected() {
for language in ["en", "zh_CN"] {
for language in ["en", "zh_CN", "pt_BR"] {
let ctx = egui::Context::default();
let mut storage = SavedPreferences::default();
let mut app = BenchUpApp::with_document(Document::default(), None, true);
Expand Down
126 changes: 86 additions & 40 deletions crates/benchup-core/build.rs
Original file line number Diff line number Diff line change
@@ -1,68 +1,114 @@
use serde_json::Value;
use std::{collections::BTreeMap, env, fmt::Write, fs, path::PathBuf};

fn main() {
const CATALOG: &str = "../../assets/i18n/en.json";
println!("cargo:rerun-if-changed={CATALOG}");
let catalog = fs::read_to_string(CATALOG).expect("read the English message catalog");
let messages: BTreeMap<String, Value> =
serde_json::from_str(&catalog).expect("parse the English message catalog");
type Catalog = BTreeMap<String, Value>;

const LANGUAGE: &str = "$crate::i18n::Language";

struct Translation {
variant: &'static str,
catalog: Catalog,
/// Whether the language uses the `one` form for a count of exactly 1.
singular: bool,
}

fn read_catalog(path: &str, name: &str) -> Catalog {
println!("cargo:rerun-if-changed={path}");
let text = fs::read_to_string(path)
.unwrap_or_else(|error| panic!("read the {name} message catalog: {error}"));
serde_json::from_str(&text)
.unwrap_or_else(|error| panic!("parse the {name} message catalog: {error}"))
}

fn plural(one: &str, other: &str, singular: bool) -> String {
let format = |text: &str| format!("::std::format!({text:?}, count = count $(, $($args)*)?)");
if singular {
format!("if count as f64 == 1.0 {{ {} }} else {{ {} }}", format(one), format(other))
} else {
format(other)
}
}

const CHINESE_CATALOG: &str = "../../assets/i18n/zh_CN.json";
println!("cargo:rerun-if-changed={CHINESE_CATALOG}");
let chinese: BTreeMap<String, Value> = serde_json::from_str(
&fs::read_to_string(CHINESE_CATALOG).expect("read the Chinese message catalog"),
)
.expect("parse the Chinese message catalog");
fn main() {
let messages = read_catalog("../../assets/i18n/en.json", "English");
let translations = [
Translation {
variant: "SimplifiedChinese",
catalog: read_catalog("../../assets/i18n/zh_CN.json", "Chinese"),
singular: false,
},
Translation {
variant: "BrazilianPortuguese",
catalog: read_catalog("../../assets/i18n/pt_BR.json", "Brazilian Portuguese"),
singular: true,
},
];

// Generate both format strings so Rust continues checking named arguments.
// Generate every format string so Rust continues checking named arguments.
let mut code = String::from(
"#[doc = \"Look up an application message in the selected bundled catalog.\"]\n\
#[macro_export]\nmacro_rules! tr {\n",
);
for (key, message) in messages {
let translated = chinese.get(&key).unwrap_or(&message);
match &message {
for (key, message) in &messages {
match message {
Value::String(text) => {
let translated = translated.as_str().expect("matching message types");
writeln!(code, " ({key:?}) => {{ if $crate::i18n::is_chinese() {{ {translated:?} }} else {{ {text:?} }} }};").unwrap();
let mut labels = String::new();
let mut templates = String::new();
for translation in &translations {
let translated = translation
.catalog
.get(key)
.unwrap_or(message)
.as_str()
.expect("matching message types");
let variant = translation.variant;
write!(labels, "{LANGUAGE}::{variant} => {translated:?}, ").unwrap();
write!(
templates,
"{LANGUAGE}::{variant} => ::std::format!({translated:?}, $($args)+), "
)
.unwrap();
}
writeln!(
code,
" ({key:?}) => {{ match $crate::i18n::current_language() {{ {labels}{LANGUAGE}::English => {text:?} }} }};"
)
.unwrap();
if text.contains('{') {
writeln!(
code,
" ({key:?}, $($args:tt)+) => {{ if $crate::i18n::is_chinese() {{ ::std::format!({translated:?}, $($args)+) }} else {{ ::std::format!({text:?}, $($args)+) }} }};",
" ({key:?}, $($args:tt)+) => {{ match $crate::i18n::current_language() {{ {templates}{LANGUAGE}::English => ::std::format!({text:?}, $($args)+) }} }};"
)
.unwrap();
}
}
Value::Object(forms) => {
assert_eq!(forms.len(), 2, "{key}: expected one and other forms");
let form = |category| {
let form = |forms: &serde_json::Map<String, Value>, category: &str, name: &str| {
forms
.get(category)
.and_then(Value::as_str)
.unwrap_or_else(|| panic!("{key}: missing {category} form"))
.unwrap_or_else(|| panic!("{key}: missing {name} {category} form"))
.to_owned()
};
let translated = translated
.as_object()
.expect("matching plural message types");
let zh_other = translated
.get("other")
.and_then(Value::as_str)
.expect("Chinese plural form");
let one = form("one");
let other = form("other");
let mut arms = String::new();
for translation in &translations {
let translated = translation
.catalog
.get(key)
.unwrap_or(message)
.as_object()
.expect("matching plural message types");
let one = form(translated, "one", translation.variant);
let other = form(translated, "other", translation.variant);
let variant = translation.variant;
let body = plural(&one, &other, translation.singular);
write!(arms, "\n {LANGUAGE}::{variant} => {body},").unwrap();
}
let english = plural(&form(forms, "one", "English"), &form(forms, "other", "English"), true);
writeln!(
code,
r#" ({key:?}, count = $count:expr $(, $($args:tt)*)?) => {{{{
let count = $count;
if $crate::i18n::is_chinese() {{
::std::format!({zh_other:?}, count = count $(, $($args)*)?)
}} else if count as f64 == 1.0 {{
::std::format!({one:?}, count = count $(, $($args)*)?)
}} else {{
::std::format!({other:?}, count = count $(, $($args)*)?)
}}
}}}};"#,
" ({key:?}, count = $count:expr $(, $($args:tt)*)?) => {{{{\n let count = $count;\n match $crate::i18n::current_language() {{{arms}\n {LANGUAGE}::English => {english},\n }}\n }}}};"
)
.unwrap();
}
Expand Down
36 changes: 31 additions & 5 deletions crates/benchup-core/src/i18n.rs
Original file line number Diff line number Diff line change
@@ -1,12 +1,24 @@
use std::cell::Cell;

/// A bundled message catalog.
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
pub enum Language {
English,
SimplifiedChinese,
BrazilianPortuguese,
}

thread_local! {
// UI and export operations resolve messages on their calling thread.
static CHINESE: Cell<bool> = const { Cell::new(false) };
static LANGUAGE: Cell<Language> = const { Cell::new(Language::English) };
}

pub fn set_language(language: &str) {
CHINESE.set(matches!(language, "zh_CN" | "zh-CN" | "zh-Hans"));
LANGUAGE.set(match language {
"zh_CN" | "zh-CN" | "zh-Hans" => Language::SimplifiedChinese,
"pt_BR" | "pt-BR" => Language::BrazilianPortuguese,
_ => Language::English,
});
}

/// Start an operation with the language selected by its caller.
Expand All @@ -16,15 +28,19 @@ where
F: FnOnce() -> T + Send + 'static,
T: Send + 'static,
{
let chinese = is_chinese();
let language = current_language();
std::thread::spawn(move || {
CHINESE.set(chinese);
LANGUAGE.set(language);
operation()
})
}

pub fn current_language() -> Language {
LANGUAGE.get()
}

pub fn is_chinese() -> bool {
CHINESE.get()
current_language() == Language::SimplifiedChinese
}

include!(concat!(env!("OUT_DIR"), "/messages.rs"));
Expand Down Expand Up @@ -59,6 +75,16 @@ mod tests {
);
assert_eq!(count, 1);
assert_eq!(tr!("app.selection_entities", count = 2), "2 个图元");
super::set_language("pt_BR");
assert_eq!(tr!("common.cancel"), "Cancelar");
assert_eq!(tr!("settings.settings"), "Configurações");
#[cfg(not(target_arch = "wasm32"))]
assert_eq!(
super::spawn_localized(super::current_language).join().unwrap(),
super::Language::BrazilianPortuguese
);
assert_eq!(tr!("app.selection_entities", count = 1), "1 entidade");
assert_eq!(tr!("app.selection_entities", count = 2), "2 entidades");
super::set_language("en");
assert_eq!(tr!("common.cancel"), "Cancel");
super::set_language("unsupported");
Expand Down
32 changes: 17 additions & 15 deletions scripts/test_web_i18n.cjs
Original file line number Diff line number Diff line change
Expand Up @@ -37,23 +37,25 @@ test('a late catalog response leaves the open project title intact', async () =>
assert.equal(document.title, 'Workshop - BenchUp');
});

test('Chinese messages preserve English catalog keys and format arguments', () => {
const chinese = require('../assets/i18n/zh_CN.json');
assert.deepEqual(Object.keys(chinese).sort(), Object.keys(catalog).sort());
const argumentsOf = text => [...text.matchAll(/(?<!\{)\{([^{}]*)\}(?!\})/g)]
.map(match => match[1]).sort();
for (const [key, message] of Object.entries(catalog)) {
assert.equal(typeof chinese[key], typeof message, key);
if (typeof message === 'string') {
assert.deepEqual(argumentsOf(chinese[key]), argumentsOf(message), key);
} else {
assert.deepEqual(Object.keys(chinese[key]).sort(), Object.keys(message).sort(), key);
for (const [form, text] of Object.entries(message)) {
assert.deepEqual(argumentsOf(chinese[key][form]), argumentsOf(text), `${key}.${form}`);
for (const [language, file] of [['Chinese', 'zh_CN'], ['Brazilian Portuguese', 'pt_BR']]) {
test(`${language} messages preserve English catalog keys and format arguments`, () => {
const translated = require(`../assets/i18n/${file}.json`);
assert.deepEqual(Object.keys(translated).sort(), Object.keys(catalog).sort());
const argumentsOf = text => [...text.matchAll(/(?<!\{)\{([^{}]*)\}(?!\})/g)]
.map(match => match[1]).sort();
for (const [key, message] of Object.entries(catalog)) {
assert.equal(typeof translated[key], typeof message, key);
if (typeof message === 'string') {
assert.deepEqual(argumentsOf(translated[key]), argumentsOf(message), key);
} else {
assert.deepEqual(Object.keys(translated[key]).sort(), Object.keys(message).sort(), key);
for (const [form, text] of Object.entries(message)) {
assert.deepEqual(argumentsOf(translated[key][form]), argumentsOf(text), `${key}.${form}`);
}
}
}
}
});
});
}

test('runtime translations are formatted before being passed to Rust output macros', () => {
const rustFiles = directory => fs.readdirSync(directory, {withFileTypes: true}).flatMap(entry => {
Expand Down
Loading