pub struct Catalog { /* private fields */ }Expand description
All locales of an app: one table of dotted keys per locale code, the first being the default.
Built by locales! inside a Locales static. Each
file is parsed the first time one of its keys is looked up, so adding
locales costs a Worker instance nothing until a request uses them. Files
that fail to parse are empty (so every key falls back to the default
locale), logged to the Worker logs when first used and listed in
errors. 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
Builds a catalog of (code, file contents) pairs; 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). Files are
parsed on first use; one that does not parse becomes an empty table
and one entry in errors, 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 unprefixed_default(self) -> Self
pub fn unprefixed_default(self) -> Self
Serves the default locale’s pages without a locale prefix: /pricing in English, /fr/pricing in French.
By default every locale has its prefix (/en/pricing). With this
option, I18n::path, I18n::alternates and
I18n::alternate_links leave the default locale’s paths as they
are, and localized serves pages both ways. Declare it with the
locales: ocre::locales!("en", "fr"; unprefixed_default).
§Examples
use std::sync::LazyLock;
static LOCALES: ocre::i18n::Locales =
LazyLock::new(|| ocre::i18n::Catalog::load(&[("en", "en:\n"), ("fr", "fr:\n")]).unprefixed_default());
assert_eq!(LOCALES.locale("en").path("/pricing"), "/pricing");
assert_eq!(LOCALES.locale("fr").path("/pricing"), "/fr/pricing");Sourcepub fn errors(&self) -> &[String]
pub fn errors(&self) -> &[String]
Parse errors of the locale files, one line each (locales/fr.yml line 3: ...).
Parses every file not parsed yet. 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");