Skip to content

Internationalization

This guide covers text in other languages: how scripts are displayed and entered, the text the framework supplies, translating an application with Fluent files, dates and numbers, and right-to-left languages.

CPUI shapes and lays out text itself, on every platform:

  • Chinese, Japanese, Korean, Arabic, Hebrew, Thai and Indic scripts are displayed correctly, including emoji and mixed scripts in one line.
  • Lines wrap by the rules of each script. Chinese and Japanese break between characters, and closing punctuation never starts a line. Thai, Lao, Khmer and Myanmar break between words with the word-break feature, and between characters without it.
  • A paragraph that mixes left-to-right and right-to-left text is laid out in the correct order.
  • Text inputs accept input methods, including the composition of Chinese, Japanese and Korean.

Characters come from the system fonts. When a font lacks a character, the next font in the fallback list is used; Theming describes how to add fonts. Han characters have regional glyph forms, chosen by FontSet::cjk_language and otherwise by the system language. In a browser there are no system fonts, so the application must carry a font for every script it displays.

Widgets supply some text of their own: the buttons of an alert dialog, the edit menu, the labels of the calendar and the names that screen readers announce. This text comes from a Locale. Built-in locales:

Locale Language
Locale::en() English (default)
Locale::zh_cn() Simplified Chinese
Locale::zh_tw() Traditional Chinese
Locale::ja() Japanese
Locale::ko() Korean

Set it when the app starts, or change it at any time; every window updates on its next frame:

use cpui::{Locale, Options, Ui};
let options = Options::new("Notes").locale(Locale::zh_cn());
fn language_menu(ui: &mut Ui<'_>) {
if ui.button("日本語").clicked() {
ui.set_locale(Locale::ja());
}
}

With translations installed, the framework’s text follows their language and needs no separate setting.

Locale::for_language returns the built-in locale for a language tag such as "zh-Hant-TW", or None. Locale::system() returns the locale of the user’s preferred language, falling back to English. Use it only when the application’s own text also follows the system language; otherwise its dialogs would mix two languages.

For another language, start from a built-in locale and replace its fields. The fields are &'static str; text loaded at runtime, from a file for example, is converted once with Box::leak:

use cpui::Locale;
let mut german = Locale::en();
german.ok = "OK";
german.cancel = "Abbrechen";
german.copy = "Kopieren";
german.week_starts_on_sunday = false;
let loaded = String::from("Einfügen");
german.paste = Box::leak(loaded.into_boxed_str());

With translations installed, Translations::locale("de-DE", german) uses this locale whenever the language is German.

The application’s own text is translated with Fluent files (.ftl), behind the i18n feature:

[dependencies]
cpui = { version = "0.1.1", features = ["i18n"] }

Keep one directory per language, named by its language tag, with the same files in each. The first language is the source language: a message missing from another language falls back to it.

locales/
├─ en-US/
│ └─ app.ftl
└─ zh-CN/
└─ app.ftl

When the text grows, split it into files by area of the interface, such as files.ftl and settings.ftl, and start each message id with its area. Ids must be unique across the files of a language.

A message is an id and its text. Variables are written { $name }. A selection on a number picks the plural form of each language, and attributes hold related text such as a tooltip:

title = Notes
add-file = Add a file
.tooltip = Adds an empty file
files-selected = { $count ->
[0] No files
[one] One file
*[other] { $count } files
}
title = 笔记
add-file = 添加文件
.tooltip = 添加一个空文件
files-selected = { $count ->
[0] 没有文件
*[other] { $count } 个文件
}

Each language arranges its own sentence around the variables and uses its own plural forms; the code stays the same for all of them.

Embed the files and install them when the app starts. Installing chooses the first of the user’s preferred languages that the translations include, or the source language, and the framework’s text follows it:

use cpui::i18n::Translations;
fn translations() -> Translations {
Translations::new()
.add("en-US", include_str!("../locales/en-US/app.ftl"))
.add("zh-CN", include_str!("../locales/zh-CN/app.ftl"))
}
fn main() -> Result<(), cpui::Error> {
translations().install();
cpui::run(cpui::Options::new(cpui::t!("title").to_string()), Notes::default())
}

add takes further files for a language that already has one.

t! looks a message up in the current language. Arguments are written name = value; numbers select plural forms. "id.attribute" selects an attribute. The result goes into any widget:

use cpui::{Ui, t};
fn header(ui: &mut Ui<'_>, files: u32) {
ui.heading(t!("title"));
ui.text(t!("files-selected", count = files)).muted();
ui.button(t!("add-file")).tooltip(t!("add-file.tooltip"));
}

The text is written straight into the frame. A message without arguments allocates nothing, so t! can be called on every frame.

ui.set_language(tag) switches to the installed language that best matches tag, together with the framework’s text, in every window. cpui::i18n::languages() lists the installed languages for a language setting, and cpui::i18n::language() returns the current one:

use cpui::{Ui, t};
fn language_setting(ui: &mut Ui<'_>) {
for (tag, name) in [("en-US", "English"), ("zh-CN", "简体中文")] {
if ui.button(name).clicked() {
ui.set_language(tag);
ui.set_title(t!("title"));
}
}
}

Show each language under its own name, which is not translated.

Translations::check lists files that do not parse, messages that a language lacks or adds, and messages whose variables differ from the source language’s. A test keeps the translations complete:

#[test]
fn translations_are_complete() {
assert_eq!(translations().check(), Vec::<String>::new());
}

The i18n example in the CPUI repository (cargo run -p cpui --example i18n --features i18n) combines these parts.

  • Translate what is not displayed as well: accessible names set with .aria_label(…), image descriptions set with .alt(…) and tooltips.
  • Translate whole sentences with variables rather than joining fragments: word order differs between languages.
  • Variables are not wrapped in bidirectional isolation marks, which would end up in copied text.
  • Without the i18n feature, any approach works: widgets accept any impl Display, so text can come from another translation library or from tables in the code.

The calendar and the date picker take the names of months and weekdays, the month heading and the first day of the week from the Locale. The month heading is a pattern such as "{year}年{month}".

A Date is displayed as 2026-11-27, the ISO 8601 form, which reads the same in every language. The number input writes numbers with . as the decimal separator. Formatting numbers, currencies and dates by region in the application’s own text is left to the application.

Arabic and Hebrew text is shaped and displayed right to left, in labels, paragraphs and text inputs. The layout itself is not mirrored: rows place their children from left to right, and icons point the same way in every language. Mirroring is planned; see the roadmap.