Texts with values
A text that takes values is a function per language, so its arguments have the same types in every translation.
A function per language
const PAGE_OF: ByLang<fn(u32, u32) -> String> = ByLang { en: |page, total| ..., ja: |page, total| ... };
lang.pick(PAGE_OF)(2, 5) // StringA text that puts values inside it is a ByLang of fn pointers, one function per language. pick returns the function of the language, and calling it formats the text. Each translation places the values where its language puts them, so Page 2 of 5 and 5 ページ中 2 ページ目 take the same arguments in a different order:
// `Lang` and `ByLang` are the ones the Overview declares.
// Each translation puts the values where its language puts them.
const PAGE_OF: ByLang<fn(u32, u32) -> String> = ByLang {
en: |page, total| format!("Page {page} of {total}"),
ja: |page, total| format!("{total} ページ中 {page} ページ目"),
};
const REPLIED: ByLang<fn(&str, &str) -> String> = ByLang {
en: |user, post| format!("{user} replied to “{post}”"),
ja: |user, post| format!("{user} さんが「{post}」に返信しました"),
};
assert_eq!(Lang::En.pick(PAGE_OF)(2, 5), "Page 2 of 5");
assert_eq!(Lang::Ja.pick(PAGE_OF)(2, 5), "5 ページ中 2 ページ目");
assert_eq!(Lang::Ja.pick(REPLIED)("Ada", "Release notes"), "Ada さんが「Release notes」に返信しました");Only the chosen function runs, so the other translations are never formatted. A text written as ByLang { en: format!(..), ja: format!(..) } formats every translation before pick drops all but one.
Arguments typed alike in every translation
The type fn(u32, u32) -> String belongs to the whole text, so every translation takes the same arguments of the same types, and every caller passes them. A translation that takes an i32 where the text says u32 is E0308:
// `Lang` and `ByLang` are the ones the Overview declares.
const ITEMS: ByLang<fn(u32) -> String> = ByLang {
en: |n| format!("{n} items"),
// error[E0308]: mismatched types
ja: |n: i32| format!("{n} 件"),
};The call is checked once for every language: lang.pick(PAGE_OF)("2", 5) is E0308 whichever language lang holds.
Why the type is written
Each closure has a type of its own, so two closures cannot be the one T of a ByLang without the type written out. Left to inference, the second closure is E0308:
// `Lang` and `ByLang` are the ones the Overview declares.
let items = ByLang {
en: |n: u32| format!("{n} items"),
// error[E0308]: mismatched types: no two closures, even if identical, have the same type
ja: |n: u32| format!("{n} 件"),
};With ByLang<fn(u32) -> String> written, both closures coerce to the same fn pointer. Only a closure that captures nothing coerces, so a translation that reads a local variable, such as |n| format!("{n} in {city}"), is E0308 too. Pass the value as another argument instead.
What stays unchecked
- Whether a translation uses each argument. A translation that leaves out
totalcompiles and shows a sentence without it. - Two arguments of the same type in the wrong order.
|page, total|written as|total, page|in one translation compiles and swaps the numbers in that language. - Plural forms.
{n} itemsreads1 itemsfor 1. Plurals, numbers, and dates picks the form by the number.
Example
A page of an archive that shows its position, with a 404 for a page past the last:
// `Lang` and `ByLang` are the ones the Overview declares.
use hypersig::html::{h1, p};
use hypersig::{Page, Route, Signals};
#[derive(Signals, Serialize, Deserialize)]
struct Archive {}
const TITLE: ByLang<&str> = ByLang { en: "Archive", ja: "アーカイブ" };
const PAGE_OF: ByLang<fn(u32, u32) -> String> = ByLang {
en: |page, total| format!("Page {page} of {total}"),
ja: |page, total| format!("{total} ページ中 {page} ページ目"),
};
hypersig::routes! {
fn pages(Router<()>);
const ARCHIVE: Route<(Lang, u32)> = "/{lang}/archive/{page}" => archive;
}
const TOTAL: u32 = 12;
async fn archive((): (), (lang, page): (Lang, u32)) -> Option<Page<Archive>> {
(1..=TOTAL).contains(&page).then(|| {
Page::new(lang.pick(TITLE), Archive {})
.lang(lang.tag())
.child(h1().text(lang.pick(TITLE)))
.child(p().text(lang.pick(PAGE_OF)(page, TOTAL)))
})
}GET /ja/archive/3 writes <p>12 ページ中 3 ページ目</p>. The text is written once by the server; to show a number the browser changes, use the expressions of Plurals, numbers, and dates. Next, that page covers plural forms and the formats of numbers and dates.