Skip to main content

Catalog

Struct Catalog 

Source
pub struct Catalog { /* private fields */ }
Expand description

All locales of an app, parsed: one table of dotted keys per locale code, the first being the default.

Built by locales! inside a Locales static. Files that fail to parse are empty (so every key falls back to the default locale) and listed in errors, which are also logged to the Worker logs. locale gives translations outside requests; in handlers, the I18n extractor picks the locale.

§Examples

use std::sync::LazyLock;
use ocre::i18n::{Catalog, Locales};

static LOCALES: Locales = LazyLock::new(|| {
    Catalog::load(&[("en", "en:\n  hello: \"Hello %{name}\"\n"), ("fr", "fr:\n  hello: \"Bonjour %{name}\"\n")])
});

assert_eq!(LOCALES.default_locale(), "en");
assert_eq!(LOCALES.codes().collect::<Vec<_>>(), ["en", "fr"]);
assert_eq!(LOCALES.locale("fr").t("hello").arg("name", "Ada").to_string(), "Bonjour Ada");

Implementations§

Source§

impl Catalog

Source

pub fn load(sources: &[(&'static str, &'static str)]) -> Self

Parses (code, file contents) pairs into a catalog; the first pair is the default locale.

Each file must start with its code as the root key (en:); plural rules come from the code (fr-CH uses French rules). A file that does not parse becomes an empty table and one entry in errors; each error is logged. An empty sources gives an empty en catalog with an error telling to declare locales. Never fails: missing translations show up at lookup time instead.

§Examples
let catalog = ocre::i18n::Catalog::load(&[("en", "en:\n  title: Posts\n")]);
assert!(catalog.errors().is_empty());
Source

pub fn errors(&self) -> &[String]

Parse errors from load, one line each (locales/fr.yml line 3: ...).

Empty when every file parsed. ocre dev and ocre deploy check the same files before building, so a deployed app normally has none.

§Examples
let catalog = ocre::i18n::Catalog::load(&[("en", "en:\n  title: [Posts]\n")]);
assert_eq!(catalog.errors().len(), 1);
assert!(catalog.errors()[0].starts_with("locales/en.yml line 2: "));
Source

pub fn default_locale(&self) -> &'static str

Returns the default locale code: the first code given to locales!.

Keys missing from another locale fall back to it in release builds, and requests without a usable locale get it.

§Examples
let catalog = ocre::i18n::Catalog::load(&[("fr", "fr:\n"), ("en", "en:\n")]);
assert_eq!(catalog.default_locale(), "fr");
Source

pub fn codes(&self) -> impl Iterator<Item = &'static str> + '_

Returns every locale code, default first, e.g. for a language switcher.

§Examples
let catalog = ocre::i18n::Catalog::load(&[("en", "en:\n"), ("pt-BR", "pt-BR:\n")]);
assert_eq!(catalog.codes().collect::<Vec<_>>(), ["en", "pt-BR"]);
Source

pub fn locale(&'static self, code: &str) -> I18n

Returns translations in locale code, for code outside requests such as mailers and jobs.

code matches case-insensitively and exactly (fr-CH does not match fr here, unlike Accept-Language); an unknown code gives the default locale. Requires a 'static catalog, i.e. a Locales static.

§Examples
use std::sync::LazyLock;
static LOCALES: ocre::i18n::Locales = LazyLock::new(|| {
    ocre::i18n::Catalog::load(&[("en", "en:\n  subject: Welcome\n"), ("fr", "fr:\n  subject: Bienvenue\n")])
});

let user_locale = "FR";
assert_eq!(LOCALES.locale(user_locale).t("subject").to_string(), "Bienvenue");
assert_eq!(LOCALES.locale("de").locale(), "en");

Trait Implementations§

Source§

impl Debug for Catalog

Source§

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

Formats the value using the given formatter. Read more

Auto Trait Implementations§

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> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

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