Languages
Design system 1 owns languages for every product built on it: the list of languages, choosing and remembering one, looking words up, the shared interface words, addresses that carry a language, the server’s check and the picker. Each product keeps its own words in its own translations file, in the format below.
The list and a product’s subset
Section titled “The list and a product’s subset”There is one list, LANGUAGES (ds1/1-root/i18n.ts):
| Code | English name | Own name |
|---|---|---|
da | Danish | Dansk |
de | German | Deutsch |
en | English | English |
es | Spanish | Español |
fi | Finnish | Suomi |
fr | French | Français |
is | Icelandic | Íslenska |
it | Italian | Italiano |
ja | Japanese | 日本語 |
ko | Korean | 한국어 |
nb | Norwegian Bokmål | Norsk bokmål |
nn | Norwegian Nynorsk | Norsk nynorsk |
no | Norwegian | Norsk |
pt | Portuguese | Português |
pt-BR | Brazilian Portuguese | Português (Brasil) |
sv | Swedish | Svenska |
zh | Chinese | 中文 |
LANGUAGE_METADATA[code] holds each one’s label, nativeLabel and aliases. The file has no browser code, so a product’s backend imports it too.
A product turns on the languages it translates, once, at startup:
<code><span class="line"><span style="color:var(--docs-code-keyword)">import</span><span style="color:var(--docs-code-ink)"> { configureI18n } </span><span style="color:var(--docs-code-keyword)">from</span><span style="color:var(--docs-code-string)"> "@0001-labs/design-system-1"</span><span style="color:var(--docs-code-ink)">;</span></span><span class="line"></span><span class="line"><span style="color:var(--docs-code-tag)">configureI18n</span><span style="color:var(--docs-code-ink)">({</span></span><span class="line"><span style="color:var(--docs-code-ink)"> languages: [</span><span style="color:var(--docs-code-string)">"en"</span><span style="color:var(--docs-code-ink)">, </span><span style="color:var(--docs-code-string)">"da"</span><span style="color:var(--docs-code-ink)">, </span><span style="color:var(--docs-code-string)">"ja"</span><span style="color:var(--docs-code-ink)">],</span></span><span class="line"><span style="color:var(--docs-code-ink)"> sharedWords: </span><span style="color:var(--docs-code-number)">true</span><span style="color:var(--docs-code-ink)">,</span></span><span class="line"><span style="color:var(--docs-code-ink)">});</span></span></code>pickLanguages(codes) gives any set of codes back as members of the list, in the list’s order.
Reading a language
Section titled “Reading a language”normalizeLanguage(input, fallback = "en", languages = LANGUAGES) reads whatever a browser, an address or a saved setting calls a language: da-DK, nb_NO, pt-br, or a browser’s list ("nl-NL,sv;q=0.9", the first language it knows wins). Given the product’s languages, a relative stands in for a language the product lacks: with ["en", "nb", "pt-BR"], no and nn read as nb, and pt as pt-BR. Pass null as the fallback to tell when nothing matched. isLanguage(value) tells whether a value is a code exactly as the list writes it.
Choosing and remembering a language
Section titled “Choosing and remembering a language”<code><span class="line"><span style="color:var(--docs-code-keyword)">import</span><span style="color:var(--docs-code-ink)"> { setLanguage, currentLanguage } </span><span style="color:var(--docs-code-keyword)">from</span><span style="color:var(--docs-code-string)"> "@0001-labs/design-system-1"</span><span style="color:var(--docs-code-ink)">;</span></span><span class="line"></span><span class="line"><span style="color:var(--docs-code-tag)">setLanguage</span><span style="color:var(--docs-code-ink)">(</span><span style="color:var(--docs-code-string)">"da"</span><span style="color:var(--docs-code-ink)">);</span></span><span class="line"><span style="color:var(--docs-code-ink)">currentLanguage.value; </span><span style="color:var(--docs-code-comment)">// "da"</span></span></code>setLanguage remembers the choice in localStorage (under language, or the storageKey the product configures), sets the document’s lang, fetches the language’s words when they come a file at a time, and announces language-changed on window with { language } as its detail.
setLanguage(language, { remember: false }) changes the language for this page alone, without storing it: a preview, or a language the page was asked for.
Where lang goes
Section titled “Where lang goes”lang follows the language on the document’s root by default. A page that embeds a surface in another language than its own, such as a prototype in Japanese on a page written in English, keeps it on that surface:
<code><span class="line"><span style="color:var(--docs-code-tag)">configureI18n</span><span style="color:var(--docs-code-ink)">({ langTarget: document.</span><span style="color:var(--docs-code-tag)">querySelector</span><span style="color:var(--docs-code-ink)">(</span><span style="color:var(--docs-code-string)">".prototype"</span><span style="color:var(--docs-code-ink)">) });</span></span></code>langTarget: false writes lang nowhere. With either, the theme head script leaves the root’s lang alone too.
On a first visit the language is the browser’s, as one the product speaks: the first of the browser’s languages that fits, a relative standing in. A stored choice is read the same way, so a saved pt becomes pt-BR in a product that speaks only Brazilian Portuguese.
Translating
Section titled “Translating”<code><span class="line"><span style="color:var(--docs-code-keyword)">import</span><span style="color:var(--docs-code-ink)"> { translate } </span><span style="color:var(--docs-code-keyword)">from</span><span style="color:var(--docs-code-string)"> "@0001-labs/design-system-1"</span><span style="color:var(--docs-code-ink)">;</span></span><span class="line"></span><span class="line"><span style="color:var(--docs-code-tag)">translate</span><span style="color:var(--docs-code-ink)">(</span><span style="color:var(--docs-code-string)">"Save"</span><span style="color:var(--docs-code-ink)">); </span><span style="color:var(--docs-code-comment)">// "Gem" in Danish</span></span><span class="line"><span style="color:var(--docs-code-tag)">translate</span><span style="color:var(--docs-code-ink)">(</span><span style="color:var(--docs-code-string)">"Hello {name}"</span><span style="color:var(--docs-code-ink)">, { name: </span><span style="color:var(--docs-code-string)">"Ada"</span><span style="color:var(--docs-code-ink)"> }); </span><span style="color:var(--docs-code-comment)">// placeholders filled</span></span><span class="line"><span style="color:var(--docs-code-tag)">translate</span><span style="color:var(--docs-code-ink)">(</span><span style="color:var(--docs-code-string)">"auth.sentTo"</span><span style="color:var(--docs-code-ink)">, { email: </span><span style="color:var(--docs-code-string)">"ada@0001.dev"</span><span style="color:var(--docs-code-ink)"> }); </span><span style="color:var(--docs-code-comment)">// {{email}} too</span></span></code>translate(key, params?), getText(key, params?) and textInLanguage(key, language, params?) fill placeholders written {name} and {{name}} alike. A placeholder without a value stays as written. hasTranslation(key, language?) tells whether any table has the key.
In markup, ds-text shows a key’s text and follows the language:
<code><span class="line"><span style="color:var(--docs-code-ink)"><</span><span style="color:var(--docs-code-tag)">ds-text</span><span style="color:var(--docs-code-keyword)"> text</span><span style="color:var(--docs-code-ink)">=</span><span style="color:var(--docs-code-string)">"Save"</span><span style="color:var(--docs-code-ink)">></</span><span style="color:var(--docs-code-tag)">ds-text</span><span style="color:var(--docs-code-ink)">></span></span></code>
The fallback order
Section titled “The fallback order”A word is looked up in:
- the language itself (
ja-JP); - its primary subtag (
ja); - its relatives:
nb,noandnnfor each other,ptandpt-BRfor each other; - English (
en); - the key itself.
In each language, the product’s words come before the shared words, so a product’s own text always wins, and a shared word in the reader’s language still beats the product’s English.
The translations file format
Section titled “The translations file format”One format for every product’s words:
<code><span class="line"><span style="color:var(--docs-code-ink)">{</span></span><span class="line"><span style="color:var(--docs-code-keyword)"> "en"</span><span style="color:var(--docs-code-ink)">: { </span><span style="color:var(--docs-code-keyword)">"settings.language"</span><span style="color:var(--docs-code-ink)">: </span><span style="color:var(--docs-code-string)">"Language"</span><span style="color:var(--docs-code-ink)"> },</span></span><span class="line"><span style="color:var(--docs-code-keyword)"> "da"</span><span style="color:var(--docs-code-ink)">: { </span><span style="color:var(--docs-code-keyword)">"settings.language"</span><span style="color:var(--docs-code-ink)">: </span><span style="color:var(--docs-code-string)">"Sprog"</span><span style="color:var(--docs-code-ink)">, </span><span style="color:var(--docs-code-keyword)">"Send"</span><span style="color:var(--docs-code-ink)">: </span><span style="color:var(--docs-code-string)">"Send"</span><span style="color:var(--docs-code-ink)"> }</span></span><span class="line"><span style="color:var(--docs-code-ink)">}</span></span></code>- The top level is keyed by language code from the list.
- A key is any string: the English sentence (
"Send": "Send") or a dotted id ("settings.language"). Both are the same format, and no product’s words need rewriting. - English is the fallback table. When a key is missing from
en, the key itself is the English text: a sentence key needs no English entry; an id key needs one. - Placeholders are
{name}or{{name}}. - Sentence case everywhere, never all caps.
By default the module loads the whole table from ./translations.json (or the file translationsFile, window.DS_ONE_TRANSLATIONS_FILE or a data-ds-one-translations attribute names). A page that wants its words translated imports 1-root/i18n, or names its table with window.DS_ONE_I18N, window.DS_ONE_TRANSLATIONS, window.DS_ONE_TRANSLATIONS_FILE, a data-ds-one-translations script, a ds-one:translations meta or a ds-one-translations link. Components that only look up their own words read them through 1-root/words, which uses the translator once the module has loaded, shows the English key until then, and loads the module itself only when the page names a table, so a page without languages never downloads it.
validateTranslations(file) checks a file: errors for what cannot load (not an object of language tables, a text that is not a string), warnings for a language outside the list, an id key with no English text, and a translation whose placeholders differ from its English text’s.
Per-language files
Section titled “Per-language files”A page can load only its own language:
<code><span class="line"><span style="color:var(--docs-code-tag)">configureI18n</span><span style="color:var(--docs-code-ink)">({</span></span><span class="line"><span style="color:var(--docs-code-ink)"> languages: [</span><span style="color:var(--docs-code-string)">"en"</span><span style="color:var(--docs-code-ink)">, </span><span style="color:var(--docs-code-string)">"da"</span><span style="color:var(--docs-code-ink)">, </span><span style="color:var(--docs-code-string)">"ja"</span><span style="color:var(--docs-code-ink)">],</span></span><span class="line"><span style="color:var(--docs-code-ink)"> languageFile: </span><span style="color:var(--docs-code-string)">"/translations/{language}.json"</span><span style="color:var(--docs-code-ink)">,</span></span><span class="line"><span style="color:var(--docs-code-ink)">});</span></span></code>Each file holds one language’s table, wrapped ({ "da": { … } }) or bare ({ … }). When a language’s file is missing, the whole table is the fallback. Switching language fetches the new language’s file once.
Words in a TypeScript module
Section titled “Words in a TypeScript module”A product can ship its words as a module of the same shape and hand them over at startup. Tables may be Partial:
<code><span class="line"><span style="color:var(--docs-code-keyword)">import</span><span style="color:var(--docs-code-ink)"> { loadTranslations } </span><span style="color:var(--docs-code-keyword)">from</span><span style="color:var(--docs-code-string)"> "@0001-labs/design-system-1"</span><span style="color:var(--docs-code-ink)">;</span></span><span class="line"><span style="color:var(--docs-code-keyword)">import</span><span style="color:var(--docs-code-ink)"> { translations } </span><span style="color:var(--docs-code-keyword)">from</span><span style="color:var(--docs-code-string)"> "./i18n/translations"</span><span style="color:var(--docs-code-ink)">;</span></span><span class="line"></span><span class="line"><span style="color:var(--docs-code-tag)">loadTranslations</span><span style="color:var(--docs-code-ink)">(translations); </span><span style="color:var(--docs-code-comment)">// { en: {…}, da: {…} }</span></span><span class="line"><span style="color:var(--docs-code-tag)">loadTranslations</span><span style="color:var(--docs-code-ink)">(</span><span style="color:var(--docs-code-string)">"da"</span><span style="color:var(--docs-code-ink)">, { </span><span style="color:var(--docs-code-string)">"settings.language"</span><span style="color:var(--docs-code-ink)">: </span><span style="color:var(--docs-code-string)">"Sprog"</span><span style="color:var(--docs-code-ink)"> });</span></span></code>The shared words
Section titled “The shared words”Design system 1 carries the interface words every product shares, in ds1/1-root/translations/<language>.json, keyed by the English sentence:
- buttons: Save, Cancel, Delete, Close, Back, Next, Done, Continue, Search, Edit, Yes, No;
- actions: Try again, Undo, Redo, Copy, Copy link, Remove, Rename, Duplicate, Download, Refresh, Open, Send, Clear;
- states and names: Saved, Saving…, Untitled, Account;
- sign-in: Sign in, Sign out, Email, Password;
- empty states: Nothing here yet;
- dates: Today, Yesterday, Tomorrow;
- settings: Settings, Language, Theme, Light, Dark, Note, Page and the accent colours
ds-cycleshows; ds-message’s words;- the language names (
translate("Japanese")).
Use them rather than a word of the product’s own: “Try again” after a failure, never a local “Retry”, and Close for a dismiss button. They are translated into every language of the list except nn and no, which read nb’s; a word left out of a language reads English. Turn them on with configureI18n({ sharedWords: true }) (or await loadSharedWords()). They arrive in chunks of their own, one per language: the reader’s language, the languages it falls back to and English, never all fifteen, and a language switch fetches only the new one. Products that do not use them do not carry them, and they never change which languages a product offers.
Language-aware addresses
Section titled “Language-aware addresses”/ja/pricing is the pricing page in Japanese, /pricing the same page in the default language. The functions are pure, for a browser, a Worker or a test alike (ds1/1-root/i18n.ts):
<code><span class="line"><span style="color:var(--docs-code-keyword)">import</span><span style="color:var(--docs-code-ink)"> {</span></span><span class="line"><span style="color:var(--docs-code-ink)"> parseLanguagePath,</span></span><span class="line"><span style="color:var(--docs-code-ink)"> withLanguagePrefix,</span></span><span class="line"><span style="color:var(--docs-code-ink)"> stripLanguagePrefix,</span></span><span class="line"><span style="color:var(--docs-code-ink)"> isLanguageSlug,</span></span><span class="line"><span style="color:var(--docs-code-ink)">} </span><span style="color:var(--docs-code-keyword)">from</span><span style="color:var(--docs-code-string)"> "@0001-labs/design-system-1"</span><span style="color:var(--docs-code-ink)">;</span></span><span class="line"></span><span class="line"><span style="color:var(--docs-code-tag)">parseLanguagePath</span><span style="color:var(--docs-code-ink)">(</span><span style="color:var(--docs-code-string)">"/ja/pricing"</span><span style="color:var(--docs-code-ink)">); </span><span style="color:var(--docs-code-comment)">// { language: "ja", restPath: "/pricing" }</span></span><span class="line"><span style="color:var(--docs-code-tag)">withLanguagePrefix</span><span style="color:var(--docs-code-ink)">(</span><span style="color:var(--docs-code-string)">"/pricing"</span><span style="color:var(--docs-code-ink)">, </span><span style="color:var(--docs-code-string)">"pt-BR"</span><span style="color:var(--docs-code-ink)">); </span><span style="color:var(--docs-code-comment)">// "/pt-br/pricing"</span></span><span class="line"><span style="color:var(--docs-code-tag)">withLanguagePrefix</span><span style="color:var(--docs-code-ink)">(</span><span style="color:var(--docs-code-string)">"/ja/pricing"</span><span style="color:var(--docs-code-ink)">, </span><span style="color:var(--docs-code-string)">"en"</span><span style="color:var(--docs-code-ink)">); </span><span style="color:var(--docs-code-comment)">// "/pricing"</span></span><span class="line"><span style="color:var(--docs-code-tag)">stripLanguagePrefix</span><span style="color:var(--docs-code-ink)">(</span><span style="color:var(--docs-code-string)">"/ja/pricing"</span><span style="color:var(--docs-code-ink)">); </span><span style="color:var(--docs-code-comment)">// "/pricing"</span></span></code>The language an address names
Section titled “The language an address names”A page at /ja/… speaks Japanese, whatever the reader chose before. Take the language from the address, and the stored choice is left as it is, so following a link to /ja/… never overwrites what a person chose for the rest of the product:
<code><span class="line"><span style="color:var(--docs-code-tag)">configureI18n</span><span style="color:var(--docs-code-ink)">({ languages: [</span><span style="color:var(--docs-code-string)">"en"</span><span style="color:var(--docs-code-ink)">, </span><span style="color:var(--docs-code-string)">"da"</span><span style="color:var(--docs-code-ink)">, </span><span style="color:var(--docs-code-string)">"ja"</span><span style="color:var(--docs-code-ink)">], languageFromAddress: </span><span style="color:var(--docs-code-number)">true</span><span style="color:var(--docs-code-ink)"> });</span></span></code>With languageFromAddress, the language at startup is the address’s prefix (by routing); an address without one keeps the stored choice or the browser’s. A person’s choice through setLanguage (or the language cycle) is still stored; the product then goes to the address in that language with withLanguagePrefix. Set it on window.DS_ONE_I18N before the theme head script, and lang is right before the first paint too.
Calling setLanguage(language) with the address’s language at startup instead stores it as the person’s choice: pass { remember: false } if a product does it that way.
The languages that get a prefix are the product’s languages; the default language (en) has none. Change either through configureI18n({ routing: { … } }) or configureLanguageRouting:
<code><span class="line"><span style="color:var(--docs-code-tag)">configureI18n</span><span style="color:var(--docs-code-ink)">({</span></span><span class="line"><span style="color:var(--docs-code-ink)"> languages: [</span><span style="color:var(--docs-code-string)">"en"</span><span style="color:var(--docs-code-ink)">, </span><span style="color:var(--docs-code-string)">"da"</span><span style="color:var(--docs-code-ink)">, </span><span style="color:var(--docs-code-string)">"ja"</span><span style="color:var(--docs-code-ink)">],</span></span><span class="line"><span style="color:var(--docs-code-ink)"> routing: { languages: [</span><span style="color:var(--docs-code-string)">"da"</span><span style="color:var(--docs-code-ink)">, </span><span style="color:var(--docs-code-string)">"ja"</span><span style="color:var(--docs-code-ink)">], defaultLanguage: </span><span style="color:var(--docs-code-string)">"en"</span><span style="color:var(--docs-code-ink)">, prefixDefaultLanguage: </span><span style="color:var(--docs-code-number)">false</span><span style="color:var(--docs-code-ink)"> },</span></span><span class="line"><span style="color:var(--docs-code-ink)">});</span></span></code>A prefix already there is replaced, never doubled, and a query and hash stay on the address. Product-specific rules (which pages never carry a language) stay in the product.
Checking a language on a server
Section titled “Checking a language on a server”A server checks a language against the same codes the interface offers, but it does not import them from Design system 1: i18n.ts loads each language through a dynamic import, which a Convex schema may not contain, and the package keeps no convex dependency. Build the validator in the product from its own list of codes, in a file that imports only convex/values:
<code><span class="line"><span style="color:var(--docs-code-keyword)">import</span><span style="color:var(--docs-code-ink)"> { v } </span><span style="color:var(--docs-code-keyword)">from</span><span style="color:var(--docs-code-string)"> "convex/values"</span><span style="color:var(--docs-code-ink)">;</span></span><span class="line"></span><span class="line"><span style="color:var(--docs-code-keyword)">export</span><span style="color:var(--docs-code-keyword)"> const</span><span style="color:var(--docs-code-ink)"> languages </span><span style="color:var(--docs-code-keyword)">=</span><span style="color:var(--docs-code-ink)"> [</span><span style="color:var(--docs-code-string)">"en"</span><span style="color:var(--docs-code-ink)">, </span><span style="color:var(--docs-code-string)">"da"</span><span style="color:var(--docs-code-ink)">, </span><span style="color:var(--docs-code-string)">"nb"</span><span style="color:var(--docs-code-ink)">, </span><span style="color:var(--docs-code-string)">"pt-BR"</span><span style="color:var(--docs-code-ink)">] </span><span style="color:var(--docs-code-keyword)">as</span><span style="color:var(--docs-code-keyword)"> const</span><span style="color:var(--docs-code-ink)">;</span></span><span class="line"><span style="color:var(--docs-code-keyword)">export</span><span style="color:var(--docs-code-keyword)"> const</span><span style="color:var(--docs-code-ink)"> languageValidator </span><span style="color:var(--docs-code-keyword)">=</span><span style="color:var(--docs-code-ink)"> v.</span><span style="color:var(--docs-code-tag)">union</span><span style="color:var(--docs-code-ink)">(</span><span style="color:var(--docs-code-keyword)">...</span><span style="color:var(--docs-code-ink)">languages.</span><span style="color:var(--docs-code-tag)">map</span><span style="color:var(--docs-code-ink)">(code </span><span style="color:var(--docs-code-keyword)">=></span><span style="color:var(--docs-code-ink)"> v.</span><span style="color:var(--docs-code-tag)">literal</span><span style="color:var(--docs-code-ink)">(code)));</span></span></code>A product that offers every language of the list writes all of its codes there.
The picker
Section titled “The picker”<code><span class="line"><span style="color:var(--docs-code-ink)"><</span><span style="color:var(--docs-code-tag)">ds-cycle</span><span style="color:var(--docs-code-keyword)"> type</span><span style="color:var(--docs-code-ink)">=</span><span style="color:var(--docs-code-string)">"language"</span><span style="color:var(--docs-code-ink)">></</span><span style="color:var(--docs-code-tag)">ds-cycle</span><span style="color:var(--docs-code-ink)">></span></span></code>
The language cycle goes through the product’s languages, each shown by its own name, sets the language on a press and follows it when something else changes it. It uses a normal button that fits its language name; there is no fixed-width item variant. See Cycle.
Dates, numbers and money
Section titled “Dates, numbers and money”Dates, times, relative time (“3 minutes ago”), numbers, money and lists follow the current language too, through cached Intl formatters: formatDate, formatRelativeTime, formatNumber, formatMoney, formatList and the rest. See Formatting.
Options
Section titled “Options”| Option | Default | What it does |
|---|---|---|
languages | the languages in the loaded words | The languages the product speaks: offered by the picker, the browser’s language lands on one, they get an address prefix |
sharedWords | false | Load the shared interface words beneath the product’s |
languageFile | none | A file per language: a pattern with {language}, or a function |
translationsFile | ./translations.json | The whole table |
storageKey | language | Where the choice is remembered |
routing | the product’s languages, no default prefix | Which languages get an address prefix |
languageFromAddress | false | Start in the language the address’s prefix names, without storing it |
langTarget | the document’s root | The element that carries lang, or false for none |
reportMissingKeys | false | Report keys with no text on the console, each once (also window.DS_ONE_I18N_DEBUG = true) |
Call configureI18n straight after importing the module, or set window.DS_ONE_I18N before it loads.