Commit 08dbe113 authored by Bilal Elmoussaoui's avatar Bilal Elmoussaoui

i18n: use named variables

Fixes #32
parent 451ca975
...@@ -328,6 +328,7 @@ dependencies = [ ...@@ -328,6 +328,7 @@ dependencies = [
"libadwaita", "libadwaita",
"log", "log",
"pretty_env_logger", "pretty_env_logger",
"regex",
] ]
[[package]] [[package]]
......
...@@ -11,4 +11,4 @@ log = "0.4" ...@@ -11,4 +11,4 @@ log = "0.4"
gettext-rs = { version = "0.7", features = ["gettext-system"] } gettext-rs = { version = "0.7", features = ["gettext-system"] }
adw = {package = "libadwaita", version = "0.1"} adw = {package = "libadwaita", version = "0.1"}
pretty_env_logger = "0.4" pretty_env_logger = "0.4"
regex = "1.5"
// based on https://gitlab.gnome.org/World/podcasts/-/blob/master/podcasts-gtk/src/i18n|utils.rs // based on https://gitlab.gnome.org/World/podcasts/-/blob/master/podcasts-gtk/src/i18n|utils.rs
use gettextrs::gettext; use gettextrs::gettext;
use gtk::{gio, glib}; use gtk::{gio, glib};
use regex::{Captures, Regex};
pub fn action<T, F>(thing: &T, name: &str, action: F) pub fn action<T, F>(thing: &T, name: &str, action: F)
where where
...@@ -15,12 +16,14 @@ where ...@@ -15,12 +16,14 @@ where
thing.add_action(&act); thing.add_action(&act);
} }
pub fn i18n_f(format: &str, args: &[&str]) -> String { pub fn i18n_f(format: &str, kwargs: &[(&str, &str)]) -> String {
let s = gettext(format); let mut s = gettext(format);
let mut parts = s.split("{}"); for (k, v) in kwargs {
let mut output = parts.next().unwrap_or_default().to_string(); if let Ok(re) = Regex::new(&format!("\\{{{}\\}}", k)) {
for (p, a) in parts.zip(args.iter()) { s = re
output += &(a.to_string() + &p.to_string()); .replace_all(&s, |_: &Captures<'_>| v.to_string())
.to_string();
} }
output }
s
} }
...@@ -75,7 +75,10 @@ mod imp { ...@@ -75,7 +75,10 @@ mod imp {
let name = glib::os_info("NAME").unwrap_or_else(|| "GNOME".into()); let name = glib::os_info("NAME").unwrap_or_else(|| "GNOME".into());
let version = glib::os_info("VERSION").unwrap_or_else(|| "".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 // 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( let welcome_page = ImagePageWidget::new(
"/org/gnome/Tour/welcome.svg", "/org/gnome/Tour/welcome.svg",
gettext("Start the Tour"), gettext("Start the Tour"),
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment