Load translations properly when forgebuild is not setup, just cloned
This commit is contained in:
parent
8f7bc18837
commit
8fa9d7dca0
|
@ -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
|
||||
|
|
1
i18n
1
i18n
|
@ -1 +0,0 @@
|
|||
Subproject commit 5fd01cce804b3863abb4f465365df8051ba86ab2
|
|
@ -0,0 +1 @@
|
|||
Subproject commit c0e3bc649743b53a19c549bb0bfd66e2b62f2866
|
71
src/log.rs
71
src/log.rs
|
@ -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();
|
||||
/// 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();
|
||||
|
||||
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<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);
|
||||
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()
|
||||
|
|
Loading…
Reference in New Issue