Merge pull request #8734 from sylvestre/l10n-perf
Some checks are pending
CICD / Style/cargo-deny (push) Waiting to run
CICD / Style/deps (push) Waiting to run
CICD / Build (push) Blocked by required conditions
CICD / Tests/BusyBox test suite (push) Blocked by required conditions
CICD / Tests/Toybox test suite (push) Blocked by required conditions
CICD / Code Coverage (push) Waiting to run
CICD / Separate Builds (push) Waiting to run
CICD / Documentation/warnings (push) Waiting to run
CICD / MinRustV (push) Waiting to run
CICD / Dependencies (push) Waiting to run
CICD / Build/nightly (push) Blocked by required conditions
CICD / Build/Makefile (push) Blocked by required conditions
CICD / Build/stable (push) Blocked by required conditions
CICD / Binary sizes (push) Blocked by required conditions
CICD / Test all features separately (push) Blocked by required conditions
CICD / Build/SELinux (push) Blocked by required conditions
CICD / Run benchmarks (CodSpeed) (push) Blocked by required conditions
GnuTests / Aggregate GNU test results (push) Blocked by required conditions
GnuTests / Run GNU tests (native) (push) Waiting to run
GnuTests / Run GNU tests (SELinux) (push) Waiting to run
Android / Test builds (push) Waiting to run
Code Quality / Style/format (push) Waiting to run
Code Quality / Style/lint (push) Waiting to run
Code Quality / Style/spelling (push) Waiting to run
Code Quality / Style/toml (push) Waiting to run
FreeBSD / Style and Lint (push) Waiting to run
FreeBSD / Tests (push) Waiting to run
Code Quality / Style/Python (push) Waiting to run
Code Quality / Pre-commit hooks (push) Waiting to run
Devcontainer / Verify devcontainer (push) Waiting to run
WSL2 / Test (push) Waiting to run

l10n: replace the hash table by a long match per file to faciliate th…
This commit is contained in:
Daniel Hofstetter 2025-09-25 11:09:48 +02:00 committed by GitHub
commit 5efafd6563
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
2 changed files with 21 additions and 28 deletions

View file

@ -18,16 +18,15 @@ pub fn main() -> Result<(), Box<dyn std::error::Error>> {
"// This file contains embedded English locale files"
)?;
writeln!(embedded_file)?;
writeln!(embedded_file, "use std::collections::HashMap;")?;
// No imports needed for match-based lookup
writeln!(embedded_file)?;
// Start the function that returns embedded locales
// Generate optimized lookup function instead of HashMap
writeln!(
embedded_file,
"pub fn get_embedded_locales() -> HashMap<&'static str, &'static str> {{"
"pub fn get_embedded_locale(key: &str) -> Option<&'static str> {{"
)?;
writeln!(embedded_file, " let mut locales = HashMap::new();")?;
writeln!(embedded_file)?;
writeln!(embedded_file, " match key {{")?;
// Try to detect if we're building for a specific utility by checking build configuration
// This attempts to identify individual utility builds vs multicall binary builds
@ -44,8 +43,8 @@ pub fn main() -> Result<(), Box<dyn std::error::Error>> {
}
}
writeln!(embedded_file)?;
writeln!(embedded_file, " locales")?;
writeln!(embedded_file, " _ => None,")?;
writeln!(embedded_file, " }}")?;
writeln!(embedded_file, "}}")?;
embedded_file.flush()?;
@ -127,12 +126,11 @@ fn embed_single_utility_locale(
if locale_path.exists() {
let content = fs::read_to_string(&locale_path)?;
writeln!(embedded_file, " // Locale for {util_name}")?;
writeln!(embedded_file, " // Locale for {util_name}")?;
writeln!(
embedded_file,
" locales.insert(\"{util_name}/en-US.ftl\", r###\"{content}\"###);"
" \"{util_name}/en-US.ftl\" => Some(r###\"{content}\"###),"
)?;
writeln!(embedded_file)?;
// Tell Cargo to rerun if this file changes
println!("cargo:rerun-if-changed={}", locale_path.display());
@ -142,10 +140,10 @@ fn embed_single_utility_locale(
let uucore_locale_path = project_root.join("src/uucore/locales/en-US.ftl");
if uucore_locale_path.exists() {
let content = fs::read_to_string(&uucore_locale_path)?;
writeln!(embedded_file, " // Common uucore locale")?;
writeln!(embedded_file, " // Common uucore locale")?;
writeln!(
embedded_file,
" locales.insert(\"uucore/en-US.ftl\", r###\"{content}\"###);"
" \"uucore/en-US.ftl\" => Some(r###\"{content}\"###),"
)?;
println!("cargo:rerun-if-changed={}", uucore_locale_path.display());
}
@ -185,12 +183,11 @@ fn embed_all_utility_locales(
let locale_path = src_uu_dir.join(util_name).join("locales/en-US.ftl");
if locale_path.exists() {
let content = fs::read_to_string(&locale_path)?;
writeln!(embedded_file, " // Locale for {util_name}")?;
writeln!(embedded_file, " // Locale for {util_name}")?;
writeln!(
embedded_file,
" locales.insert(\"{util_name}/en-US.ftl\", r###\"{content}\"###);"
" \"{util_name}/en-US.ftl\" => Some(r###\"{content}\"###),"
)?;
writeln!(embedded_file)?;
// Tell Cargo to rerun if this file changes
println!("cargo:rerun-if-changed={}", locale_path.display());
@ -201,10 +198,10 @@ fn embed_all_utility_locales(
let uucore_locale_path = project_root.join("src/uucore/locales/en-US.ftl");
if uucore_locale_path.exists() {
let content = fs::read_to_string(&uucore_locale_path)?;
writeln!(embedded_file, " // Common uucore locale")?;
writeln!(embedded_file, " // Common uucore locale")?;
writeln!(
embedded_file,
" locales.insert(\"uucore/en-US.ftl\", r###\"{content}\"###);"
" \"uucore/en-US.ftl\" => Some(r###\"{content}\"###),"
)?;
println!("cargo:rerun-if-changed={}", uucore_locale_path.display());
}
@ -220,7 +217,7 @@ fn embed_static_utility_locales(
writeln!(
embedded_file,
" // Static utility locales for crates.io builds"
" // Static utility locales for crates.io builds"
)?;
let manifest_dir = env::var("CARGO_MANIFEST_DIR").unwrap_or_default();
@ -232,12 +229,11 @@ fn embed_static_utility_locales(
let uucore_locale_file = Path::new(&manifest_dir).join("locales/en-US.ftl");
if uucore_locale_file.is_file() {
let content = std::fs::read_to_string(&uucore_locale_file)?;
writeln!(embedded_file, " // Common uucore locale")?;
writeln!(embedded_file, " // Common uucore locale")?;
writeln!(
embedded_file,
" locales.insert(\"uucore/en-US.ftl\", r###\"{content}\"###);"
" \"uucore/en-US.ftl\" => Some(r###\"{content}\"###),"
)?;
writeln!(embedded_file)?;
}
// Collect and sort for deterministic builds
@ -255,12 +251,11 @@ fn embed_static_utility_locales(
let locale_file = entry.path().join("locales/en-US.ftl");
if locale_file.is_file() {
let content = std::fs::read_to_string(&locale_file)?;
writeln!(embedded_file, " // Locale for {util_name}")?;
writeln!(embedded_file, " // Locale for {util_name}")?;
writeln!(
embedded_file,
" locales.insert(\"{util_name}/en-US.ftl\", r###\"{content}\"###);"
" \"{util_name}/en-US.ftl\" => Some(r###\"{content}\"###),"
)?;
writeln!(embedded_file)?;
}
}
}

View file

@ -235,20 +235,18 @@ fn create_english_bundle_from_embedded(
));
}
let embedded_locales = get_embedded_locales();
let mut bundle = FluentBundle::new(vec![locale.clone()]);
bundle.set_use_isolating(false);
// First, try to load common uucore strings
let uucore_key = "uucore/en-US.ftl";
if let Some(uucore_content) = embedded_locales.get(uucore_key) {
if let Some(uucore_content) = get_embedded_locale("uucore/en-US.ftl") {
let uucore_resource = parse_fluent_resource(uucore_content)?;
bundle.add_resource_overriding(uucore_resource);
}
// Then, try to load utility-specific strings
let locale_key = format!("{util_name}/en-US.ftl");
if let Some(ftl_content) = embedded_locales.get(locale_key.as_str()) {
if let Some(ftl_content) = get_embedded_locale(&locale_key) {
let resource = parse_fluent_resource(ftl_content)?;
bundle.add_resource_overriding(resource);
}