aboutsummaryrefslogtreecommitdiff
path: root/crates/cli/src/locale.rs
diff options
context:
space:
mode:
Diffstat (limited to 'crates/cli/src/locale.rs')
-rw-r--r--crates/cli/src/locale.rs38
1 files changed, 38 insertions, 0 deletions
diff --git a/crates/cli/src/locale.rs b/crates/cli/src/locale.rs
new file mode 100644
index 0000000..f9d42b6
--- /dev/null
+++ b/crates/cli/src/locale.rs
@@ -0,0 +1,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
+ }
+}