1//! Inspect Unix system for locale configuration
2
3use std::env;
4use super::{LanguageRange,Locale};
5
6fn tag(s: &str) -> super::Result<LanguageRange> {
7 LanguageRange::from_unix(s).or_else(|_| LanguageRange::new(lt:s))
8}
9
10// TODO: Read /etc/locale.alias
11fn tag_inv(s: &str) -> LanguageRange {
12 tag(s).unwrap_or(default:LanguageRange::invariant())
13}
14
15pub fn system_locale() -> Option<Locale> {
16 // LC_ALL overrides everything
17 if let Ok(all) = env::var("LC_ALL") {
18 if let Ok(t) = tag(all.as_ref()) {
19 return Some(Locale::from(t));
20 }
21 }
22 // LANG is default
23 let mut loc =
24 if let Ok(lang) = env::var("LANG") {
25 Locale::from(tag_inv(lang.as_ref()))
26 } else {
27 Locale::invariant()
28 };
29 // category overrides
30 for &(cat, var) in [
31 ("ctype", "LC_CTYPE"),
32 ("numeric", "LC_NUMERIC"),
33 ("time", "LC_TIME"),
34 ("collate", "LC_COLLATE"),
35 ("monetary", "LC_MONETARY"),
36 ("messages", "LC_MESSAGES"),
37 ("paper", "LC_PAPER"),
38 ("name", "LC_NAME"),
39 ("address", "LC_ADDRESS"),
40 ("telephone", "LC_TELEPHONE"),
41 ("measurement", "LC_MEASUREMENT"),
42 ].iter() {
43 if let Ok(val) = env::var(var) {
44 if let Ok(tag) = tag(val.as_ref())
45 {
46 loc.add_category(cat, &tag);
47 }
48 }
49 }
50 // LANGUAGE defines fallbacks
51 if let Ok(langs) = env::var("LANGUAGE") {
52 for i in langs.split(':') {
53 if i != "" {
54 if let Ok(tag) = tag(i) {
55 loc.add(&tag);
56 }
57 }
58 }
59 }
60 if loc.as_ref() != "" {
61 return Some(loc);
62 } else {
63 return None;
64 }
65}
66