Typed translations
We’ll ask the Journal for the same missing page twice. Watch the sign-in link in the navigation. The first request asks for English and the second for Canadian French.
Ask for the page in two languages
From the checkout, start the Journal with scripts/bwf dev. In a second terminal, run the two requests below and compare the sign-in links in the returned HTML.
curl -i -H 'Accept-Language: en' http://127.0.0.1:18088/introuvable
curl -i -H 'Accept-Language: fr-CA' http://127.0.0.1:18088/introuvable
The second request asks for fr-CA. In examples/canonical/RouteBindings.bend, Bwf.I18n.preferred can choose fr because that is in the catalog. The layout calls Messages.sign_in(locale). Look for lang="fr" and “Se connecter.” The response varies by Accept-Language and Accept.
Write the zero, one and other cases
Create messages.mf2.json next to the application entry. If you’d like to see how the Journal organizes its messages, open examples/canonical/messages.mf2.json alongside your file.
{
"default": "en",
"locales": {
"en": {
"article_count": ".input {$count :integer}\n.match $count\n0 {{No articles}}\none {{One article}}\n* {{{$count} articles}}"
},
"fr": {
"article_count": ".input {$count :integer}\n.match $count\n0 {{Aucun article}}\none {{Un article}}\n* {{{$count} articles}}"
}
}
}
.input declares the message argument. .match tries the exact value 0 first. one is a plural category for the selected language. * handles anything else.
Pass the locale and count from BSX
Put this view beside the catalog so the import below resolves. Call the generated message function with the reader’s locale and the count as a Nat. What comes back is a string. Insert it in BSX just like any other text and the view will escape it.
import Base
import Bwf.Html as Html
import Bwf.Text as Text
import ./messages.mf2.json as Messages
def summary(+locale: Messages.Locale, count: Nat) -> Html.Html:
<p lang={Messages.locale_code(locale)}>{Messages.article_count(locale, count)}</p>Try a broken catalog in a scratch copy
Run npm ci in the checkout, then scripts/bwf check --entry ArticleCount.bsx. Change $count to $total in the French message and run the check again. It reports a mismatch with the default locale. Restore the name and pass "3" in the view to see a Bend argument error.
The supported MessageFormat 2 templates and CLDR plural rules are compiled into Bend. The native server and the Bun server call those generated functions.