Skip to main content

Translation

Struct Translation 

Source
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>

Source

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");
Source

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");
Source

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");
Source

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");
Source

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>&lt;script&gt;</b>");

Trait Implementations§

Source§

impl<'a> Clone for Translation<'a>

Source§

fn clone(&self) -> Translation<'a>

Returns a duplicate of the value. Read more
1.0.0 (const: unstable) · Source§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more
Source§

impl<'a> Debug for Translation<'a>

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more
Source§

impl Display for Translation<'_>

Debug builds (ocre dev) show missing translations instead of falling back.

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more

Auto Trait Implementations§

§

impl<'a> Freeze for Translation<'a>

§

impl<'a> RefUnwindSafe for Translation<'a>

§

impl<'a> Send for Translation<'a>

§

impl<'a> Sync for Translation<'a>

§

impl<'a> Unpin for Translation<'a>

§

impl<'a> UnsafeUnpin for Translation<'a>

§

impl<'a> UnwindSafe for Translation<'a>

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
§

impl<ST, DT> CastableFrom<ST, Initialized, Initialized> for DT
where ST: ?Sized, DT: ?Sized,

§

impl<ST, DT> CastableFrom<ST, Uninit, Uninit> for DT
where ST: ?Sized, DT: ?Sized,

Source§

impl<T> CloneToUninit for T
where T: Clone,

Source§

unsafe fn clone_to_uninit(&self, dest: *mut u8)

🔬This is a nightly-only experimental API. (clone_to_uninit)
Performs copy-assignment from self to dest. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

§

impl<T> FromRef<T> for T
where T: Clone,

§

fn from_ref(input: &T) -> T

Converts to this type from a reference to the input type.
Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

§

impl<T> Read<Exclusive, BecauseExclusive> for T
where T: ?Sized,

Source§

impl<T> Same for T

Source§

type Output = T

Should always be Self
Source§

impl<T> ToOwned for T
where T: Clone,

Source§

type Owned = T

The resulting type after obtaining ownership.
Source§

fn to_owned(&self) -> T

Creates owned data from borrowed data, usually by cloning. Read more
Source§

fn clone_into(&self, target: &mut T)

Uses borrowed data to replace owned data, usually by cloning. Read more
Source§

impl<T> ToString for T
where T: Display + ?Sized,

Source§

fn to_string(&self) -> String

Converts the given value to a String. Read more
Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.
Source§

impl<S, T> Upcast<T> for S
where T: UpcastFrom<S> + ?Sized, S: ?Sized,

Source§

fn upcast(&self) -> &T
where Self: ErasableGeneric, T: Sized + ErasableGeneric<Repr = Self::Repr>,

Perform a zero-cost type-safe upcast to a wider ref type within the Wasm bindgen generics type system. Read more
Source§

fn upcast_into(self) -> T
where Self: Sized + ErasableGeneric, T: Sized + ErasableGeneric<Repr = Self::Repr>,

Perform a zero-cost type-safe upcast to a wider type within the Wasm bindgen generics type system. Read more
§

impl<V, T> VZip<V> for T
where V: MultiLane<T>,

§

fn vzip(self) -> V