diff --git a/docs/src/l10n.md b/docs/src/l10n.md index 2c2a5582fe2..f5c9c0814f0 100644 --- a/docs/src/l10n.md +++ b/docs/src/l10n.md @@ -51,6 +51,9 @@ This is typically done: The string parameter determines the lookup path for Fluent files. **English always works** because it's embedded, but other languages need their `.ftl` files to be available at runtime. +Calling it is not required for English: a project using `uucore` as a library still reads the English strings `uucore` itself owns. +Only a utility's own strings need the name that `setup_localization` supplies. + --- ## Locale Detection diff --git a/src/uucore/Cargo.toml b/src/uucore/Cargo.toml index edb7f86de45..98e8dd0b27f 100644 --- a/src/uucore/Cargo.toml +++ b/src/uucore/Cargo.toml @@ -89,6 +89,7 @@ thiserror = { workspace = true } [build-dependencies] cfg_aliases = { workspace = true } +fluent-syntax = { workspace = true } [dev-dependencies] tempfile = { workspace = true } diff --git a/src/uucore/build.rs b/src/uucore/build.rs index 7adada1b7e5..2ca3a09925a 100644 --- a/src/uucore/build.rs +++ b/src/uucore/build.rs @@ -90,10 +90,54 @@ pub fn main() -> Result<(), Box> { writeln!(embedded_file, " }}")?; writeln!(embedded_file, "}}")?; + embed_uucore_message_ids(&mut embedded_file)?; embedded_file.flush()?; Ok(()) } +/// Index uucore's English message ids so a missing id never needs runtime parsing. +fn embed_uucore_message_ids(embedded_file: &mut File) -> Result<(), Box> { + let manifest_dir = PathBuf::from(env::var("CARGO_MANIFEST_DIR")?); + let mut ids = std::collections::BTreeSet::new(); + for relative_path in ["locales/en-US.ftl", "locales/errors/en-US.ftl"] { + let path = manifest_dir.join(relative_path); + println!("cargo:rerun-if-changed={}", path.display()); + let content = std::fs::read_to_string(&path)?; + let resource = + fluent_syntax::parser::parse_runtime(content.as_str()).map_err(|(_, errors)| { + std::io::Error::new( + std::io::ErrorKind::InvalidData, + format!("Invalid Fluent resource '{}': {errors:?}", path.display()), + ) + })?; + for entry in resource.body { + if let fluent_syntax::ast::Entry::Message(message) = entry + && message.value.is_some() + { + ids.insert(message.id.name.to_string()); + } + } + } + + writeln!(embedded_file)?; + writeln!( + embedded_file, + "fn embedded_uucore_message_exists(id: &str) -> bool {{" + )?; + if ids.is_empty() { + writeln!(embedded_file, " false")?; + } else { + writeln!(embedded_file, " matches!(id,")?; + for (index, id) in ids.into_iter().enumerate() { + let separator = if index == 0 { "" } else { "| " }; + writeln!(embedded_file, " {separator}{id:?}")?; + } + writeln!(embedded_file, " )")?; + } + writeln!(embedded_file, "}}")?; + Ok(()) +} + /// Get the project root directory /// /// # Errors diff --git a/src/uucore/src/lib/mods/locale.rs b/src/uucore/src/lib/mods/locale.rs index ab323fc893a..d001939190d 100644 --- a/src/uucore/src/lib/mods/locale.rs +++ b/src/uucore/src/lib/mods/locale.rs @@ -68,6 +68,7 @@ include!(concat!(env!("OUT_DIR"), "/embedded_locales.rs")); struct Localizer { primary_bundle: FluentBundle<&'static FluentResource>, fallback_bundle: Option>, + is_default: bool, } impl Localizer { @@ -75,6 +76,7 @@ impl Localizer { Self { primary_bundle, fallback_bundle: None, + is_default: false, } } @@ -105,6 +107,9 @@ impl Localizer { // Only now, once nothing common has matched, is it worth parsing the // strings that only an error path asks for. + if self.is_default && !embedded_uucore_message_exists(id) { + return id.to_string(); + } if let Some(message) = errors_message(self.primary_bundle.locales.as_slice(), id, args) { return message; } @@ -125,7 +130,7 @@ fn errors_message( id: &str, args: Option<&FluentArgs>, ) -> Option { - ERRORS_BUNDLE.with(|cell| { + ERRORS_BUNDLE.with_borrow(|cell| { let bundle = cell.get_or_init(|| build_errors_bundle(locales)).as_ref()?; let message = bundle.get_message(id)?.value()?; let mut errs = Vec::new(); @@ -192,8 +197,8 @@ thread_local! { static LOCALIZER: RefCell> = const { RefCell::new(None) }; /// Built on the first lookup that misses every ordinary bundle; `None` /// when there are no error strings to be found at all. - static ERRORS_BUNDLE: OnceLock>> = - const { OnceLock::new() }; + static ERRORS_BUNDLE: RefCell>>> = + const { RefCell::new(OnceLock::new()) }; } /// Helper function to find the uucore locales directory from a utility's locales directory @@ -411,7 +416,9 @@ fn create_english_bundle_from_embedded( // Then, try to load utility-specific strings let locale_key = format!("{util_name}/en-US.ftl"); - if let Some(ftl_content) = get_embedded_locale(&locale_key) { + if util_name != "uucore" + && let Some(ftl_content) = get_embedded_locale(&locale_key) + { let resource = cached_resource(format!("embedded:{locale_key}"), || { parse_fluent_resource_owned(ftl_content) })?; @@ -468,16 +475,50 @@ fn create_wasi_bundle_from_embedded( } fn set_localizer(localizer: Localizer) { - LOCALIZER.with_borrow_mut(|slot| *slot = Some(localizer)); + LOCALIZER.with_borrow_mut(|slot| { + if slot + .as_ref() + .is_some_and(|old| old.primary_bundle.locales != localizer.primary_bundle.locales) + { + // A fallback lookup may have cached English before explicit setup. + ERRORS_BUNDLE.with_borrow_mut(|cell| { + cell.take(); + }); + } + *slot = Some(localizer); + }); } fn get_message_internal(id: &str, args: Option) -> String { - LOCALIZER.with_borrow(|slot| { - slot.as_ref() - .map_or_else(|| id.to_string(), |loc| loc.format(id, args.as_ref())) // Return the key ID if localizer not initialized + if let Some(message) = + LOCALIZER.with_borrow(|slot| slot.as_ref().map(|loc| loc.format(id, args.as_ref()))) + { + return message; + } + // A utility may request its own strings before setting up localization. + // Those ids cannot use the uucore fallback, so leave its resources unparsed. + if !embedded_uucore_message_exists(id) { + return id.to_string(); + } + LOCALIZER.with_borrow_mut(|slot| { + // Nothing set a localizer up; a library consumer still reads text, not an id. + slot.get_or_insert_with(default_localizer) + .format(id, args.as_ref()) }) } +/// What a process that never called [`setup_localization`] reads: uucore's own strings. +fn default_localizer() -> Localizer { + let locale = LanguageIdentifier::from_str(DEFAULT_LOCALE) + .expect("Default locale should always be valid"); + // Use an empty bundle if loading the embedded English strings fails. + let bundle = create_english_bundle_from_embedded(&locale, "uucore") + .unwrap_or_else(|_| FluentBundle::new(vec![locale])); + let mut localizer = Localizer::new(bundle); + localizer.is_default = true; + localizer +} + /// Retrieves a localized message by its identifier. /// /// Looks up a message with the given ID in the current locale bundle and returns @@ -485,6 +526,8 @@ fn get_message_internal(id: &str, args: Option) -> String { /// it will fall back to English. If the message is not found in English either, /// returns the message ID itself. /// +/// Without [`setup_localization`], uucore's own English strings are still read. +/// /// # Arguments /// /// * `id` - The message identifier in the Fluent resources @@ -514,6 +557,8 @@ pub fn get_message(id: &str) -> String { /// it will fall back to English. If the message is not found in English either, /// returns the message ID itself. /// +/// Without [`setup_localization`], uucore's own English strings are still read. +/// /// # Arguments /// /// * `id` - The message identifier in the Fluent resources @@ -1680,6 +1725,99 @@ invalid-syntax = This is { $missing .unwrap(); } + #[test] + fn test_default_message_lookup_matches_embedded_resources() { + for key in ["uucore/en-US.ftl", "uucore-errors/en-US.ftl"] { + let content = get_embedded_locale(key).unwrap(); + let resource = FluentResource::try_new(content.to_string()).unwrap(); + for entry in resource.entries() { + if let fluent_syntax::ast::Entry::Message(message) = entry + && message.value.is_some() + { + assert!(embedded_uucore_message_exists(message.id.name)); + } + } + } + for id in [ + "", + "common", + "common-error-extra", + "common-error ", + "ls-about", + ] { + assert!( + !embedded_uucore_message_exists(id), + "unexpected message: {id}" + ); + } + } + + #[test] + fn test_unknown_messages_do_not_initialize_default_resources() { + std::thread::spawn(|| { + for id in ["ls-about", "cat-usage", "nonexistent-message"] { + assert_eq!(get_message(id), id); + } + LOCALIZER.with_borrow(|slot| assert!(slot.is_none())); + ERRORS_BUNDLE.with_borrow(|cell| assert!(cell.get().is_none())); + + assert_eq!(get_message("common-error"), "error"); + assert_eq!(get_message("ls-about"), "ls-about"); + ERRORS_BUNDLE.with_borrow(|cell| assert!(cell.get().is_none())); + + setup_localization("ls").unwrap(); + assert_ne!(get_message("ls-about"), "ls-about"); + }) + .join() + .unwrap(); + } + + #[test] + fn test_uucore_strings_survive_a_missing_localizer() { + // A library consumer never calls `setup_localization`; it still reads text. + std::thread::spawn(|| { + let mut args = FluentArgs::new(); + args.set("operator", "x"); + assert_eq!( + get_message_with_args("mode-error-invalid-operator", args), + "invalid operator (expected +, -, or =, but found x)" + ); + + // The strings only an error path asks for are found too + let mut args = FluentArgs::new(); + args.set("spec", "q"); + assert_eq!( + get_message_with_args("format-error-invalid-spec", args), + "%q: invalid conversion specification" + ); + + // A utility's own string still needs the utility name + assert_eq!(get_message("ls-about"), "ls-about"); + }) + .join() + .unwrap(); + } + + #[cfg(debug_assertions)] + #[test] + fn test_setup_replaces_default_error_locale() { + std::thread::spawn(|| { + assert_eq!( + get_message("format-error-wrong-spec-type"), + "wrong % directive type was given" + ); + let temp_dir = create_test_locales_dir(); + let locale = LanguageIdentifier::from_str("fr-FR").unwrap(); + init_test_localization(&locale, temp_dir.path()).unwrap(); + assert_eq!( + get_message("format-error-wrong-spec-type"), + "type de directive % incorrect" + ); + }) + .join() + .unwrap(); + } + #[test] fn test_detect_system_locale_from_lang_env() { // Test locale parsing logic directly instead of relying on environment variables @@ -1845,11 +1983,12 @@ invalid-syntax = This is { $missing // and we need fallbacks for clap error handling // First, test when localizer is not initialized + // uucore ships English for the strings it owns, so these are text now let error_msg = get_message("common-error"); - assert_eq!(error_msg, "common-error"); // Should return key when not initialized + assert_eq!(error_msg, "error"); let tip_msg = get_message("common-tip"); - assert_eq!(tip_msg, "common-tip"); // Should return key when not initialized + assert_eq!(tip_msg, "tip"); // Now initialize with setup_localization let result = setup_localization("comm");