From 8fa9d7dca0bc2f803f5f36b6523f97881013e533 Mon Sep 17 00:00:00 2001 From: southerntofu Date: Wed, 5 Jan 2022 18:38:54 +0100 Subject: [PATCH] Load translations properly when forgebuild is not setup, just cloned --- .gitmodules | 3 +++ i18n | 1 - spec | 1 + src/log.rs | 71 ++++++++++++++++++++++++++++++----------------------- 4 files changed, 44 insertions(+), 32 deletions(-) delete mode 160000 i18n create mode 160000 spec diff --git a/.gitmodules b/.gitmodules index 4474564..529dae5 100644 --- a/.gitmodules +++ b/.gitmodules @@ -1,3 +1,6 @@ [submodule "i18n"] path = i18n url = https://tildegit.org/southerntofu/git-build-i18n +[submodule "spec"] + path = spec + url = https://tildegit.org/forge/build diff --git a/i18n b/i18n deleted file mode 160000 index 5fd01cc..0000000 --- a/i18n +++ /dev/null @@ -1 +0,0 @@ -Subproject commit 5fd01cce804b3863abb4f465365df8051ba86ab2 diff --git a/spec b/spec new file mode 160000 index 0000000..c0e3bc6 --- /dev/null +++ b/spec @@ -0,0 +1 @@ +Subproject commit c0e3bc649743b53a19c549bb0bfd66e2b62f2866 diff --git a/src/log.rs b/src/log.rs index 2e04c1f..f010aab 100644 --- a/src/log.rs +++ b/src/log.rs @@ -54,30 +54,41 @@ impl Lang { pub type Context = HashMap; -/// 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(); +/// 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 { + let mut bindir = PathBuf::from(env::args().nth(0).expect("Argument 0 should contain the path to the program")); + bindir.pop(); - 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") - } + 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 { @@ -102,17 +113,15 @@ fn load_translations() -> HashMap { load_translations_from_file(&path) }, Lang::Some(lang) => { - let mut path = find_translations(); - if !path.is_dir() { - panic!("Could not find translations in {:?}", 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."); } - path.push(format!("{}.json", lang)); - - if !path.is_file() { - panic!("Could not find translation file: {:?}", path); - } - - load_translations_from_file(&path) }, _ => { HashMap::new()