Skip to content

DesignCraft: An open-source page and layout design editor written by scratch in pure Rust

Merge origin/main into multi-language-fix

Brandon Thomas8d2d180062a0
diff --cc crates/ui-egui/src/i18n.rs
index d7657220a,b1054835d..efad741d0
--- a/crates/ui-egui/src/i18n.rs
+++ b/crates/ui-egui/src/i18n.rs
@@@ -1,13 -1,20 +1,22 @@@
//! Interface language (Edit › Interface Language): translations of menu titles, the most used
//! menu items and panel names. Untranslated strings stay English. The translations are our own.
+use std::{collections::HashMap, sync::OnceLock};
+
mod ar;
+ mod pt_br;
/// Supported interface languages: (code, name in that language).
- pub const LANGUAGES: &[(&str, &str)] =
- &[("", "English"), ("de", "Deutsch"), ("fr", "Français"), ("es", "Español"), ("ja", "日本語"), ("zh", "简体中文"), ("ar", "العربية")];
+ pub const LANGUAGES: &[(&str, &str)] = &[
+ ("", "English"),
+ ("de", "Deutsch"),
+ ("fr", "Français"),
+ ("es", "Español"),
+ ("ja", "日本語"),
+ ("zh", "简体中文"),
+ ("ar", "العربية"),
+ ("pt-br", "Português (Brasil)"),
+ ];
/// English → [German, French, Spanish, Japanese, Simplified Chinese].
const TABLE: &[(&str, [&str; 5])] = &[
@@@ -1912,8 -1925,19 +1921,19 @@@
"在两端对齐的阿拉伯语文本中",
],
),
+ ("Korean Line Breaks:", ["Korean Line Breaks:", "Korean Line Breaks:", "Korean Line Breaks:", "Korean Line Breaks:", "韩文换行:"]),
+ (
+ "Between syllables (not only at spaces)",
+ [
+ "Between syllables (not only at spaces)",
+ "Between syllables (not only at spaces)",
+ "Between syllables (not only at spaces)",
+ "Between syllables (not only at spaces)",
+ "在音节之间(不仅在空格处)",
+ ],
+ ),
("Binding:", ["Binding:", "Binding:", "Binding:", "Binding:", "装订方向:"]),
- ("Ruby:", ["Ruby:", "Ruby:", "Ruby:", "Ruby:", "注音:"]),
+ ("Ruby:", ["Ruby:", "Ruby:", "Ruby:", "Ruby:", "旁注:"]),
(
"Set over the selected text; empty removes it.",
[
@@@ -2544,9 -2568,30 +2564,30 @@@
("Interface", ["Interface", "Interface", "Interface", "Interface", "界面"]),
("Advanced Type", ["Advanced Type", "Advanced Type", "Advanced Type", "Advanced Type", "高级文字"]),
("Composition", ["Composition", "Composition", "Composition", "Composition", "排版"]),
+ (
+ "Show Font Names in English",
+ [
+ "Schriftnamen auf Englisch anzeigen",
+ "Afficher les noms de police en anglais",
+ "Mostrar nombres de fuentes en inglés",
+ "フォント名を英語で表示",
+ "以英文显示字体名称",
+ ],
+ ),
+ ("Missing Glyphs", ["Fehlende Glyphen", "Glyphes manquants", "Glifos que faltan", "欠落グリフ", "缺失字形"]),
+ (
+ "Draw Missing Glyphs from Fallback Fonts",
+ [
+ "Fehlende Glyphen aus Ersatzschriften zeichnen",
+ "Dessiner les glyphes manquants avec des polices de substitution",
+ "Dibujar los glifos que faltan con fuentes alternativas",
+ "欠落グリフを代替フォントで表示",
+ "用后备字体绘制缺失字形",
+ ],
+ ),
- ("Units & Increments", ["Units & Increments", "Units & Increments", "Units & Increments", "Units & Increments", "单位与增量"]),
+ ("Units & Increments", ["Units & Increments", "Units & Increments", "Units & Increments", "Units & Increments", "单位和增量"]),
("Grids", ["Grids", "Grids", "Grids", "Grids", "网格"]),
- ("Guides & Pasteboard", ["Guides & Pasteboard", "Guides & Pasteboard", "Guides & Pasteboard", "Guides & Pasteboard", "参考线与粘贴板"]),
+ ("Guides & Pasteboard", ["Guides & Pasteboard", "Guides & Pasteboard", "Guides & Pasteboard", "Guides & Pasteboard", "参考线和粘贴板"]),
("Dictionary", ["Dictionary", "Dictionary", "Dictionary", "Dictionary", "词典"]),
("Autocorrect", ["Autocorrect", "Autocorrect", "Autocorrect", "Autocorrect", "自动更正"]),
("Story Editor Display", ["Story Editor Display", "Story Editor Display", "Story Editor Display", "Story Editor Display", "文章编辑器显示"]),
@@@ -2635,13 -2680,17 +2676,17 @@@ fn column(lang: &str) -> Option<usize>
/// `s` in `lang` (English, or the string itself, when there's no translation).
pub fn tr<'a>(lang: &str, s: &'a str) -> &'a str {
+ static TRANSLATIONS: OnceLock<HashMap<&'static str, [&'static str; 5]>> = OnceLock::new();
+ static ARABIC: OnceLock<HashMap<&'static str, &'static str>> = OnceLock::new();
++ static PORTUGUESE_BR: OnceLock<HashMap<&'static str, &'static str>> = OnceLock::new();
if lang == "ar" {
- return ar::TABLE.iter().find(|(en, _)| *en == s).map_or(s, |(_, t)| *t);
+ return ARABIC.get_or_init(|| ar::TABLE.iter().copied().collect()).get(s).copied().unwrap_or(s);
}
+ if lang == "pt-br" {
- return pt_br::TABLE.iter().find(|(en, _)| *en == s).map_or(s, |(_, t)| *t);
++ return PORTUGUESE_BR.get_or_init(|| pt_br::TABLE.iter().copied().collect()).get(s).copied().unwrap_or(s);
+ }
let Some(c) = column(lang) else { return s };
- match TABLE.iter().find(|(en, _)| *en == s) {
- Some((_, t)) => t[c],
- None => s,
- }
+ TRANSLATIONS.get_or_init(|| TABLE.iter().copied().collect()).get(s).and_then(|row| row.get(c)).copied().unwrap_or(s)
}
/// Localize reserved built-in style names only; user-defined names are document data.
@@@ -2664,23 -2713,6 +2709,26 @@@ pub fn workspace_name<'a>(lang: &str, n
mod tests {
use super::*;
+ #[test]
+ fn cached_lookup_preserves_every_translation_across_language_switches() {
+ for (key, translations) in TABLE {
+ for (lang, expected) in ["de", "fr", "es", "ja", "zh"].into_iter().zip(translations) {
+ assert_eq!(tr(lang, key), *expected, "{lang}: {key}");
+ }
+ assert_eq!(tr("", key), *key);
+ }
+ for (key, expected) in ar::TABLE {
+ assert_eq!(tr("ar", key), *expected, "ar: {key}");
+ }
++ for (key, expected) in pt_br::TABLE {
++ assert_eq!(tr("pt-br", key), *expected, "pt-br: {key}");
++ }
+ let unknown = String::from("A user-defined untranslated label");
- for lang in ["ar", "zh", "ja", "de", "fr", "es", "", "unknown"] {
++ for lang in ["ar", "pt-br", "zh", "ja", "de", "fr", "es", "", "unknown"] {
+ assert!(std::ptr::eq(tr(lang, &unknown), unknown.as_str()));
+ }
+ }
+
#[test]
fn translates_known_strings_and_keeps_the_rest() {
assert_eq!(tr("de", "File"), "Datei");
diff --cc crates/ui-egui/src/i18n/ar.rs
index 375447746,34375752a..783f33e5e
--- a/crates/ui-egui/src/i18n/ar.rs
+++ b/crates/ui-egui/src/i18n/ar.rs
@@@ -330,12 -329,14 +330,14 @@@ pub(super) const TABLE: &[(&str, &str)
("Words with at Least:", "الحد الأدنى لطول الكلمة:"),
("After First:", "الأحرف قبل الواصلة:"),
("Before Last:", "الأحرف بعد الواصلة:"),
- ("Hyphen Limit:", "حد الأسطر المتتالية بواصلة:"),
+ ("Hyphen Limit:", "الحد الأقصى للواصلات:"),
("Word Spacing:", "تباعد الكلمات:"),
("Letter Spacing:", "تباعد الحروف:"),
- ("Glyph Scaling:", "تحجيم الحروف:"),
+ ("Glyph Scaling:", "قياس الحروف الرسومية:"),
("Insert Kashidas:", "إدراج الكشيدة:"),
("In justified Arabic text", "في النص العربي المضبوط"),
+ ("Korean Line Breaks:", "فواصل الأسطر الكورية:"),
+ ("Between syllables (not only at spaces)", "بين المقاطع (وليس عند المسافات فقط)"),
("Minimum Space Before First Footnote:", "أقل مسافة قبل الحاشية الأولى:"),
("Space Between Footnotes:", "المسافة بين الحواشي:"),
("Offset:", "الإزاحة:"),
@@@ -968,8 -969,11 +970,11 @@@
("⌥-click clears overrides", "النقر مع ⌥ يمسح التجاوزات"),
("⚠ out of date", "⚠ غير محدّث"),
("Interface", "الواجهة"),
- ("Advanced Type", "إعدادات النص المتقدمة"),
- ("Composition", "تنضيد النص"),
+ ("Advanced Type", "كتابة متقدمة"),
+ ("Composition", "التركيب"),
+ ("Show Font Names in English", "إظهار أسماء الخطوط بالإنجليزية"),
+ ("Missing Glyphs", "الحروف الناقصة"),
+ ("Draw Missing Glyphs from Fallback Fonts", "رسم الحروف الناقصة بخطوط بديلة"),
("Units & Increments", "الوحدات والزيادات"),
("Grids", "الشبكات"),
("Guides & Pasteboard", "الأدلة ولوحة اللصق"),