日本語

The language of the page

Page::lang writes the page's language into <html lang>, which the browser and screen readers read to show and speak the text.

Page::lang

impl<S: Signals> Page<S> {
    pub fn lang(self, tag: impl Into<LanguageTag>) -> Self;
}

pub trait Locale {
    fn tag(self) -> LanguageTag;
    // ...
}

Page::lang writes tag into the lang attribute of <html>. It takes a LanguageTag, or an IntlLocale, which converts into one. In a page whose route holds the language, the tag comes from Locale::tag, which returns the string serde writes for the variant, the same text as the route's first segment:

// `Lang` is a `Locale` enum with the variants `En` and `Ja`.
use hypersig::{IntlLocale, LanguageTag, Page, Signals};

#[derive(Signals, Serialize, Deserialize)]
struct Note {}

let html = Page::new("ノート", Note {}).lang(Lang::Ja.tag()).render();
assert!(html.starts_with(r#"<!doctype html><html lang="ja">"#));

// A tag written in the program, checked when it compiles.
const PT_BR: LanguageTag = LanguageTag::new("pt-BR");
let html = Page::new("Nota", Note {}).lang(PT_BR).render();
assert!(html.starts_with(r#"<!doctype html><html lang="pt-BR">"#));

// The locale the page writes its numbers and dates in.
let html = Page::new("Note", Note {}).lang(IntlLocale::En).render();
assert!(html.starts_with(r#"<!doctype html><html lang="en">"#));

The first of these renders:

<!doctype html><html lang="ja"><head>…</head><body>…</body></html>

A page without lang has no lang attribute, and the browser guesses the language from the text.

What the browser does with it

  • A screen reader picks the voice and the pronunciation rules of the language.
  • The browser picks fonts and glyphs for the language. Kanji shared by Japanese and Chinese take their Japanese shapes under lang="ja".
  • hyphens: auto hyphenates words by the language's rules, and does nothing on a page without lang.

What a LanguageTag checks

A LanguageTag has the shape of a BCP 47 tag: subtags of one to eight ASCII letters and digits, separated by -, the first of two, three, or five to eight letters. ja, en-US, and pt-BR pass. en_US, which the browser does not read as a language, does not. Page::lang takes no &str, so a tag written in the program goes through LanguageTag::new, whose panic in a const is E0080:

use hypersig::LanguageTag;

// error[E0080]: evaluation panicked: a subtag holds only ASCII letters and
// digits, separated by `-`
const EN_US: LanguageTag = LanguageTag::new("en_US");

Only the shape is checked. Whether the language exists in the IANA registry is not, so xx passes.

A segment that is not a tag

Locale::tag reads the tag at run time, from the string serde writes, so the compiler does not see it. A variant renamed to a string that is not shaped as a tag compiles, serves its pages at that segment, and panics when tag reads it:

use hypersig_i18n::Locale;

#[derive(Segment, Serialize, Deserialize, Clone, Copy, PartialEq, Eq, Debug)]
enum Lang {
    #[serde(rename = "en_US")]
    EnUs,
}

impl Locale for Lang {
    type Text<T> = T;

    fn pick<T>(self, text: T) -> T {
        text
    }
}

// Panics: a locale's segment is its language tag: `en_US` is not a BCP 47
// language tag
let tag = Lang::EnUs.tag();

In a page handler the panic happens on every request for that language, and each request gets an empty 500 Internal Server Error. Name each variant with its tag, through rename_all = "lowercase" or #[serde(rename = "pt-BR")].

The check in a debug build

In a debug build, Page::render panics where lang names the language of an IntlLocale and the page formats its plurals and dates only in other IntlLocales, as a page with lang="ja" that formats only with IntlLocale::En does. A release build skips the check. Plurals, numbers, and dates shows how to pick the IntlLocale from the page's language.

Next