aboutsummaryrefslogtreecommitdiff
path: root/crates/cli/src/locale.rs
blob: f9d42b6470e39511a3a5490b6dd95d6995ff6d67 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
// Locale represents the locales accepted for delivering answers on this
// tool. That is, it's not about i18n on the strings for this application. but
// rather the different translations accepted in places like
// `Word.translations`.
pub enum Locale {
    English,
    Catalan,
}

impl Locale {
    // Returns the string representation for the locale's code.
    pub fn to_code(&self) -> &str {
        match self {
            Self::English => "en",
            Self::Catalan => "ca",
        }
    }
}

impl std::fmt::Display for Locale {
    fn fmt(&self, f: &mut std::fmt::Formatter) -> std::fmt::Result {
        match self {
            Self::English => write!(f, "english"),
            Self::Catalan => write!(f, "català"),
        }
    }
}

/// Fetches the Locale object that is suitable for the current environment.
pub fn current_locale() -> Locale {
    let raw_locale = std::env::var("LC_ALL").unwrap_or("en".to_string());

    if raw_locale.starts_with("ca") {
        Locale::Catalan
    } else {
        Locale::English
    }
}