日本語

Links to other languages

A link to the same page in another language is a link to the same route with only the language changed, so the route checks it like any other link.

impl<P: Params, Q: Search> Route<P, Q> {
    pub fn link_with<S>(&self, args: impl IntoExpr<S, Q::Args<P>>) -> Link<S>;
}

Route::link_with takes the route's parameters and returns a Link for El::href. For a route whose first parameter is the language, the same page in another language is the same call with only the language changed: on /ja/members/7, MEMBER.link_with((&Lang::En, id)) writes href="/en/members/7". The language goes by reference, as any enum constant in a link does, because IntoExpr is implemented for &T. The route builds the path, so a link to another language cannot point at a page the route does not serve.

Listing every language

pub fn locales<L: Locale>() -> &'static [L];

locales returns every variant of the enum in declaration order, the default first. A language switcher maps over it and links each language to the page the reader is on, with the same remaining parameters. A language added to the enum appears in the switcher without a change to it:

// `Lang` is a `Locale` enum with the variants `En` and `Ja`, and `ByLang` its text struct.
use hypersig::html::{El, a, attr, li, nav, ul};
use hypersig::{Page, Route, Signals};
use hypersig_i18n::locales;

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

hypersig::routes! {
    fn pages(Router<()>);
    const MEMBER: Route<(Lang, i32)> = "/{lang}/members/{id}" => member;
}

/// A link to the member `id` in each language, the current one marked.
fn switcher(current: Lang, id: i32) -> El<Profile> {
    nav().child(ul().children(locales::<Lang>().iter().map(|&lang| {
        let tag = lang.tag();
        let link = a()
            .href(MEMBER.link_with((&lang, id)))
            .attr(attr::HREFLANG, tag.as_str())
            .attr(attr::LANG, tag.as_str())
            .text(lang.pick(ByLang { en: "English", ja: "日本語" }));
        let link = if lang == current { link.attr(attr::ARIA_CURRENT, "page") } else { link };
        li().child(link)
    })))
}

async fn member((): (), (lang, id): (Lang, i32)) -> Page<Profile> {
    let title = lang.pick(ByLang { en: "Member", ja: "メンバー" });
    Page::new(title, Profile { name: "Ada".to_owned() })
        .lang(lang.tag())
        .child(switcher(lang, id))
}

assert_eq!(
    switcher(Lang::Ja, 7).render(),
    concat!(
        r#"<nav><ul>"#,
        r#"<li><a href="/en/members/7" hreflang="en" lang="en">English</a></li>"#,
        r#"<li><a href="/ja/members/7" hreflang="ja" lang="ja" aria-current="page">日本語</a></li>"#,
        r#"</ul></nav>"#,
    ),
);

switcher(Lang::Ja, 7) writes:

<nav>
  <ul>
    <li><a href="/en/members/7" hreflang="en" lang="en">English</a></li>
    <li><a href="/ja/members/7" hreflang="ja" lang="ja" aria-current="page">日本語</a></li>
  </ul>
</nav>

link_with writes only href. The switcher adds three attributes with El::attr and the constants of html::attr:

AttributeValueWhat reads it
langThe tag of the link's language, from tagA screen reader reads 日本語 in a Japanese voice on an English page, and the browser picks Japanese glyphs for it
hreflangThe tag of the page the link opensIt states the language of the target. Browsers show nothing for it
aria-currentpage, on the link to the language the reader is inA screen reader announces the link as the current page

Each link's text is in its own language, English and 日本語, so a reader who cannot read the current page still finds their language.

What the compiler checks

The link takes the whole tuple of the route. A link that gives only the language, and so would lose the page's other parameters, is E0277:

use hypersig::Route;
use hypersig::html::a;

hypersig::routes! {
    fn pages(Router<()>);
    const MEMBER: Route<(Lang, i32)> = "/{lang}/members/{id}" => member;
}

// error[E0277]: `&Lang` is not an expression of type `(Lang, i32)` on the page
// whose signals are `_`
let link = a::<Profile>().href(MEMBER.link_with(&Lang::Ja)).text("日本語");

What stays unchecked

  • The values of El::attr are text. A misspelled aria-current value, such as pgae, is written as given, and a screen reader treats a value it does not know as true, announcing the link as current without saying it is the page.
  • Nothing checks that the remaining parameters are the reader's. A switcher that passes a fixed id instead of the page's links every language to that one member.

Next