Skip to content

Commit 888542f

Browse files
committed
locale: try to guess where the locale files are
1 parent d80cc2e commit 888542f

File tree

1 file changed

+66
-7
lines changed

1 file changed

+66
-7
lines changed

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

Lines changed: 66 additions & 7 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 {
@@ -287,16 +291,71 @@ pub fn setup_localization(p: &str) -> Result<(), LocalizationError> {
287291
LanguageIdentifier::from_str(DEFAULT_LOCALE).expect("Default locale should always be valid")
288292
});
289293

290-
let coreutils_path = PathBuf::from(format!("src/uu/{p}/locales/"));
291-
let locales_dir = if coreutils_path.exists() {
292-
coreutils_path
293-
} else {
294-
PathBuf::from(p)
295-
};
296-
294+
let locales_dir = get_locales_dir(p)?;
297295
init_localization(&locale, &locales_dir)
298296
}
299297

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

0 commit comments

Comments
 (0)