@ -54,30 +54,41 @@ impl Lang {
pub type Context = HashMap < String , String > ;
/// Returns a PathBuf containing the translations. Does not mean
/// the translations are in there!
/// First attempts to find $FORGEBUILDI18N from env, otherwise tries
/// $HOME/.local/share/forgebuild/i18n or /usr/local/share/forgebuild/i18n
fn find_translations ( ) -> PathBuf {
match env ::var ( "FORGEBUILDI18N" ) {
Ok ( dir ) = > PathBuf ::from ( dir ) ,
Err ( _ ) = > {
//let mut path = home_dir().expect("$HOME folder not found");
let owner = get_effective_uid ( ) ;
let mut path = get_user_by_uid ( owner ) . expect ( "Failed owner profile" )
. home_dir ( ) . to_path_buf ( ) ;
path . push ( ".local/share/forgebuild/i18n" ) ;
println! ( "{}" , path . to_str ( ) . unwrap ( ) ) ;
if path . is_dir ( ) {
path
} else {
// Didn't find from env or in home directory,
// return default /usr/share/local/forgebuild/i18n
PathBuf ::from ( "/usr/share/local/forgebuild/i18n" )
}
/// Finds the JSON translation files checking in order, relative to the program path:
/// - ../../../build/i18n
/// - ../../spec/i18n
/// - $HOME/.local/share/forgebuild/i18n
/// - /usr/share/forgebuild/i18n
/// If all of the above fails, but ../../spec/ folder exists (we are in a build.rs repo)
/// attempt to inialize the spec submodule which may have been forgotten on clone
fn find_translations ( ) -> Option < PathBuf > {
let mut bindir = PathBuf ::from ( env ::args ( ) . nth ( 0 ) . expect ( "Argument 0 should contain the path to the program" ) ) ;
bindir . pop ( ) ;
let mut homedir = get_user_by_uid ( get_effective_uid ( ) ) . expect ( "Failed owner profile" ) . home_dir ( ) ;
let options = [
bindir . join ( "../../../build/i18n" ) ,
bindir . join ( "../../spec/i18n" ) ,
get_user_by_uid ( get_effective_uid ( ) ) . expect ( "Failed to get info about $USER" ) . home_dir ( ) . join ( ".local/share/forgebuild/i18n" ) ,
PathBuf ::from ( "/usr/share/forgebuild/i18n" ) ,
] ;
let mut found = false ;
for entry in options {
if entry . is_dir ( ) {
// We found a matching entry
return Some ( entry ) ;
}
}
// Maybe spec folder exists but hasn't been cloned
if bindir . join ( "../../spec/" ) . is_dir ( ) {
// TODO: Try to clone
unimplemented! ( "TODO: The spec submodule has not been cloned. We should clone it here. In the meantime, you can do it manually from the build.rs repo using git submodule init && git submodule update" ) ;
}
return None ;
}
fn load_translations_from_file ( path : & Path ) -> HashMap < String , String > {
@ -102,17 +113,15 @@ fn load_translations() -> HashMap<String, String> {
load_translations_from_file ( & path )
} ,
Lang ::Some ( lang ) = > {
let mut path = find_translations ( ) ;
if ! path . is_dir ( ) {
panic! ( "Could not find translations in {:?}" , path ) ;
}
path . push ( format! ( "{}.json" , lang ) ) ;
if ! path . is_file ( ) {
panic! ( " Could not find translation file: {:?}", path ) ;
if let Some ( i18ndir ) = find_translations ( ) {
let i18nfile = i18ndir . join ( format! ( "{}.json" , lang ) ) ;
if ! i18nfile . is_file ( ) {
panic! ("No such translation file: {:? }", i18nfile ) ;
}
return load_translations_from_file ( & i18nfile )
} else {
panic! ( " No translation folder found." ) ;
}
load_translations_from_file ( & path )
} ,
_ = > {
HashMap ::new ( )