Skip to content

Commit 3b9d0a8

Browse files
committed
l10n: replace the hash table by a long match per file to faciliate the lookup
1 parent 5eb8144 commit 3b9d0a8

2 files changed

Lines changed: 21 additions & 28 deletions

File tree

src/uucore/build.rs

Lines changed: 19 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -18,16 +18,15 @@ pub fn main() -> Result<(), Box<dyn std::error::Error>> {
1818
"// This file contains embedded English locale files"
1919
)?;
2020
writeln!(embedded_file)?;
21-
writeln!(embedded_file, "use std::collections::HashMap;")?;
21+
// No imports needed for match-based lookup
2222
writeln!(embedded_file)?;
2323

24-
// Start the function that returns embedded locales
24+
// Generate optimized lookup function instead of HashMap
2525
writeln!(
2626
embedded_file,
27-
"pub fn get_embedded_locales() -> HashMap<&'static str, &'static str> {{"
27+
"pub fn get_embedded_locale(key: &str) -> Option<&'static str> {{"
2828
)?;
29-
writeln!(embedded_file, " let mut locales = HashMap::new();")?;
30-
writeln!(embedded_file)?;
29+
writeln!(embedded_file, " match key {{")?;
3130

3231
// Try to detect if we're building for a specific utility by checking build configuration
3332
// This attempts to identify individual utility builds vs multicall binary builds
@@ -44,8 +43,8 @@ pub fn main() -> Result<(), Box<dyn std::error::Error>> {
4443
}
4544
}
4645

47-
writeln!(embedded_file)?;
48-
writeln!(embedded_file, " locales")?;
46+
writeln!(embedded_file, " _ => None,")?;
47+
writeln!(embedded_file, " }}")?;
4948
writeln!(embedded_file, "}}")?;
5049

5150
embedded_file.flush()?;
@@ -127,12 +126,11 @@ fn embed_single_utility_locale(
127126

128127
if locale_path.exists() {
129128
let content = fs::read_to_string(&locale_path)?;
130-
writeln!(embedded_file, " // Locale for {util_name}")?;
129+
writeln!(embedded_file, " // Locale for {util_name}")?;
131130
writeln!(
132131
embedded_file,
133-
" locales.insert(\"{util_name}/en-US.ftl\", r###\"{content}\"###);"
132+
" \"{util_name}/en-US.ftl\" => Some(r###\"{content}\"###),"
134133
)?;
135-
writeln!(embedded_file)?;
136134

137135
// Tell Cargo to rerun if this file changes
138136
println!("cargo:rerun-if-changed={}", locale_path.display());
@@ -142,10 +140,10 @@ fn embed_single_utility_locale(
142140
let uucore_locale_path = project_root.join("src/uucore/locales/en-US.ftl");
143141
if uucore_locale_path.exists() {
144142
let content = fs::read_to_string(&uucore_locale_path)?;
145-
writeln!(embedded_file, " // Common uucore locale")?;
143+
writeln!(embedded_file, " // Common uucore locale")?;
146144
writeln!(
147145
embedded_file,
148-
" locales.insert(\"uucore/en-US.ftl\", r###\"{content}\"###);"
146+
" \"uucore/en-US.ftl\" => Some(r###\"{content}\"###),"
149147
)?;
150148
println!("cargo:rerun-if-changed={}", uucore_locale_path.display());
151149
}
@@ -185,12 +183,11 @@ fn embed_all_utility_locales(
185183
let locale_path = src_uu_dir.join(util_name).join("locales/en-US.ftl");
186184
if locale_path.exists() {
187185
let content = fs::read_to_string(&locale_path)?;
188-
writeln!(embedded_file, " // Locale for {util_name}")?;
186+
writeln!(embedded_file, " // Locale for {util_name}")?;
189187
writeln!(
190188
embedded_file,
191-
" locales.insert(\"{util_name}/en-US.ftl\", r###\"{content}\"###);"
189+
" \"{util_name}/en-US.ftl\" => Some(r###\"{content}\"###),"
192190
)?;
193-
writeln!(embedded_file)?;
194191

195192
// Tell Cargo to rerun if this file changes
196193
println!("cargo:rerun-if-changed={}", locale_path.display());
@@ -201,10 +198,10 @@ fn embed_all_utility_locales(
201198
let uucore_locale_path = project_root.join("src/uucore/locales/en-US.ftl");
202199
if uucore_locale_path.exists() {
203200
let content = fs::read_to_string(&uucore_locale_path)?;
204-
writeln!(embedded_file, " // Common uucore locale")?;
201+
writeln!(embedded_file, " // Common uucore locale")?;
205202
writeln!(
206203
embedded_file,
207-
" locales.insert(\"uucore/en-US.ftl\", r###\"{content}\"###);"
204+
" \"uucore/en-US.ftl\" => Some(r###\"{content}\"###),"
208205
)?;
209206
println!("cargo:rerun-if-changed={}", uucore_locale_path.display());
210207
}
@@ -220,7 +217,7 @@ fn embed_static_utility_locales(
220217

221218
writeln!(
222219
embedded_file,
223-
" // Static utility locales for crates.io builds"
220+
" // Static utility locales for crates.io builds"
224221
)?;
225222

226223
let manifest_dir = env::var("CARGO_MANIFEST_DIR").unwrap_or_default();
@@ -232,12 +229,11 @@ fn embed_static_utility_locales(
232229
let uucore_locale_file = Path::new(&manifest_dir).join("locales/en-US.ftl");
233230
if uucore_locale_file.is_file() {
234231
let content = std::fs::read_to_string(&uucore_locale_file)?;
235-
writeln!(embedded_file, " // Common uucore locale")?;
232+
writeln!(embedded_file, " // Common uucore locale")?;
236233
writeln!(
237234
embedded_file,
238-
" locales.insert(\"uucore/en-US.ftl\", r###\"{content}\"###);"
235+
" \"uucore/en-US.ftl\" => Some(r###\"{content}\"###),"
239236
)?;
240-
writeln!(embedded_file)?;
241237
}
242238

243239
// Collect and sort for deterministic builds
@@ -255,12 +251,11 @@ fn embed_static_utility_locales(
255251
let locale_file = entry.path().join("locales/en-US.ftl");
256252
if locale_file.is_file() {
257253
let content = std::fs::read_to_string(&locale_file)?;
258-
writeln!(embedded_file, " // Locale for {util_name}")?;
254+
writeln!(embedded_file, " // Locale for {util_name}")?;
259255
writeln!(
260256
embedded_file,
261-
" locales.insert(\"{util_name}/en-US.ftl\", r###\"{content}\"###);"
257+
" \"{util_name}/en-US.ftl\" => Some(r###\"{content}\"###),"
262258
)?;
263-
writeln!(embedded_file)?;
264259
}
265260
}
266261
}

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

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -235,20 +235,18 @@ fn create_english_bundle_from_embedded(
235235
));
236236
}
237237

238-
let embedded_locales = get_embedded_locales();
239238
let mut bundle = FluentBundle::new(vec![locale.clone()]);
240239
bundle.set_use_isolating(false);
241240

242241
// First, try to load common uucore strings
243-
let uucore_key = "uucore/en-US.ftl";
244-
if let Some(uucore_content) = embedded_locales.get(uucore_key) {
242+
if let Some(uucore_content) = get_embedded_locale("uucore/en-US.ftl") {
245243
let uucore_resource = parse_fluent_resource(uucore_content)?;
246244
bundle.add_resource_overriding(uucore_resource);
247245
}
248246

249247
// Then, try to load utility-specific strings
250248
let locale_key = format!("{util_name}/en-US.ftl");
251-
if let Some(ftl_content) = embedded_locales.get(locale_key.as_str()) {
249+
if let Some(ftl_content) = get_embedded_locale(&locale_key) {
252250
let resource = parse_fluent_resource(ftl_content)?;
253251
bundle.add_resource_overriding(resource);
254252
}

0 commit comments

Comments
 (0)