Skip to main content

layer

Function layer 

Source
pub fn layer(locales: &'static Locales) -> Extension<&'static Catalog>
Expand description

Makes the catalog available to the I18n extractor, as an axum layer.

Forces locales (parsing the files if no translation did yet) and returns an [Extension] holding it. Add it last in routes(), after the // ocre:routes marker, so it covers every route; without it, I18n fails with a 500 whose log line names this fix.

ยงExamples

use std::sync::LazyLock;
use axum::{Router, routing::get};
use ocre::{Ctx, i18n::{Catalog, I18n, Locales}};

static LOCALES: Locales = LazyLock::new(|| Catalog::load(&[("en", "en:\n  title: Posts\n")]));

async fn home(i18n: I18n) -> String {
    i18n.t("title").to_string()
}

let _app: Router<Ctx> = Router::new()
    .route("/", get(home))
    // ocre:routes
    .layer(ocre::i18n::layer(&LOCALES));