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

i18n: use named variables

Fixes #32
parent 451ca975
......@@ -328,6 +328,7 @@ dependencies = [
"libadwaita",
"log",
"pretty_env_logger",
"regex",
]
[[package]]
......
......@@ -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"
// 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
}
......@@ -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"),
......
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