@@ -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 {
@@ -287,16 +291,71 @@ pub fn setup_localization(p: &str) -> Result<(), LocalizationError> {
287
291
LanguageIdentifier :: from_str ( DEFAULT_LOCALE ) . expect ( "Default locale should always be valid" )
288
292
} ) ;
289
293
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) ?;
297
295
init_localization ( & locale, & locales_dir)
298
296
}
299
297
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
+
300
359
#[ cfg( test) ]
301
360
mod tests {
302
361
use super :: * ;
0 commit comments