Find translations in several places

This commit is contained in:
southerntofu 2020-11-23 19:45:35 +01:00
parent 3e048747cd
commit 8d854c1d1a
1 changed files with 23 additions and 2 deletions

View File

@ -13,9 +13,30 @@ lazy_static!{
pub type Context<'a> = &'a HashMap<&'a str, &'a str>;
/// 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 home = env::var("HOME").expect("$HOME environment variable is not defined!");
let mut path = PathBuf::from(home);
path.push(".local/share/forgebuild/i18n");
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")
}
}
}
}
fn load_translations() -> HashMap<String, String> {
let folder = env::var("I18N").unwrap_or("./i18n/".to_string());
let mut path = PathBuf::from(folder);
let mut path = find_translations();
if !path.is_dir() {
panic!("Could not find translations in {:?}", path);
}