日本語

Pages in each language

The language is the first segment of each page's route, so the page in another language is the same route with only the language changed.

The language in the route

A Locale is a Segment, so it can be the first parameter of a route, as in Route<(Lang, i32)> for "/{lang}/members/{id}". The handler receives the language with the other parameters, and a request for a language the enum does not have gets a 404 before the handler runs.

use hypersig::html::a;
use hypersig::{Page, Route};

#[derive(Signals, Serialize, Deserialize)]
struct Profile {
    name: String,
}

hypersig::routes! {
    fn pages(Router<()>);
    // The language is the first segment of every page.
    const MEMBER: Route<(Lang, i32)> = "/{lang}/members/{id}" => member;
}

async fn member((): (), (lang, id): (Lang, i32)) -> Page<Profile> {
    let other = if lang == Lang::En { Lang::Ja } else { Lang::En };
    Page::new(lang.pick(ByLang { en: "Member", ja: "メンバー" }), Profile { name: "Ada".to_owned() })
        .lang(lang.tag())
        // The same page in the other language is the same route with another locale.
        .child(a().href(MEMBER.link_with((&other, id))).text(other.pick(ByLang { en: "English", ja: "日本語" })))
}

assert_eq!(MEMBER.url((Lang::Ja, 7)).as_str(), "/ja/members/7");

A link to the same page in another language is made by passing link_with the other language with the same remaining parameters, so the route checks it as it checks other links. A language switcher lists Locale::ALL and links, in each language, to the page the reader is on.

The language of the page

Page::lang writes the tag into <html lang>. The browser uses it to decide hyphenation, the voice a screen reader uses, and the fonts, so Japanese text set with lang="ja" gets Japanese glyphs.

LocalePage::alternates takes an Origin and a function from a language to the page's Url, as in |lang| POST.url((lang, id)). It writes one <link rel="alternate" hreflang> per language of Locale::ALL, and an x-default at the first language, so a search engine shows each reader the page in their language.