Vue-i18n
vue-i18n's $t and useI18n(), rendered in the reader's locale.
The translations are the ferrovue-i18n crate, which ferrovue holds as its module ferrovue::i18n with the i18n feature. The feature is on by default; generated code needs it once the project configures i18n.
Configuring
Point i18n at the directory of locale files, one JSON file per locale, and name the default and fallback locales:
{ "i18n": { "messages": "client/locales", "locale": "en", "fallbackLocale": "en" } }{ "greeting": "Hello {name}!", "apples": "no apples | one apple | {count} apples", "common": { "app": "ferrovue" } }The compiler parses every message with vue-i18n's own message compiler, at build time, and writes the result to i18n.rs as static tables. Nothing is parsed at run time.
The generated module
// @generated by ferrovue from locales/. Do not edit: change the locale files and run
// `ferrovue`.
//! The messages of every locale, compiled by vue-i18n's own message compiler.
use ferrovue::i18n::{Locale, Message, Part};
/// The locale a page renders in when it names none.
pub const LOCALE: &str = "en";
/// The locales a missing message falls back to, in order.
pub const FALLBACK: &[&str] = &["en"];
/// Every locale's messages.
pub static LOCALES: &[Locale] = &[
Locale::new("en", &[
("apples", Message::new(&[&[Part::Text("no apples")], &[Part::Text("one apple")], &[Part::Named("count"), Part::Text(" apples")]])),
("common.app", Message::new(&[&[Part::Text("ferrovue")]])),
("greeting", Message::new(&[&[Part::Text("Hello "), Part::Named("name"), Part::Text("!")]])),
]),
Locale::new("nl", &[
("apples", Message::new(&[&[Part::Text("geen appels")], &[Part::Text("één appel")], &[Part::Named("count"), Part::Text(" appels")]])),
("greeting", Message::new(&[&[Part::Text("Hallo "), Part::Named("name"), Part::Text("!")]])),
]),
];
/// The messages in `locale`, with the configured fallbacks: build one per request.
pub fn i18n(locale: &str) -> ferrovue::i18n::I18n {
ferrovue::i18n::I18n::new(LOCALES, locale, FALLBACK)
}(Messages are listed by key, nested keys joined with dots and sorted, so that lookup is a binary search.) Locale::new and Message::new are const, so the tables are statics, and messages written by hand are built the same way.
Rendering in a locale
Choose the locale per request, from the URL, a cookie or Accept-Language, build an I18n with the generated i18n(locale), and pass it to every component that translates. It comes after the props, the slots, the route and the stores in render's parameters.
<!-- Translated.vue -->
<script setup lang="ts">
import { useI18n } from "vue-i18n";
defineProps<{ name: string; count: number }>();
const { locale } = useI18n();
</script>
<template>
<p :lang="locale">{{ $t("greeting", { name }) }} {{ $t("apples", count) }} {{ $t("common.app") }}</p>
</template>/// Write the component's server render into `out`.
pub fn render(out: &mut String, props: &Props<'_>, fv_i18n: &fv::i18n::I18n) {
out.reserve(
22
+ props.name.len()
);
out.push_str("<p");
if fv_i18n.locale().is_empty() {
out.push_str(" lang");
} else {
out.push_str(" lang=\"");
fv::core::escape_into(out, fv_i18n.locale());
out.push('"');
}
out.push('>');
fv::core::escape_into(out, &fv_i18n.t("greeting", &fv::i18n::Args::new().with_named(&[("name", fv::i18n::Value::Str(&props.name))])));
out.push(' ');
fv::core::escape_into(out, &fv_i18n.t("apples", &fv::i18n::Args::new().with_plural(props.count)));
out.push(' ');
fv::core::escape_into(out, &fv_i18n.t("common.app", &fv::i18n::Args::new()));
out.push_str("</p>");
}
let props = translated::Props::new("Ada", 2);
let mut out = String::new();
translated::render(&mut out, &props, &i18n::i18n("nl"));
// `common.app` has no Dutch message, so it falls back to English.
assert_eq!(out, r#"<p lang="nl">Hallo Ada! 2 appels ferrovue</p>"#);
out.clear();
translated::render(&mut out, &props, &i18n::i18n("fr"));
// A locale with no messages is still the current one; every key falls back.
assert_eq!(out, r#"<p lang="fr">Hello Ada! 2 apples ferrovue</p>"#);Each $t is a call of I18n::t with an Args, built with Args::new and its with_named, with_list and with_plural, and its result is escaped like any other interpolation.
What is supported
Named ({name}) and list ({0}) values, plurals chosen by vue-i18n's default rule (t("apples", count), or a count or n value), literals ({'{'}), linked messages (@:key) with the upper, lower and capitalize modifiers, nested and flat keys, fallback locales in order, and a missing key, which renders as the key itself, as vue-i18n does. useI18n()'s locale reads I18n::locale.
The plural case is chosen as vue-i18n chooses it, from a count or n value that is a finite number, else from the plural number. A fractional count (a Float) is kept as it is: with two cases, only exactly 1 is singular, so 1.5 is plural; with three, 0 is the first, 1 the second, and 2 or more the third. A count that is NaN or infinite is passed over for the plural number. A fraction between -2 and 2, such as 1.5 or 0.5, chooses none of three or more cases, and vue-i18n throws: the render is refused with FV1414, which a generated try_render returns and render panics with (see refused at render time). A linked message that chooses no case is written as its key, as vue-i18n writes it.
A translated message is text: it is escaped when written, so a message holding <b> shows the tag as text, as {{ $t(…) }} does in Vue. v-html of a message is refused, as v-html of anything but a TrustedHtml prop is.
On the client, give vue-i18n the same locale files and the locale the server rendered in, or the hydrated text will differ.