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
impl Catalog
Sourcepub fn load(sources: &[(&'static str, &'static str)]) -> Self
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());Sourcepub fn errors(&self) -> &[String]
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: "));Sourcepub fn default_locale(&self) -> &'static str
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");Sourcepub fn codes(&self) -> impl Iterator<Item = &'static str> + '_
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"]);Sourcepub fn locale(&'static self, code: &str) -> I18n
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");