[librsvg: 38/45] api.rs: Move the UserLanguage::new constructor to here




commit 91deb9c2ca2b9af9cff25ae1500d51b54788ae39
Author: Federico Mena Quintero <federico gnome org>
Date:   Tue Aug 23 18:03:06 2022 -0500

    api.rs: Move the UserLanguage::new constructor to here
    
    Let's have the mess with deal with Unix locales right here.
    
    Part-of: <https://gitlab.gnome.org/GNOME/librsvg/-/merge_requests/731>

 src/accept_language.rs | 36 ------------------------------------
 src/api.rs             | 42 +++++++++++++++++++++++++++++++++++++++++-
 2 files changed, 41 insertions(+), 37 deletions(-)
---
diff --git a/src/accept_language.rs b/src/accept_language.rs
index d980c21f6..172c3e859 100644
--- a/src/accept_language.rs
+++ b/src/accept_language.rs
@@ -221,20 +221,6 @@ impl LanguageTags {
 }
 
 impl UserLanguage {
-    pub fn new(language: &Language) -> UserLanguage {
-        match *language {
-            Language::FromEnvironment => UserLanguage::LanguageTags(
-                LanguageTags::from_locale(&locale_from_environment())
-                    .map_err(|s| {
-                        rsvg_log!("could not convert locale to language tags: {}", s);
-                    })
-                    .unwrap_or_else(|_| LanguageTags::empty()),
-            ),
-
-            Language::AcceptLanguage(ref a) => UserLanguage::AcceptLanguage(a.clone()),
-        }
-    }
-
     pub fn any_matches(&self, tags: &LanguageTags) -> bool {
         match *self {
             UserLanguage::LanguageTags(ref language_tags) => {
@@ -247,28 +233,6 @@ impl UserLanguage {
     }
 }
 
-/// Gets the user's preferred locale from the environment and
-/// translates it to a `Locale` with `LanguageRange` fallbacks.
-///
-/// The `Locale::current()` call only contemplates a single language,
-/// but glib is smarter, and `g_get_langauge_names()` can provide
-/// fallbacks, for example, when LC_MESSAGES="en_US.UTF-8:de" (USA
-/// English and German).  This function converts the output of
-/// `g_get_language_names()` into a `Locale` with appropriate
-/// fallbacks.
-fn locale_from_environment() -> Locale {
-    let mut locale = Locale::invariant();
-
-    for name in glib::language_names() {
-        let name = name.as_str();
-        if let Ok(range) = LanguageRange::from_unix(name) {
-            locale.add(&range);
-        }
-    }
-
-    locale
-}
-
 #[cfg(test)]
 mod tests {
     use super::*;
diff --git a/src/api.rs b/src/api.rs
index 622a26fbf..7feada13f 100644
--- a/src/api.rs
+++ b/src/api.rs
@@ -17,8 +17,10 @@ use std::path::Path;
 use gio::prelude::*; // Re-exposes glib's prelude as well
 use gio::Cancellable;
 
+use locale_config::{LanguageRange, Locale};
+
 use crate::{
-    accept_language::UserLanguage,
+    accept_language::{LanguageTags, UserLanguage},
     dpi::Dpi,
     handle::{Handle, LoadOptions},
     session::Session,
@@ -309,6 +311,44 @@ pub struct IntrinsicDimensions {
     pub vbox: Option<cairo::Rectangle>,
 }
 
+/// Gets the user's preferred locale from the environment and
+/// translates it to a `Locale` with `LanguageRange` fallbacks.
+///
+/// The `Locale::current()` call only contemplates a single language,
+/// but glib is smarter, and `g_get_langauge_names()` can provide
+/// fallbacks, for example, when LC_MESSAGES="en_US.UTF-8:de" (USA
+/// English and German).  This function converts the output of
+/// `g_get_language_names()` into a `Locale` with appropriate
+/// fallbacks.
+fn locale_from_environment() -> Locale {
+    let mut locale = Locale::invariant();
+
+    for name in glib::language_names() {
+        let name = name.as_str();
+        if let Ok(range) = LanguageRange::from_unix(name) {
+            locale.add(&range);
+        }
+    }
+
+    locale
+}
+
+impl UserLanguage {
+    fn new(language: &Language) -> UserLanguage {
+        match *language {
+            Language::FromEnvironment => UserLanguage::LanguageTags(
+                LanguageTags::from_locale(&locale_from_environment())
+                    .map_err(|s| {
+                        rsvg_log!("could not convert locale to language tags: {}", s);
+                    })
+                    .unwrap_or_else(|_| LanguageTags::empty()),
+            ),
+
+            Language::AcceptLanguage(ref a) => UserLanguage::AcceptLanguage(a.clone()),
+        }
+    }
+}
+
 impl<'a> CairoRenderer<'a> {
     /// Creates a `CairoRenderer` for the specified `SvgHandle`.
     ///


[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]