Skip to content

uucore: add functions to manage translations #7955

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
wants to merge 2 commits into
base: main
Choose a base branch
from

Conversation

sylvestre
Copy link
Contributor

No description provided.

@sylvestre sylvestre force-pushed the locale branch 8 times, most recently from 572c58e to 3551c6f Compare May 18, 2025 19:44
Copy link

GNU testsuite comparison:

GNU test failed: tests/mkdir/selinux. tests/mkdir/selinux is passing on 'main'. Maybe you have to rebase?
Skip an intermittent issue tests/timeout/timeout (fails in this run but passes in the 'main' branch)

Copy link

GNU testsuite comparison:

Skip an intermittent issue tests/timeout/timeout (fails in this run but passes in the 'main' branch)

Copy link

GNU testsuite comparison:

Skipping an intermittent issue tests/misc/stdbuf (passes in this run but fails in the 'main' branch)

@sylvestre sylvestre marked this pull request as ready for review May 21, 2025 17:47
@sylvestre sylvestre requested review from cakebaker and Copilot May 21, 2025 17:47
Copy link

@Copilot Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull Request Overview

This PR adds new functions and structures to manage localization and translations in the uucore package. Key changes include:

  • Introduction of the locale module with functionality to initialize localization, load Fluent bundles, and retrieve localized messages.
  • Updates to mod files and Cargo.toml to expose the new locale module and add necessary Fluent dependencies.
  • Modifications to configuration files (deny.toml and VSCode wordlist) to support the new dependencies.

Reviewed Changes

Copilot reviewed 9 out of 9 changed files in this pull request and generated no comments.

Show a summary per file
File Description
src/uucore/src/lib/mods/locale.rs New localization functionality with resource loading and message formatting functions.
src/uucore/src/lib/mods.rs Locale module exposed.
src/uucore/src/lib/lib.rs Locale module publicly re-exported.
src/uucore/Cargo.toml Dependency changes for Fluent and related crates.
deny.toml Updated dependency skips relevant to Fluent usage.
Cargo.toml Added Fluent dependencies.
.vscode/cspell.dictionaries/workspace.wordlist.txt Updated wordlist for new terminology.
Comments suppressed due to low confidence (1)

src/uucore/src/lib/mods/locale.rs:229

  • [nitpick] The parameter name 'rustc_args' may be misleading since the function works with Fluent arguments for localization. Consider renaming it to 'args' or 'fluent_args' to better reflect its purpose.
    rustc_args: HashMap<String, String>,

Copy link

GNU testsuite comparison:

Skip an intermittent issue tests/misc/tee (fails in this run but passes in the 'main' branch)
Skip an intermittent issue tests/timeout/timeout (fails in this run but passes in the 'main' branch)

@sylvestre sylvestre force-pushed the locale branch 2 times, most recently from 793d4e0 to fc17288 Compare May 22, 2025 07:52
let locale_path = config.get_locale_path(&try_locale);
tried_paths.push(locale_path.clone());

if let Ok(ftl_string) = fs::read_to_string(&locale_path) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What does ftl mean?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

fluent

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

in the wrong order

Comment on lines +134 to +135
loaded = true;
break;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Instead of a break you could do an early return. It would make the loaded variable obsolete.

Copy link

GNU testsuite comparison:

Skipping an intermittent issue tests/misc/stdbuf (passes in this run but fails in the 'main' branch)
Skipping an intermittent issue tests/timeout/timeout (passes in this run but fails in the 'main' branch)

Ok(bundle)
}

fn get_message_internal(id: &str, args: Option<FluentArgs>, default: &str) -> String {
Copy link
Contributor

@cakebaker cakebaker May 22, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think it should be get_internal_message. Never mind, my advice is incorrect, though I'm still not completely happy with the function name.

/// ```
pub fn get_message_with_args(
id: &str,
rustc_args: HashMap<String, String>,
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't understand why it is prefixed with rustc.

Comment on lines +232 to +235
let mut args = FluentArgs::new();
for (k, v) in rustc_args {
args.set(k, v);
}
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think you can simplify it to:

Suggested change
let mut args = FluentArgs::new();
for (k, v) in rustc_args {
args.set(k, v);
}
let args = rustc_args.into_iter().collect();

Comment on lines +314 to +318
let locale = match detect_system_locale() {
Ok(loc) => loc,
Err(_) => LanguageIdentifier::from_str(DEFAULT_LOCALE)
.expect("Default locale should always be valid"),
};
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think you can simplify it to:

Suggested change
let locale = match detect_system_locale() {
Ok(loc) => loc,
Err(_) => LanguageIdentifier::from_str(DEFAULT_LOCALE)
.expect("Default locale should always be valid"),
};
let locale = detect_system_locale().unwrap_or_else(|_| {
LanguageIdentifier::from_str(DEFAULT_LOCALE).expect("Default locale should always be valid")
});

Comment on lines +102 to +109
// Always ensure DEFAULT_LOCALE is in the fallback chain
let default_locale: LanguageIdentifier = DEFAULT_LOCALE
.parse()
.map_err(|_| LocalizationError::Parse("Failed to parse default locale".into()))?;

if !locales_to_try.contains(&default_locale) {
locales_to_try.push(default_locale);
}
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is this code necessary? In setup_localization you already add the DEFAULT_LOCALE as a fallback locale and thus it is always in locales_to_try because of line 100.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants