diff options
author | Federico Mena Quintero <federico@gnome.org> | 2022-08-23 18:03:06 -0500 |
---|---|---|
committer | Federico Mena Quintero <federico@gnome.org> | 2022-08-23 18:03:06 -0500 |
commit | 91deb9c2ca2b9af9cff25ae1500d51b54788ae39 (patch) | |
tree | 6c82c12ec2a2a9b7ffcb3f6c6a39f999b890f49d | |
parent | ae982ed625dcb988706fd458a8dccf2963218e15 (diff) | |
download | librsvg-91deb9c2ca2b9af9cff25ae1500d51b54788ae39.tar.gz |
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>
-rw-r--r-- | src/accept_language.rs | 36 | ||||
-rw-r--r-- | src/api.rs | 42 |
2 files changed, 41 insertions, 37 deletions
diff --git a/src/accept_language.rs b/src/accept_language.rs index d980c21f..172c3e85 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::*; @@ -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`. /// |