Skip to content

Commit 406df32

Browse files
committed
locale: try to guess where the locale files are
1 parent f2963ec commit 406df32

File tree

1 file changed

+68
-9
lines changed

1 file changed

+68
-9
lines changed

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

Lines changed: 68 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,10 @@ pub enum LocalizationError {
2525
Parse(String),
2626
#[error("Bundle error: {0}")]
2727
Bundle(String),
28+
#[error("Locales directory not found: {0}")]
29+
LocalesDirNotFound(String),
30+
#[error("Path resolution error: {0}")]
31+
PathResolution(String),
2832
}
2933

3034
impl From<std::io::Error> for LocalizationError {
@@ -295,21 +299,76 @@ pub fn setup_localization(p: &str) -> Result<(), LocalizationError> {
295299
LanguageIdentifier::from_str(DEFAULT_LOCALE).expect("Default locale should always be valid")
296300
});
297301

298-
let coreutils_path = PathBuf::from(format!("src/uu/{p}/locales/"));
299-
let locales_dir = if coreutils_path.exists() {
300-
coreutils_path
301-
} else {
302-
PathBuf::from(p)
303-
};
304-
302+
let locales_dir = get_locales_dir(p)?;
305303
init_localization(&locale, &locales_dir)
306304
}
307305

308-
#[cfg(test)]
306+
/// Helper function to get the locales directory based on the build configuration
307+
/// Working in progress
308+
fn get_locales_dir(p: &str) -> Result<PathBuf, LocalizationError> {
309+
#[cfg(debug_assertions)]
310+
{
311+
// During development, use the project's locales directory
312+
let manifest_dir = env!("CARGO_MANIFEST_DIR");
313+
// from uucore path, load the locales directory from the program directory
314+
let dev_path = PathBuf::from(manifest_dir)
315+
.join(p)
316+
.join("locales");
317+
318+
if dev_path.exists() {
319+
return Ok(dev_path);
320+
}
321+
322+
// Fallback for development if the expected path doesn't exist
323+
let fallback_dev_path = PathBuf::from(manifest_dir).join(p);
324+
if fallback_dev_path.exists() {
325+
return Ok(fallback_dev_path);
326+
}
327+
328+
Err(LocalizationError::LocalesDirNotFound(format!(
329+
"Development locales directory not found at {} or {}",
330+
dev_path.display(),
331+
fallback_dev_path.display()
332+
)))
333+
}
334+
335+
#[cfg(not(debug_assertions))]
336+
{
337+
use std::env;
338+
// In release builds, look relative to executable
339+
let exe_path = env::current_exe().map_err(|e| {
340+
LocalizationError::PathResolution(format!("Failed to get executable path: {}", e))
341+
})?;
342+
343+
let exe_dir = exe_path.parent().ok_or_else(|| {
344+
LocalizationError::PathResolution("Failed to get executable directory".to_string())
345+
})?;
346+
347+
// Try the coreutils-style path first
348+
let coreutils_path = exe_dir.join("locales").join(p);
349+
if coreutils_path.exists() {
350+
return Ok(coreutils_path);
351+
}
352+
353+
// Fallback to just the parameter as a relative path
354+
let fallback_path = exe_dir.join(p);
355+
if fallback_path.exists() {
356+
return Ok(fallback_path);
357+
}
358+
359+
return Err(LocalizationError::LocalesDirNotFound(format!(
360+
"Release locales directory not found at {} or {}",
361+
coreutils_path.display(),
362+
fallback_path.display()
363+
)));
364+
}
365+
}
366+
367+
Err(LocalizationError::LocalesDirNotFound(format!(
309368
mod tests {
310369
use super::*;
311370
use std::collections::HashMap;
312-
use std::env;
371+
)))
313372
use std::fs;
314373
use std::path::PathBuf;
315374
use tempfile::TempDir;

0 commit comments

Comments
 (0)