Debian Codegraph @

podcasts-gtk/src/i18n.rs

raw symbols SCIP
1 // i18n.rs
2 //
3 // Copyright 2018 Daniel Garcia Moreno <[email protected]>
4 //
5 // This program is free software: you can redistribute it and/or modify
6 // it under the terms of the GNU General Public License as published by
7 // the Free Software Foundation, either version 3 of the License, or
8 // (at your option) any later version.
9 //
10 // This program is distributed in the hope that it will be useful,
11 // but WITHOUT ANY WARRANTY; without even the implied warranty of
12 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 // GNU General Public License for more details.
14 //
15 // You should have received a copy of the GNU General Public License
16 // along with this program. If not, see <http://www.gnu.org/licenses/>.
17 //
18 // SPDX-License-Identifier: GPL-3.0-or-later
19
20 use gettextrs::gettext;
21 use gettextrs::ngettext;
22 use regex::Captures;
23 use regex::Regex;
24
25 #[allow(dead_code)]
26 fn freplace(input: String, args: &[&str]) -> String {
27 let mut parts = input.split("{}");
28 let mut output = parts.next().unwrap_or("").to_string();
29 for (p, a) in parts.zip(args.iter()) {
30 output += &(a.to_string() + p);
31 }
32 output
33 }
34
35 #[allow(dead_code)]
36 fn kreplace(input: String, kwargs: &[(&str, &str)]) -> String {
37 let mut s = input;
38 for (k, v) in kwargs {
39 if let Ok(re) = Regex::new(&format!("\\{{{}\\}}", k)) {
40 s = re
41 .replace_all(&s, |_: &Captures<'_>| v.to_string())
42 .to_string();
43 }
44 }
45
46 s
47 }
48
49 #[allow(dead_code)]
50 pub(crate) fn i18n(format: &str) -> String {
51 gettext(format)
52 }
53
54 #[allow(dead_code)]
55 pub(crate) fn i18n_f(format: &str, args: &[&str]) -> String {
56 let s = gettext(format);
57 freplace(s, args)
58 }
59
60 #[allow(dead_code)]
61 pub(crate) fn i18n_k(format: &str, kwargs: &[(&str, &str)]) -> String {
62 let s = gettext(format);
63 kreplace(s, kwargs)
64 }
65
66 #[allow(dead_code)]
67 pub(crate) fn ni18n(single: &str, multiple: &str, number: u32) -> String {
68 ngettext(single, multiple, number)
69 }
70
71 #[allow(dead_code)]
72 pub(crate) fn ni18n_f(single: &str, multiple: &str, number: u32, args: &[&str]) -> String {
73 let s = ngettext(single, multiple, number);
74 freplace(s, args)
75 }
76
77 #[allow(dead_code)]
78 pub(crate) fn ni18n_k(
79 single: &str,
80 multiple: &str,
81 number: u32,
82 kwargs: &[(&str, &str)],
83 ) -> String {
84 let s = ngettext(single, multiple, number);
85 kreplace(s, kwargs)
86 }
87
88 #[cfg(test)]
89 mod tests {
90 use super::*;
91 #[test]
92 fn test_i18n() {
93 let out = i18n("translate1");
94 assert_eq!(out, "translate1");
95
96 let out = ni18n("translate1", "translate multi", 1);
97 assert_eq!(out, "translate1");
98 let out = ni18n("translate1", "translate multi", 2);
99 assert_eq!(out, "translate multi");
100 }
101
102 #[test]
103 fn test_i18n_f() {
104 let out = i18n_f("{} param", &["one"]);
105 assert_eq!(out, "one param");
106
107 let out = i18n_f("middle {} param", &["one"]);
108 assert_eq!(out, "middle one param");
109
110 let out = i18n_f("end {}", &["one"]);
111 assert_eq!(out, "end one");
112
113 let out = i18n_f("multiple {} and {}", &["one", "two"]);
114 assert_eq!(out, "multiple one and two");
115
116 let out = ni18n_f("singular {} and {}", "plural {} and {}", 2, &["one", "two"]);
117 assert_eq!(out, "plural one and two");
118 let out = ni18n_f("singular {} and {}", "plural {} and {}", 1, &["one", "two"]);
119 assert_eq!(out, "singular one and two");
120 }
121
122 #[test]
123 fn test_i18n_k() {
124 let out = i18n_k("{one} param", &[("one", "one")]);
125 assert_eq!(out, "one param");
126
127 let out = i18n_k("middle {one} param", &[("one", "one")]);
128 assert_eq!(out, "middle one param");
129
130 let out = i18n_k("end {one}", &[("one", "one")]);
131 assert_eq!(out, "end one");
132
133 let out = i18n_k("multiple {one} and {two}", &[("one", "1"), ("two", "two")]);
134 assert_eq!(out, "multiple 1 and two");
135
136 let out = i18n_k("multiple {two} and {one}", &[("one", "1"), ("two", "two")]);
137 assert_eq!(out, "multiple two and 1");
138
139 let out = i18n_k("multiple {one} and {one}", &[("one", "1"), ("two", "two")]);
140 assert_eq!(out, "multiple 1 and 1");
141
142 let out = ni18n_k(
143 "singular {one} and {two}",
144 "plural {one} and {two}",
145 1,
146 &[("one", "1"), ("two", "two")],
147 );
148 assert_eq!(out, "singular 1 and two");
149 let out = ni18n_k(
150 "singular {one} and {two}",
151 "plural {one} and {two}",
152 2,
153 &[("one", "1"), ("two", "two")],
154 );
155 assert_eq!(out, "plural 1 and two");
156 }
157 }