Load translations properly when forgebuild is not setup, just cloned

This commit is contained in:
southerntofu 2022-01-05 18:38:54 +01:00
parent 8f7bc18837
commit 8fa9d7dca0
4 changed files with 44 additions and 32 deletions

3
.gitmodules vendored
View File

@ -1,3 +1,6 @@
[submodule "i18n"] [submodule "i18n"]
path = i18n path = i18n
url = https://tildegit.org/southerntofu/git-build-i18n url = https://tildegit.org/southerntofu/git-build-i18n
[submodule "spec"]
path = spec
url = https://tildegit.org/forge/build

1
i18n

@ -1 +0,0 @@
Subproject commit 5fd01cce804b3863abb4f465365df8051ba86ab2

1
spec Submodule

@ -0,0 +1 @@
Subproject commit c0e3bc649743b53a19c549bb0bfd66e2b62f2866

View File

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