diff --git a/crates/engine/src/settings/prefs/choices.rs b/crates/engine/src/settings/prefs/choices.rs index 20f7a482e..da302e495 100644 --- a/crates/engine/src/settings/prefs/choices.rs +++ b/crates/engine/src/settings/prefs/choices.rs @@ -14,6 +14,7 @@ pub const LANGUAGES: &[(&str, &str)] = &[ ("Русский", "ru"), ("Українська", "uk"), ("ქართული", "ka"), + ("العربية", "ar"), ("日本語", "ja"), ("简体中文", "zh-hans"), ("繁體中文", "zh-hant"), diff --git a/crates/engine/src/tests/settings.rs b/crates/engine/src/tests/settings.rs index 20a1e93fd..e78e16a3c 100644 --- a/crates/engine/src/tests/settings.rs +++ b/crates/engine/src/tests/settings.rs @@ -1347,6 +1347,23 @@ fn swedish_interface_language_is_validated_and_persisted() { assert_eq!(Prefs::from_json(r#"{"general":{"language":"sv"}}"#).general.language, "sv"); } +/// Arabic is stored as `ar` rather than a locale tag, and persists like every other catalog. +#[test] +fn arabic_interface_language_is_validated_and_persisted() { + let store = Arc::new(MemoryConfig::default()); + let mut s = Session { config: Some(store.clone()), ..Default::default() }; + assert!(crate::prefs::LANGUAGES.contains(&("العربية", "ar"))); + for value in ["ar-SA", "ar_EG", "AR"] { + assert!(s.execute("prefs.set", json!({"key": "general.language", "value": value})).is_err(), "{value}"); + } + s.execute("prefs.set", json!({"key": "general.language", "value": "ar"})).unwrap(); + assert_eq!(s.execute("prefs.get", json!({"key": "general.language"})).unwrap(), json!("ar")); + let mut ar = Session { config: Some(store), ..Default::default() }; + ar.load_settings(); + assert_eq!(ar.prefs.general.language, "ar"); + assert_eq!(Prefs::from_json(r#"{"general":{"language":"ar"}}"#).general.language, "ar"); +} + #[test] fn unicode_label_colors_fall_back_without_panicking() { use effectcraft_color::Label; diff --git a/crates/ui-egui/src/i18n.rs b/crates/ui-egui/src/i18n.rs index b9b3cf3bc..e9f5abe3e 100644 --- a/crates/ui-egui/src/i18n.rs +++ b/crates/ui-egui/src/i18n.rs @@ -14,8 +14,31 @@ use effectcraft_engine::menus::{MenuEntry, MenuNode}; pub(crate) use ui::catalog_characters; pub(crate) use ui::{set_current, tr, tr_args}; +/// An optional Arabic menu catalog loaded dynamically on native or registered by a consumer. +static ARABIC_CATALOG: std::sync::OnceLock<&'static [(&'static str, &'static str, &'static str)]> = std::sync::OnceLock::new(); + +#[cfg(test)] +std::thread_local! { + static TEST_ARABIC_CATALOG: std::cell::Cell> = const { std::cell::Cell::new(None) }; +} + +/// Registers an Arabic menu catalog. Returns whether registration succeeded. +#[allow(dead_code)] +pub(crate) fn set_arabic_catalog(catalog: &'static [(&'static str, &'static str, &'static str)]) -> bool { + ARABIC_CATALOG.set(catalog).is_ok() +} + +/// Whether an Arabic catalog has been loaded into the runtime. +pub(crate) fn has_arabic_catalog() -> bool { + #[cfg(test)] + if TEST_ARABIC_CATALOG.with(|c| c.get().is_some()) { + return true; + } + ARABIC_CATALOG.get().is_some() +} + /// Every language with a catalog, in the order Settings ▸ General ▸ Language lists them. -const CATALOG_CODES: [&str; 13] = ["cs", "de", "es", "fr", "pl", "pt-br", "sv", "ru", "uk", "ka", "ja", "zh-hans", "zh-hant"]; +const CATALOG_CODES: [&str; 14] = ["cs", "de", "es", "fr", "pl", "pt-br", "sv", "ru", "uk", "ka", "ar", "ja", "zh-hans", "zh-hant"]; /// The active interface language: the stored preference when it names a catalog, otherwise `en` /// (English is the source of every label and needs no lookup). @@ -26,9 +49,20 @@ pub(crate) fn language(app: &EffectcraftApp) -> &'static str { } } +/// Resolve the interface language for `stored` preference on a specific platform (`is_web`). +pub(crate) fn resolve_language(stored: &str, is_web: bool) -> &'static str { + if stored == "ar" && (is_web || !has_arabic_catalog()) { + return "en"; + } + if stored == "system" { + return "en"; + } + CATALOG_CODES.iter().copied().find(|c| *c == stored).unwrap_or("en") +} + /// `stored` as the language to show: a catalog's code, otherwise `en`. fn code(stored: &str) -> &'static str { - CATALOG_CODES.iter().copied().find(|c| *c == stored).unwrap_or("en") + resolve_language(stored, cfg!(target_arch = "wasm32")) } /// The UI is in Japanese: the fast path for the menu builders. @@ -81,6 +115,21 @@ pub(crate) fn swedish(app: &EffectcraftApp) -> bool { language(app) == "sv" } +/// The UI is in Arabic. +pub(crate) fn arabic(app: &EffectcraftApp) -> bool { + language(app) == "ar" +} + +/// Whether `language` reads right-to-left. +pub(crate) fn is_rtl_language(language: &str) -> bool { + matches!(language, "ar" | "he") +} + +/// Whether the current interface language reads right-to-left. +pub(crate) fn is_rtl() -> bool { + is_rtl_language(ui::current()) +} + /// Settings ▸ General ▸ Language ▸ Match System: the operating system's interface language /// where EffectCraft has it, else English (#229), as After Effects installs in the system's /// language. The browser build follows the locales its page passes in ([`set_system_locales`]). @@ -135,7 +184,7 @@ fn first_supported(requested: Option<&str>, browser: &[String], drawable: impl F /// rather than draw boxes; `?lang=` and a language chosen in Settings still select them. #[cfg_attr(not(target_arch = "wasm32"), allow(dead_code))] fn web_draws(language: &str) -> bool { - !matches!(language, "ja" | "zh-hans" | "zh-hant" | "ka") + !matches!(language, "ja" | "zh-hans" | "zh-hant" | "ka" | "ar") } /// The language EffectCraft shows for a BCP 47 locale (`ja-JP` → `ja`, `zh-Hans-CN` → `zh-hans`, @@ -214,6 +263,13 @@ pub(crate) fn label<'a>(app: &EffectcraftApp, command: &str, source: &'a str) -> /// The menu catalog for `language`; `None` for English. fn menu_catalog(language: &str) -> Option<&'static [(&'static str, &'static str, &'static str)]> { + if language == "ar" { + #[cfg(test)] + if let Some(cat) = TEST_ARABIC_CATALOG.with(|c| c.get()) { + return Some(cat); + } + return ARABIC_CATALOG.get().copied(); + } Some(match language { "ja" => JAPANESE, "zh-hans" => SIMPLIFIED_CHINESE, @@ -9021,7 +9077,10 @@ mod tests { keys(&parse(TREE, mac).unwrap(), &mut actual); } for code in CATALOG_CODES { - let catalog = menu_catalog(code).unwrap(); + if code == "ar" && menu_catalog(code).is_none() { + continue; + } + let catalog = menu_catalog(code).unwrap_or_else(|| panic!("{code}: missing shipped menu catalog")); let mut unique = BTreeSet::new(); for (command, en, translated) in catalog { let key = (command.to_string(), en.to_string()); @@ -9327,6 +9386,10 @@ mod tests { let closed_path = menu_entry("path.toggleClosed", "Closed"); let undo = menu_entry("edit.undo", "Undo"); for code in CATALOG_CODES { + if code == "ar" && menu_catalog(code).is_none() { + continue; + } + assert!(menu_catalog(code).is_some(), "{code}: missing shipped menu catalog"); app.session.execute("prefs.set", json!({"key":"general.language", "value":code})).unwrap(); let drawn = entry(&app, &new_comp, new_comp.label.clone()); assert_eq!(drawn, label(&app, "comp.new", "New Composition..."), "{code}"); @@ -9358,4 +9421,49 @@ mod tests { out.textures_delta.clear(); assert!(app.auto.find("settings.general.language").is_some()); } + + #[test] + fn web_arabic_preference_falls_back_to_english() { + assert_eq!(resolve_language("ar", true), "en"); + let mut app = EffectcraftApp::new(effectcraft_engine::Session::default()); + app.session.execute("prefs.set", json!({"key": "general.language", "value": "ar"})).unwrap(); + // On web, stored "ar" preference must fall back to "en". + assert_eq!(resolve_language(&app.session.prefs.general.language, true), "en"); + } + + #[test] + fn native_arabic_availability_is_gated_on_catalog_and_verifies_real_translations() { + let mut app = EffectcraftApp::new(effectcraft_engine::Session::default()); + assert!(!arabic(&app)); + assert!(is_rtl_language("ar")); + assert!(is_rtl_language("he")); + assert!(!is_rtl_language("en")); + + // When the catalog is not loaded, native resolution falls back to "en" and stays LTR. + assert_eq!(resolve_language("ar", false), "en"); + + // Now load an Arabic catalog on native (scoped to this test thread so parallel tests remain isolated). + static AR_TEST_CATALOG: &[(&str, &str, &str)] = &[("comp.new", "New Composition...", "تركيب جديد..."), ("file.new", "New", "جديد")]; + struct Reset(Option<&'static [(&'static str, &'static str, &'static str)]>); + impl Drop for Reset { + fn drop(&mut self) { + TEST_ARABIC_CATALOG.with(|c| c.set(self.0)); + } + } + let prev = TEST_ARABIC_CATALOG.with(|c| c.replace(Some(AR_TEST_CATALOG))); + let _reset = Reset(prev); + assert!(has_arabic_catalog()); + assert_eq!(resolve_language("ar", false), "ar"); + + // With catalog loaded on native, selecting Arabic activates "ar", RTL, and real translations. + set_current("ar"); + app.session.execute("prefs.set", json!({"key": "general.language", "value": "ar"})).unwrap(); + assert!(arabic(&app)); + assert!(is_rtl()); + assert_eq!(language(&app), "ar"); + assert_eq!(label(&app, "comp.new", "New Composition..."), "تركيب جديد..."); + + set_current("en"); + assert!(!is_rtl()); + } } diff --git a/crates/ui-egui/src/native_menu.rs b/crates/ui-egui/src/native_menu.rs index 23e2a3512..df90b119a 100644 --- a/crates/ui-egui/src/native_menu.rs +++ b/crates/ui-egui/src/native_menu.rs @@ -282,6 +282,8 @@ pub fn build(app: &EffectcraftApp) -> NativeMenu { "Usługi" } else if crate::i18n::georgian(app) { "სერვისები" + } else if crate::i18n::arabic(app) { + crate::i18n::label(app, "", "Services") } else if crate::i18n::swedish(app) { "Tjänster" } else { diff --git a/crates/ui-egui/src/panels/home.rs b/crates/ui-egui/src/panels/home.rs index 968206d66..f8b88894e 100644 --- a/crates/ui-egui/src/panels/home.rs +++ b/crates/ui-egui/src/panels/home.rs @@ -228,11 +228,21 @@ pub fn show(app: &mut EffectcraftApp, ui: &mut egui::Ui, rect: Rect) { let p = ui.painter().with_clip_rect(rect); p.rect_filled(rect, 0.0, t.app_bg); let mut actions: Vec<(&str, serde_json::Value)> = vec![]; - let rail = Rect::from_min_size(rect.min, vec2(RAIL_W.min(rect.width() * 0.4), rect.height())); - p.rect_filled(rail, 0.0, t.panel_bg); - p.line_segment([rail.right_top(), rail.right_bottom()], Stroke::new(1.0, t.separator)); + let rail_w = RAIL_W.min(rect.width() * 0.4); + let (rail, main) = if crate::i18n::is_rtl() { + let rail = Rect::from_min_max(pos2(rect.max.x - rail_w, rect.min.y), rect.max); + p.rect_filled(rail, 0.0, t.panel_bg); + p.line_segment([rail.left_top(), rail.left_bottom()], Stroke::new(1.0, t.separator)); + let main = Rect::from_min_max(pos2(rect.min.x + 40.0, rect.min.y + 32.0), pos2(rail.min.x - 40.0, rect.max.y - 16.0)); + (rail, main) + } else { + let rail = Rect::from_min_size(rect.min, vec2(rail_w, rect.height())); + p.rect_filled(rail, 0.0, t.panel_bg); + p.line_segment([rail.right_top(), rail.right_bottom()], Stroke::new(1.0, t.separator)); + let main = Rect::from_min_max(pos2(rail.max.x + 40.0, rect.min.y + 32.0), pos2(rect.max.x - 40.0, rect.max.y - 16.0)); + (rail, main) + }; rail_ui(app, ui, &p, rail, &mut actions); - let main = Rect::from_min_max(pos2(rail.max.x + 40.0, rect.min.y + 32.0), pos2(rect.max.x - 40.0, rect.max.y - 16.0)); if app.ui.home_learn { super::learn::home_tab(app, ui, main); } else if app.ui.home_templates { diff --git a/crates/ui-egui/src/panels/settings.rs b/crates/ui-egui/src/panels/settings.rs index 5984450be..7ea01c45e 100644 --- a/crates/ui-egui/src/panels/settings.rs +++ b/crates/ui-egui/src/panels/settings.rs @@ -178,6 +178,12 @@ fn page_ui(app: &mut EffectcraftApp, ui: &mut egui::Ui, page: &Page, cur: &Value .width(220.0) .show_ui(ui, |ui| { for (l, val) in opts { + if key == "general.language" + && *val == "ar" + && (cfg!(target_arch = "wasm32") || !crate::i18n::has_arabic_catalog()) + { + continue; + } if ui.selectable_label(*val == curv, tr(l)).clicked() { acts.push(Act::Set(key.into(), json!(val))); } diff --git a/crates/ui-egui/src/theme.rs b/crates/ui-egui/src/theme.rs index bc5f71200..f925f1a43 100644 --- a/crates/ui-egui/src/theme.rs +++ b/crates/ui-egui/src/theme.rs @@ -647,6 +647,7 @@ pub fn apply_visuals(ctx: &egui::Context, t: &Tokens) { // egui keeps one style per light/dark theme and picks it by its theme preference (see // [`theme_preference`]); both get ours, so whichever it picks, egui's defaults never show. ctx.all_styles_mut(|s| { + s.right_to_left = crate::i18n::is_rtl(); s.visuals = v.clone(); s.spacing.item_spacing = egui::vec2(6.0, 4.0); s.spacing.button_padding = egui::vec2(8.0, 3.0);