Usage
Installation
npm install -D @monstermann/stdlib-i18npnpm add -D @monstermann/stdlib-i18nyarn add -D @monstermann/stdlib-i18nbun add -D @monstermann/stdlib-i18nnub add -D @monstermann/stdlib-i18naube add -D @monstermann/stdlib-i18nSet up stdlib-transform for your bundler, without it every message ends up in every bundle.
Messages
One key per message, with one translation per language:
auth.signIn:
en: Sign in
de: Anmelden
auth.codeSent:
en: We sent a code to {email}
de: Wir haben einen Code an {email} gesendet
Files and their names don’t matter, they are only used for organization - they all get bundled together.
Translations are written in the ICU message format:
| Syntax | Props |
|---|---|
Hello {name} |
{ name: number | string } |
{count, number} new |
{ count: number } |
Due {when, date, short}, {when, time} |
{ when: Date | number } |
{count, plural, one {# task} other {# tasks}} |
{ count: number } |
{place, selectordinal, one {#st} other {#th}} |
{ place: number } |
{role, select, admin {Admin} other {Member}} |
{ role: string } |
Compiling
{
"scripts": {
"i18n": "stdlib-i18n --messages 'i18n/*.yaml' --output src/i18n"
}
}
--messagescan take several globs- The
outputfolder gets anindex.tsand a_messages.ts - Git ignored files are skipped
Translating
Every message takes the language first, then its props:
import { m } from "./i18n"
m.auth.signIn({ lang: "de" })
m.auth.codeSent({ lang: "de" }, { email: "ada@example.com" })
As shown in the introduction, you can build your own wrappers to automate MessageContext, for example building a useMessage hook for react, or building an abstraction that pulls user preferences from a database for servers.
Auto-imports
Depending on your setup, the TypeScript LSP may autocomplete from _messages.ts in your editor, which you can prevent by telling it to skip files starting with _:
{ "autoImportSpecifierExcludeRegexes": ["(^|/)_[^/]*$"] }