[gnome-tour] i18n: use named variables



commit 08dbe113b8e74936cc202a0b06008aef148e6b26
Author: Bilal Elmoussaoui <belmouss redhat com>
Date:   Sat Jan 29 12:03:29 2022 +0100

    i18n: use named variables
    
    Fixes #32

 Cargo.lock               |  1 +
 Cargo.toml               |  2 +-
 src/utils.rs             | 17 ++++++++++-------
 src/widgets/paginator.rs |  5 ++++-
 4 files changed, 16 insertions(+), 9 deletions(-)
---
diff --git a/Cargo.lock b/Cargo.lock
index 47b24c2..e8aff92 100644
--- a/Cargo.lock
+++ b/Cargo.lock
@@ -328,6 +328,7 @@ dependencies = [
  "libadwaita",
  "log",
  "pretty_env_logger",
+ "regex",
 ]
 
 [[package]]
diff --git a/Cargo.toml b/Cargo.toml
index b81201b..976dc4c 100644
--- a/Cargo.toml
+++ b/Cargo.toml
@@ -11,4 +11,4 @@ log = "0.4"
 gettext-rs = { version = "0.7", features = ["gettext-system"] }
 adw = {package = "libadwaita", version = "0.1"}
 pretty_env_logger = "0.4"
-
+regex = "1.5"
diff --git a/src/utils.rs b/src/utils.rs
index 5b4fa37..b26127f 100644
--- a/src/utils.rs
+++ b/src/utils.rs
@@ -1,6 +1,7 @@
 // based on https://gitlab.gnome.org/World/podcasts/-/blob/master/podcasts-gtk/src/i18n|utils.rs
 use gettextrs::gettext;
 use gtk::{gio, glib};
+use regex::{Captures, Regex};
 
 pub fn action<T, F>(thing: &T, name: &str, action: F)
 where
@@ -15,12 +16,14 @@ where
     thing.add_action(&act);
 }
 
-pub fn i18n_f(format: &str, args: &[&str]) -> String {
-    let s = gettext(format);
-    let mut parts = s.split("{}");
-    let mut output = parts.next().unwrap_or_default().to_string();
-    for (p, a) in parts.zip(args.iter()) {
-        output += &(a.to_string() + &p.to_string());
+pub fn i18n_f(format: &str, kwargs: &[(&str, &str)]) -> String {
+    let mut s = gettext(format);
+    for (k, v) in kwargs {
+        if let Ok(re) = Regex::new(&format!("\\{{{}\\}}", k)) {
+            s = re
+                .replace_all(&s, |_: &Captures<'_>| v.to_string())
+                .to_string();
+        }
     }
-    output
+    s
 }
diff --git a/src/widgets/paginator.rs b/src/widgets/paginator.rs
index 9b2f293..7ca2161 100644
--- a/src/widgets/paginator.rs
+++ b/src/widgets/paginator.rs
@@ -75,7 +75,10 @@ mod imp {
             let name = glib::os_info("NAME").unwrap_or_else(|| "GNOME".into());
             let version = glib::os_info("VERSION").unwrap_or_else(|| "".into());
             // Translators: The following string is formated as "Learn about new and essential features in 
GNOME 3.36" for example
-            let body = i18n_f("Learn about the key features in {} {}.", &[&name, &version]);
+            let body = i18n_f(
+                "Learn about the key features in {name} {version}.",
+                &[("name", &name), ("version", &version)],
+            );
             let welcome_page = ImagePageWidget::new(
                 "/org/gnome/Tour/welcome.svg",
                 gettext("Start the Tour"),


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