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