Text in other languages
A few components show or announce words of their own, such as "Close" or "No matches". They are English by default, and each is a field of a struct that a page in another language fills in.
The text structs
Every word a component needs from you is an argument: a field's label, a button's text, an icon button's name. The words a component adds by itself, such as the name of a dialog's close button, come from a struct with one public String field per word. Its Default is English, and the method that renders the component takes it by reference:
| Struct | Taken by | Fields and their English defaults |
|---|---|---|
ComboboxText | Combobox::render | list: "Suggestions", the list's name. empty: "No matches", shown when no option matches. |
DialogText | Dialog::dialog, Dialog::sheet | close: "Close", the name of the button in the corner. |
PaginationText | pagination | label: "Pagination", the <nav>'s name. previous: "Previous". next: "Next". |
TableText | DataTable, ServerTable | empty: "No results.". select_row: "Select row". select_all: "Select all rows". previous_page: "Previous page". next_page: "Next page". expand_row: "Show row details". |
Each struct derives Debug, Clone, PartialEq, and Eq. ComboboxText is declared as:
pub struct ComboboxText {
pub list: String,
pub empty: String,
}
impl Default for ComboboxText { /* "Suggestions", "No matches" */ }The words reach the page as text and as aria-label. With ComboboxText { list: "候補".into(), empty: "一致する候補はありません".into() }, the combobox renders:
<div role="listbox" aria-label="候補">…</div>
<p>一致する候補はありません</p>Words set by a method
Toaster is a builder, so it takes its two words through methods instead of a struct: label names the region in place of "Notifications", and dismiss_label names the button that removes a toast in place of "Dismiss".
impl<S> Toaster<S> {
pub fn label(self, label: impl Into<String>) -> Self
pub fn dismiss_label(self, label: impl Into<String>) -> Self
}A word left out
The fields are public and the structs have no constructor, so a translation is a struct expression that names every field. One that leaves a field out fails to compile with E0063, so a word added to a struct in a later version shows up as an error in every translation of it:
use hypersig_ui::ComboboxText;
// `empty`, the text shown when no option matches, is left out.
let japanese = ComboboxText {
list: "候補".into(),
};Writing ..ComboboxText::default() after the fields you translate fills the rest with English and turns the error off, so leave it out of a translation.
What the types leave unchecked
- Nothing ties a text struct to the page's language. A Japanese page that passes
DialogText::default()names its close button "Close" in English. - An empty string compiles. An empty
closeleaves the close button with no name, which a screen reader announces as just "button".
Example: one function per struct
A function per struct that matches on the page's language keeps every translation in one place, and the match stops compiling when a language is added without its words:
use hypersig::{DialogRef, NodeRef, Signals};
use hypersig_ui::{
ButtonSize, ButtonVariant, Combobox, ComboboxState, ComboboxText, Dialog, DialogText,
PaginationText, TableText, Toast, Toaster, dialog_footer,
};
use serde::{Deserialize, Serialize};
#[derive(Clone, Copy)]
enum Lang {
En,
Ja,
}
#[derive(Signals, Serialize, Deserialize)]
struct Shipping {
country: ComboboxState,
toasts: Vec<Toast>,
}
hypersig::node_ref!(const COUNTRY_FIELD: NodeRef<Shipping>);
hypersig::node_ref!(const COUNTRY_OPTIONS: NodeRef<Shipping>);
const COUNTRY: Combobox<Shipping> = Combobox::new(COUNTRY_FIELD, COUNTRY_OPTIONS);
hypersig::dialog_ref!(const HELP_DIALOG: DialogRef<Shipping>);
const HELP: Dialog<Shipping> = Dialog::new(HELP_DIALOG);
// Each struct has a field per word, so a translation that leaves one out
// does not compile.
fn combobox_text(lang: Lang) -> ComboboxText {
match lang {
Lang::En => ComboboxText::default(),
Lang::Ja => ComboboxText {
list: "候補".into(),
empty: "一致する候補はありません".into(),
},
}
}
fn dialog_text(lang: Lang) -> DialogText {
match lang {
Lang::En => DialogText::default(),
Lang::Ja => DialogText { close: "閉じる".into() },
}
}
fn pagination_text(lang: Lang) -> PaginationText {
match lang {
Lang::En => PaginationText::default(),
Lang::Ja => PaginationText {
label: "ページ送り".into(),
previous: "前へ".into(),
next: "次へ".into(),
},
}
}
fn table_text(lang: Lang) -> TableText {
match lang {
Lang::En => TableText::default(),
Lang::Ja => TableText {
empty: "該当する行はありません。".into(),
select_row: "行を選ぶ".into(),
select_all: "すべての行を選ぶ".into(),
previous_page: "前のページ".into(),
next_page: "次のページ".into(),
expand_row: "行の詳細を表示".into(),
},
}
}
let lang = Lang::Ja;
let country = COUNTRY.render(
"国",
Shipping::country(),
["フランス", "日本", "ケニア"],
&combobox_text(lang),
);
let help = HELP.dialog(
"配送について",
"3 営業日以内に発送します。",
dialog_footer().child(HELP.close(ButtonVariant::Default, ButtonSize::Default).text("OK")),
&dialog_text(lang),
);
// The toast region takes its two names through methods.
let toasts = Toaster::new(Shipping::toasts())
.label("通知")
.dismiss_label("閉じる");Next
Writing a text covers writing every text of a page once per language with hypersig_i18n. Combobox, Dialog, Pagination, and Data table show each struct in use.