[librsvg: 4/7] element: simply get the locale from the env



commit 372d4426f3d352e088f2f4825fa2e3b65affdf20
Author: Paolo Borelli <pborelli gnome org>
Date:   Sat Mar 28 12:49:00 2020 +0100

    element: simply get the locale from the env
    
    We do not need anymore to attach it to the load options and
    propagate it all over the place.

 rsvg_internals/src/document.rs |  6 +-----
 rsvg_internals/src/element.rs  | 27 ++++++++++++++++++++++++---
 rsvg_internals/src/handle.rs   | 33 ---------------------------------
 rsvg_internals/src/node.rs     |  5 ++---
 4 files changed, 27 insertions(+), 44 deletions(-)
---
diff --git a/rsvg_internals/src/document.rs b/rsvg_internals/src/document.rs
index cda56a96..26f24ecb 100644
--- a/rsvg_internals/src/document.rs
+++ b/rsvg_internals/src/document.rs
@@ -453,11 +453,7 @@ impl DocumentBuilder {
         pbag: &PropertyBag,
         parent: Option<Node>,
     ) -> Node {
-        let node = Node::new(NodeData::new_element(
-            name,
-            pbag,
-            self.load_options.locale(),
-        ));
+        let node = Node::new(NodeData::new_element(name, pbag));
 
         if let Some(id) = node.borrow_element().get_id() {
             // This is so we don't overwrite an existing id
diff --git a/rsvg_internals/src/element.rs b/rsvg_internals/src/element.rs
index a2a6ccbe..f72618ae 100644
--- a/rsvg_internals/src/element.rs
+++ b/rsvg_internals/src/element.rs
@@ -6,7 +6,7 @@
 //! [`create_element`]: fn.create_element.html
 
 use downcast_rs::*;
-use locale_config::Locale;
+use locale_config::{LanguageRange, Locale};
 use markup5ever::{expanded_name, local_name, namespace_url, ns, QualName};
 use once_cell::sync::Lazy;
 use std::collections::{HashMap, HashSet};
@@ -579,7 +579,7 @@ static ELEMENT_CREATORS: Lazy<HashMap<&'static str, (ElementCreateFn, ElementCre
 ///
 /// [`Element`]: type.Element.html
 /// [`NonRendering`]: ../structure/struct.NonRendering.html
-pub fn create_element(name: &QualName, pbag: &PropertyBag, locale: &Locale) -> Element {
+pub fn create_element(name: &QualName, pbag: &PropertyBag) -> Element {
     let mut id = None;
     let mut class = None;
 
@@ -619,11 +619,32 @@ pub fn create_element(name: &QualName, pbag: &PropertyBag, locale: &Locale) -> E
 
     let mut element = create_fn(name, id, class);
 
-    element.set_atts(pbag, locale);
+    element.set_atts(pbag, &locale_from_environment());
 
     element
 }
 
+/// 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::get_language_names() {
+        if let Ok(range) = LanguageRange::from_unix(&name) {
+            locale.add(&range);
+        }
+    }
+
+    locale
+}
+
 #[cfg(ignore)]
 mod sizes {
     //! This module is in this file just because here we have all the imports.
diff --git a/rsvg_internals/src/handle.rs b/rsvg_internals/src/handle.rs
index b88381dc..bd0e0f46 100644
--- a/rsvg_internals/src/handle.rs
+++ b/rsvg_internals/src/handle.rs
@@ -5,8 +5,6 @@
 use std::cell::Cell;
 use std::ptr;
 
-use locale_config::{LanguageRange, Locale};
-
 use crate::allowed_url::{AllowedUrl, Href};
 use crate::bbox::BoundingBox;
 use crate::css::{Origin, Stylesheet};
@@ -30,10 +28,6 @@ pub struct LoadOptions {
 
     /// Whether to keep original (undecoded) image data to embed in Cairo PDF surfaces.
     pub keep_image_data: bool,
-
-    /// The environment's locale, used for the `<switch>` element and the `systemLanguage`
-    /// attribute.
-    locale: Locale,
 }
 
 impl LoadOptions {
@@ -43,7 +37,6 @@ impl LoadOptions {
             base_url,
             unlimited_size: false,
             keep_image_data: false,
-            locale: locale_from_environment(),
         }
     }
 
@@ -74,13 +67,8 @@ impl LoadOptions {
             base_url: Some((**base_url).clone()),
             unlimited_size: self.unlimited_size,
             keep_image_data: self.keep_image_data,
-            locale: self.locale.clone(),
         }
     }
-
-    pub fn locale(&self) -> &Locale {
-        &self.locale
-    }
 }
 
 // Keep in sync with rsvg.h:RsvgDimensionData
@@ -593,27 +581,6 @@ fn check_cairo_context(cr: &cairo::Context) -> Result<(), RenderingError> {
     }
 }
 
-/// 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::get_language_names() {
-        if let Ok(range) = LanguageRange::from_unix(&name) {
-            locale.add(&range);
-        }
-    }
-
-    locale
-}
-
 fn unit_rectangle() -> Rect {
     Rect::from_size(1.0, 1.0)
 }
diff --git a/rsvg_internals/src/node.rs b/rsvg_internals/src/node.rs
index 2fc41f30..54579fd5 100644
--- a/rsvg_internals/src/node.rs
+++ b/rsvg_internals/src/node.rs
@@ -11,7 +11,6 @@
 //! [`Node`]: ../../rctree/struct.Node.html
 //! [`NodeData`]: struct.NodeData.html
 
-use locale_config::Locale;
 use markup5ever::QualName;
 use std::cell::{Ref, RefMut};
 use std::fmt;
@@ -75,8 +74,8 @@ pub enum NodeData {
 }
 
 impl NodeData {
-    pub fn new_element(name: &QualName, pbag: &PropertyBag, locale: &Locale) -> NodeData {
-        NodeData::Element(Box::new(create_element(name, pbag, locale)))
+    pub fn new_element(name: &QualName, pbag: &PropertyBag) -> NodeData {
+        NodeData::Element(Box::new(create_element(name, pbag)))
     }
 
     pub fn new_chars() -> NodeData {


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