@@ -25,6 +25,10 @@ pub enum LocalizationError {
25
25
Parse ( String ) ,
26
26
#[ error( "Bundle error: {0}" ) ]
27
27
Bundle ( String ) ,
28
+ #[ error( "Locales directory not found: {0}" ) ]
29
+ LocalesDirNotFound ( String ) ,
30
+ #[ error( "Path resolution error: {0}" ) ]
31
+ PathResolution ( String ) ,
28
32
}
29
33
30
34
impl From < std:: io:: Error > for LocalizationError {
@@ -295,21 +299,76 @@ pub fn setup_localization(p: &str) -> Result<(), LocalizationError> {
295
299
LanguageIdentifier :: from_str ( DEFAULT_LOCALE ) . expect ( "Default locale should always be valid" )
296
300
} ) ;
297
301
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) ?;
305
303
init_localization ( & locale, & locales_dir)
306
304
}
307
305
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 ! (
309
368
mod tests {
310
369
use super :: * ;
311
370
use std:: collections:: HashMap ;
312
- use std :: env ;
371
+ ) ) )
313
372
use std:: fs;
314
373
use std:: path:: PathBuf ;
315
374
use tempfile:: TempDir ;
0 commit comments