Repository navigation
uucore: read the English strings when no localizer was set up #15248
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
sunsss123soldier
wants to merge
1
commit into
uutils:main
Choose a base branch
from
sunsss123soldier:uucore-english-fallback
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+197
−10
Open
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -68,13 +68,15 @@ include!(concat!(env!("OUT_DIR"), "/embedded_locales.rs")); | |
| struct Localizer { | ||
| primary_bundle: FluentBundle<&'static FluentResource>, | ||
| fallback_bundle: Option<FluentBundle<&'static FluentResource>>, | ||
| is_default: bool, | ||
| } | ||
|
|
||
| impl Localizer { | ||
| fn new(primary_bundle: FluentBundle<&'static FluentResource>) -> Self { | ||
| 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<String> { | ||
| 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<Option<Localizer>> = 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<Option<FluentBundle<FluentResource>>> = | ||
| const { OnceLock::new() }; | ||
| static ERRORS_BUNDLE: RefCell<OnceLock<Option<FluentBundle<FluentResource>>>> = | ||
| 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,23 +475,59 @@ 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<FluentArgs>) -> 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 | ||
| /// the localized text. If the message ID is not found in the current locale, | ||
| /// 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. | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. please also check that a utility string still returns the id here
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Added, ls-about is asserted to still come back as an id. |
||
| 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"); | ||
|
|
||
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
with "uucore" as util name, uucore/en-US.ftl is parsed and leaked a second time (once in UUCORE_FLUENT, once in RESOURCES), no?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
You're right. I've skipped loading utility-specific strings when the utility name is uucore, since UUCORE_FLUENT already provides that resource. This avoids the second parse and allocation.