Skip to content

Commit

Permalink
fix: Use iso-codes package for languages
Browse files Browse the repository at this point in the history
This fixes the issue where "ber" wasn't mapped to any name, and showed
up as a blank link at the end of the language list in the installer. Now
it is listed as Berber "languages".
  • Loading branch information
ids1024 authored and jackpot51 committed Jan 28, 2021
1 parent 69731ce commit 0a5f4f7
Show file tree
Hide file tree
Showing 5 changed files with 44 additions and 166 deletions.
161 changes: 0 additions & 161 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 0 additions & 1 deletion crates/locales/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@ edition = "2018"
[dependencies]
distinst-utils = { path = "../utils/" }
gettext-rs = "0.4.4"
isolang = "1.0.0"
lazy_static = "1.4.0"
once_cell = "1.5.2"
serde = "1.0.106"
Expand Down
40 changes: 40 additions & 0 deletions crates/locales/src/iso639.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
use once_cell::sync::Lazy;
use serde_json::from_reader;
use std::collections::HashMap;
use std::fs::File;

const JSON_PATH_3: &str = "/usr/share/iso-codes/json/iso_639-3.json";
const JSON_PATH_5: &str = "/usr/share/iso-codes/json/iso_639-5.json";

#[derive(Debug, Deserialize)]
pub struct Language {
alpha_2: Option<String>,
alpha_3: String,
pub name: String,
}

impl Language {
pub fn all() -> &'static [Self] {
static LANGUAGES: Lazy<Vec<Language>> = Lazy::new(|| {
let f = File::open(JSON_PATH_3).unwrap();
let mut m: HashMap<String, Vec<Language>> = from_reader(f).unwrap();
let mut languages = m.remove("639-3").unwrap();

// Language families, like Berber, which is needed
let f = File::open(JSON_PATH_5).unwrap();
let mut m: HashMap<String, Vec<Language>> = from_reader(f).unwrap();
languages.extend(m.remove("639-5").unwrap());

languages
});
Lazy::force(&LANGUAGES).as_slice()
}

pub fn from_alpha_2(alpha_2: &str) -> Option<&'static Self> {
Self::all().iter().find(|i| i.alpha_2.as_ref().map(String::as_str) == Some(alpha_2))
}

pub fn from_alpha_3(alpha_3: &str) -> Option<&'static Self> {
Self::all().iter().find(|i| i.alpha_3 == alpha_3)
}
}
6 changes: 3 additions & 3 deletions crates/locales/src/iso_codes.rs
Original file line number Diff line number Diff line change
@@ -1,15 +1,15 @@
use super::get_default;
use gettextrs::*;
use isolang::Language;
use std::env;

use crate::iso3166_1::Country;
use crate::iso639::Language;

/// Fetch the ISO 639 name of a language code.
pub fn get_language_name(code: &str) -> Option<&'static str> {
match code.len() {
2 => Language::from_639_1(code).map(|x| x.to_name()),
3 => Language::from_639_3(code).map(|x| x.to_name()),
2 => Language::from_alpha_2(code).map(|x| x.name.as_str()),
3 => Language::from_alpha_3(code).map(|x| x.name.as_str()),
_ => None,
}
}
Expand Down
2 changes: 1 addition & 1 deletion crates/locales/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
extern crate distinst_utils as misc;
extern crate gettextrs;
extern crate isolang;
#[macro_use]
extern crate lazy_static;
#[macro_use]
Expand All @@ -12,6 +11,7 @@ extern crate serde_xml_rs;

mod i18n;
mod iso3166_1;
mod iso639;
mod iso_codes;
mod keyboard_layout;
mod main_countries;
Expand Down

0 comments on commit 0a5f4f7

Please sign in to comment.