Plurals, numbers, and dates
Plural forms and the formats of numbers and dates come from hypersig, chosen per language.
IntlLocale
#[non_exhaustive]
pub enum IntlLocale {
En,
Ja,
}IntlLocale names a language the browser's Intl writes plurals and dates in, and for which hypersig holds the same rules on the server, so the first HTML already has the text the browser would write. It has English and Japanese. It is a type of hypersig, separate from the application's Lang, so each language of Lang maps to one by a text of its own:
const INTL: ByLang<IntlLocale> = ByLang { en: IntlLocale::En, ja: IntlLocale::Ja };
let intl = lang.pick(INTL);Passing a Lang where an IntlLocale is expected is E0308:
// `Lang` and `ByLang` are the ones the Overview declares.
use hypersig::{PluralCategory, Signals};
#[derive(Signals, Serialize, Deserialize)]
struct Cart {
count: i32,
}
let lang = Lang::En;
// error[E0308]: mismatched types: expected `IntlLocale`, found `Lang`
let label = Cart::count().plural(lang, |category| match category {
PluralCategory::One => " item",
_ => " items",
});An IntlLocale converts into a LanguageTag, so Page::lang(IntlLocale::Ja) writes <html lang="ja">.
Plural forms
impl<S, T: Number> Expr<S, T> {
pub fn plural<U, L: IntoExpr<S, U>>(
&self,
locale: IntlLocale,
form: impl Fn(PluralCategory) -> L,
) -> Expr<S, U>;
}
pub enum PluralCategory { Zero, One, Two, Few, Many, Other }plural is a method of a number expression or a number signal. It returns the form that form gives for the PluralCategory of the number in locale, as Intl.PluralRules picks it. English uses One for 1 and -1 and Other for every other number. Japanese uses Other for every number, so a Japanese text needs no plural at all. form is called once per category, six times, so a match on the category names a form for each one, and the browser shows the form of the current number as the signal changes:
use hypersig::html::p;
use hypersig::{IntlLocale, Page, PluralCategory, Signals};
use serde::{Deserialize, Serialize};
#[derive(Signals, Serialize, Deserialize)]
struct Cart {
count: i32,
}
let count = Cart::count();
// `form` runs once per category, and the browser keeps the form of the current count.
let label = count.to_text().concat(count.plural(IntlLocale::En, |category| match category {
PluralCategory::One => " item",
_ => " items",
}));
let html = Page::new("Cart", Cart { count: 1 })
.lang(IntlLocale::En)
.child(p().bind_text(label))
.render();For a count of 1, the server writes 1 item into the <p> of the first HTML, and the browser changes it to 2 items when count becomes 2.
Numbers
pub const fn new() -> NumberFormat;
pub const fn digits(self, digits: u8) -> Self;
pub const fn group(self, separator: &'static str) -> Self;
pub const fn point(self, point: &'static str) -> Self;
pub const fn prefix(self, prefix: &'static str) -> Self;
pub const fn suffix(self, suffix: &'static str) -> Self;NumberFormat takes no IntlLocale. It writes a number with a fixed count of decimals, a group separator, a decimal point, and affixes, and the browser and the server write the same text whatever the visitor's locale. A format per language is a ByLang<NumberFormat>, and format takes the chosen one:
const PRICE: ByLang<NumberFormat> = ByLang {
en: NumberFormat::new().digits(2).prefix("€"),
ja: NumberFormat::new().digits(2).suffix(" ユーロ"),
};
// 1536.5 is "€1,536.50" in English and "1,536.50 ユーロ" in Japanese.A language that writes 1.234,50 €, such as German, sets .group(".").point(",").suffix(" €"). NumberFormat::write returns the same text on the server, so a test can check it. Expressions covers the rounding.
Dates and times
pub const fn date(locale: IntlLocale, style: DateStyle) -> DateFormat;
pub const fn time(locale: IntlLocale, style: TimeStyle) -> DateFormat;
pub const fn date_time(locale: IntlLocale, date: DateStyle, time: TimeStyle) -> DateFormat;
pub const fn zone(self, zone: TimeZone) -> Self;DateFormat takes an IntlLocale, so a format per language is a ByLang<DateFormat>, and format_date takes the chosen one. The browser writes the date with Intl.DateTimeFormat. For 5 January 2026:
| DateStyle | IntlLocale::En | IntlLocale::Ja |
|---|---|---|
Full | Monday, January 5, 2026 | 2026年1月5日月曜日 |
Long | January 5, 2026 | 2026年1月5日 |
Medium | Jan 5, 2026 | 2026/01/05 |
Short | 1/5/26 | 2026/01/05 |
The time zone is UTC until zone sets another, such as TimeZone::Offset(540) for Japan. TimeZone::Browser uses the zone of the visitor's browser, which the server does not know, so the first HTML has no text there and the browser writes it when the page starts. relative_to, which writes 3 minutes ago, takes an IntlLocale the same way.
The check against the page's language
In a debug build, Page::render panics where Page::lang names the language of an IntlLocale and every plural, date, and relative time on the page uses another IntlLocale. A Japanese page that writes its plurals in English is read by a screen reader in the wrong language, as happens when an IntlLocale::En sits in code both languages share:
use hypersig::html::p;
use hypersig::{IntlLocale, Page, PluralCategory, Signals};
use serde::{Deserialize, Serialize};
#[derive(Signals, Serialize, Deserialize)]
struct Inbox {
unread: i32,
}
let label = Inbox::unread().plural(IntlLocale::En, |category| match category {
PluralCategory::One => "message",
_ => "messages",
});
// panicked: the page sets `lang` to `ja` and formats its plurals and dates only in `en`, ...
let _ = Page::new("受信箱", Inbox { unread: 3 })
.lang(IntlLocale::Ja)
.child(p().bind_text(label))
.render();- Only the first subtag of the tag is compared, so
langset toen-GBmatchesIntlLocale::En. - A page that also formats in its own language passes, so a Japanese page may show an English date beside its Japanese ones.
- A page in a language no
IntlLocalewrites, such asfr, passes, because it has to format in some other one. - A page without
lang, or without any plural, date, or relative time, is not checked.NumberFormattakes no locale and is never counted. - A release build skips the check and renders the page as written.
What stays unchecked
- The words of each plural form.
PluralCategory::One => " items"compiles and shows1 items. - Which
IntlLocaleaLangmaps to, beyond the debug check above.ja: IntlLocale::Enin theByLang<IntlLocale>compiles, and in a release build the Japanese page shows English dates. - The browser's copy of the Unicode CLDR data. A browser whose version differs from the server's rules in a space or a comma replaces the server's date text when the page starts.
Example
An inbox that shows the unread count, the credit left, and the date of the last update in the page's language:
// `Lang` and `ByLang` are the ones the Overview declares.
use hypersig::html::p;
use hypersig::{
DateFormat, DateStyle, Expr, IntlLocale, NumberFormat, Page, PluralCategory, Signals,
Timestamp,
};
#[derive(Signals, Serialize, Deserialize)]
struct Inbox {
unread: i32,
credit: f64,
updated: Timestamp,
}
const PRICE: ByLang<NumberFormat> = ByLang {
en: NumberFormat::new().digits(2).prefix("€"),
ja: NumberFormat::new().digits(2).suffix(" ユーロ"),
};
const UPDATED: ByLang<DateFormat> = ByLang {
en: DateFormat::date(IntlLocale::En, DateStyle::Long),
ja: DateFormat::date(IntlLocale::Ja, DateStyle::Long),
};
fn unread(lang: Lang) -> Expr<Inbox, String> {
let count = Inbox::unread().to_text();
lang.pick(ByLang {
// English has one form for 1 and another for every other number.
en: count.concat(Inbox::unread().plural(IntlLocale::En, |category| match category {
PluralCategory::One => " unread message",
_ => " unread messages",
})),
// Japanese has one form for every number.
ja: count.concat(" 件の未読メッセージ"),
})
}
fn inbox(lang: Lang, inbox: Inbox) -> Page<Inbox> {
Page::new(lang.pick(ByLang { en: "Inbox", ja: "受信箱" }), inbox)
.lang(lang.tag())
.child(p().bind_text(unread(lang)))
.child(p().bind_text(Inbox::credit().format(&lang.pick(PRICE))))
.child(p().bind_text(Inbox::updated().format_date(&lang.pick(UPDATED))))
}
// 5 January 2026, 00:00 UTC.
let updated = Timestamp::from_millis(1_767_571_200_000);
let en = inbox(Lang::En, Inbox { unread: 1, credit: 1536.5, updated }).render();
let ja = inbox(Lang::Ja, Inbox { unread: 3, credit: 1536.5, updated }).render();The server writes this text into the three <p> elements of the first HTML, in English and in Japanese:
1 unread message
€1,536.50
January 5, 2026
3 件の未読メッセージ
1,536.50 ユーロ
2026年1月5日Next, Pages in each language covers putting the language in each page's route.