Add language negotiation using `fluent-langneg`

finchie
Mar 18, 2024, 1:03 PM
HCGVXOF7P3KKS2IMGVJWI2POVOZQFPXH26YVBJZRSOYSUM4CHUBQC

Dependencies

  • [2] T6JEWQJ7 Implement converting `PosixLocale` to `icu_locid::Locale`
  • [3] LIH6JCXY Implement POSIX locale category lookup
  • [4] YNEOCYMG Create `locale-select` crate
  • [*] UKFEFT6L Create basic `Output` proc-macro
  • [*] O77KA6C4 Create `fluent_embed` crate

Change contents

  • replacement in locale_select/src/unix.rs at line 3
    [2.234][2.234:276]()
    use icu_locid::subtags::{self, Variants};
    [2.234]
    [2.276]
    use icu_locid::subtags::{self, Language, Variants};
  • edit in locale_select/src/unix.rs at line 5
    [2.321]
    [2.321]
    use crate::fetch::unix::LocaleCategory;
  • edit in locale_select/src/unix.rs at line 202
    [2.1320]
    [4.5364]
    }
    pub fn get_locales(category: LocaleCategory) -> Vec<LanguageIdentifier> {
    category
    .get_locales()
    .iter()
    .map(|locale_str| PosixLocale::from_str(locale_str))
    .map(|posix_locale| posix_locale.icu_locale())
    .filter_map(|potential_locale| potential_locale.ok())
    // TODO: is it ok to strip this posix metadata from the locale?
    .map(|locale| locale.id)
    .collect()
  • edit in locale_select/src/lib.rs at line 1
    [4.5398]
    [3.0]
    use fluent_langneg::NegotiationStrategy;
    use icu_locid::LanguageIdentifier;
  • edit in locale_select/src/lib.rs at line 6
    [4.5413]
    pub fn match_locales(
    available: &[LanguageIdentifier],
    default: &LanguageIdentifier,
    ) -> Vec<LanguageIdentifier> {
    // TODO: requesting locales should have platform-specific logic
    let requested = unix::get_locales(fetch::unix::LocaleCategory::Messages);
    let supported = fluent_langneg::negotiate_languages(
    &requested,
    &available,
    Some(&default),
    NegotiationStrategy::Matching,
    );
    // TODO: this is wasteful but avoids dealing with lifetimes for now
    supported.into_iter().cloned().collect()
    }
  • replacement in locale_select/src/fetch/unix.rs at line 3
    [3.79][3.79:106]()
    enum PosixLocaleCategory {
    [3.79]
    [3.106]
    pub enum LocaleCategory {
  • replacement in locale_select/src/fetch/unix.rs at line 18
    [3.270][3.270:297]()
    impl PosixLocaleCategory {
    [3.270]
    [3.297]
    impl LocaleCategory {
  • edit in locale_select/src/fetch/unix.rs at line 35
    [3.891][3.891:893]()
    }
  • edit in locale_select/src/fetch/unix.rs at line 36
    [3.894][3.894:921]()
    impl PosixLocaleCategory {
  • replacement in locale_select/src/fetch/unix.rs at line 44
    [3.1247][3.1247:1292]()
    fn get_locale(&self) -> Option<String> {
    [3.1247]
    [3.1292]
    pub fn get_locales(&self) -> Vec<String> {
    let mut locales = Vec::with_capacity(3);
  • replacement in locale_select/src/fetch/unix.rs at line 48
    [3.1348][3.1348:1593]()
    Some(global_locale)
    } else if let Ok(category_locale) = env::var(self.as_str()) {
    Some(category_locale)
    } else if let Ok(lang) = env::var("LANG") {
    Some(lang)
    } else {
    None
    [3.1348]
    [3.1593]
    locales.push(global_locale);
    }
    if let Ok(category_locale) = env::var(self.as_str()) {
    locales.push(category_locale);
  • edit in locale_select/src/fetch/unix.rs at line 54
    [3.1603]
    [3.1603]
    if let Ok(lang) = env::var("LANG") {
    locales.push(lang);
    }
    locales
  • replacement in locale_select/src/fetch/mod.rs at line 1
    [3.1643][3.1644:1654]()
    mod unix;
    [3.1643]
    pub mod unix;
  • edit in locale_select/Cargo.toml at line 7
    [4.5534]
    [4.5534]
    fluent-langneg = { version = "0.14.0", features = ["cldr"] }
  • edit in Cargo.lock at line 74
    [7.1637]
    [7.1637]
    name = "fluent-langneg"
    version = "0.14.0"
    source = "registry+https://github.com/rust-lang/crates.io-index"
    checksum = "0a56f2b85e609878e71a22e16b2a8c5b9de24c3d5a1c780536f3800a6a114b07"
    dependencies = [
    "icu_locid",
    "icu_locid_transform",
    ]
    [[package]]
  • edit in Cargo.lock at line 216
    [4.5715]
    [4.5715]
    "fluent-langneg",