pub struct Translation<'a> { /* private fields */ }Expand description
A translation being built by I18n::t, displayed (looked up and interpolated) when written.
Add %{name} values with arg and the plural count with
count. Displaying it looks the key up in the locale,
then (release builds only) in the default locale, and replaces each
%{name} with its value; a placeholder without a value stays as written.
A key missing everywhere, and in debug builds (ocre dev, tests) a key
missing from the locale, displays as translation missing: fr.posts.title.
The text is not escaped here: askama escapes it when it writes it.
§Examples
static LOCALES: ocre::i18n::Locales = LazyLock::new(|| {
ocre::i18n::Catalog::load(&[(
"en",
"en:\n inbox:\n zero: \"No messages, %{name}\"\n one: \"One message\"\n other: \"%{count} messages\"\n",
)])
});
let i18n = LOCALES.locale("en");
assert_eq!(i18n.t("inbox").count(0).arg("name", "Ada").to_string(), "No messages, Ada");
assert_eq!(i18n.t("inbox").count(1).to_string(), "One message");
assert_eq!(i18n.t("inbox").count(12).to_string(), "12 messages");
assert_eq!(i18n.t("nope").to_string(), "translation missing: en.nope");Implementations§
Source§impl<'a> Translation<'a>
impl<'a> Translation<'a>
Sourcepub fn arg(self, name: &'a str, value: impl Display) -> Self
pub fn arg(self, name: &'a str, value: impl Display) -> Self
Sets the value for the %{name} placeholder.
value is formatted with Display right away.
Calling it twice with the same name keeps the first value.
§Examples
static LOCALES: ocre::i18n::Locales =
LazyLock::new(|| ocre::i18n::Catalog::load(&[("en", "en:\n hi: \"Hi %{name}\"\n")]));
assert_eq!(LOCALES.locale("en").t("hi").arg("name", "Ada").to_string(), "Hi Ada");Sourcepub fn count(self, n: impl Count) -> Self
pub fn count(self, n: impl Count) -> Self
Sets the plural count: picks the form for n and fills %{count}.
The form follows the locale’s CLDR rule (French: 0 and 1 are one;
Russian: one, few, many); a zero form, when present, wins
for 0. A missing form falls back to other. An explicit
arg("count", ..) overrides the %{count} text.
§Examples
static LOCALES: ocre::i18n::Locales = LazyLock::new(|| {
ocre::i18n::Catalog::load(&[("fr", "fr:\n posts:\n one: \"%{count} article\"\n other: \"%{count} articles\"\n")])
});
assert_eq!(LOCALES.locale("fr").t("posts").count(0).to_string(), "0 article");
assert_eq!(LOCALES.locale("fr").t("posts").count(2_usize).to_string(), "2 articles");Sourcepub fn or_key(self, key: &'a str) -> Self
pub fn or_key(self, key: &'a str) -> Self
Tries another key when this one has no translation (Rails’ default: :"other.key").
Alternatives are tried in the order given, each like the first key
(locale, then default locale in release builds); a leading . is
relative to the scope.
§Examples
static LOCALES: ocre::i18n::Locales =
LazyLock::new(|| ocre::i18n::Catalog::load(&[("en", "en:\n actions:\n save: Save\n")]));
let i18n = LOCALES.locale("en");
assert_eq!(i18n.t("posts.form.save").or_key("actions.save").to_string(), "Save");Sourcepub fn or(self, text: impl Into<Cow<'a, str>>) -> Self
pub fn or(self, text: impl Into<Cow<'a, str>>) -> Self
Uses text when neither the key nor its alternatives have a translation (Rails’ default: "text").
It replaces translation missing: ... in every build (debug builds
included, where a key missing from the locale shows text rather
than the default locale’s translation). %{name} placeholders in
text are filled like in a translation.
§Examples
static LOCALES: ocre::i18n::Locales = LazyLock::new(|| ocre::i18n::Catalog::load(&[("en", "en:\n")]));
let i18n = LOCALES.locale("en");
assert_eq!(i18n.t("posts.empty").or("No posts yet, %{name}").arg("name", "Ada").to_string(), "No posts yet, Ada");Sourcepub fn html(self) -> HtmlTranslation<'a>
pub fn html(self) -> HtmlTranslation<'a>
Marks the translation as HTML: its text is written as is, the values of arg escaped.
Rails’ _html keys: for translations containing markup, such as
terms: "I accept the <a href=\"/terms\">terms</a>, %{name}". askama
writes the result without escaping it again (feature html); the
%{name} values are escaped, so user input stays text. Use it only
for keys whose text you wrote, never for text from users.
§Examples
static LOCALES: ocre::i18n::Locales = LazyLock::new(|| {
ocre::i18n::Catalog::load(&[("en", "en:\n welcome: \"Hello <b>%{name}</b>\"\n")])
});
let i18n = LOCALES.locale("en");
assert_eq!(i18n.t("welcome").arg("name", "<script>").html().to_string(), "Hello <b><script></b>");Trait Implementations§
Source§impl<'a> Clone for Translation<'a>
impl<'a> Clone for Translation<'a>
Source§fn clone(&self) -> Translation<'a>
fn clone(&self) -> Translation<'a>
1.0.0 (const: unstable) · Source§fn clone_from(&mut self, source: &Self)
fn clone_from(&mut self, source: &Self)
source. Read more