Getting started
This repository contains internationalization for SvelteKit.
Installation
1. Install package
pnpm i @deckweiss/internationalization
2. Initialize i18n
import { initialize as initializeI18n } from '@deckweiss/internationalization';
import de from '$lib/translations/de.json';
import en from '$lib/translations/en.json';
initializeI18n({
defaultLocale: 'en',
locales: {
de,
en
}
});
import { handle } from '@deckweiss/internationalization';
import './i18n.js';
export { handle };
import './i18n.js';
3. Use translations
// +page.svelte
<script>
import { t } from '@deckweiss/internationalization';
</script>
<p>{$t('app.day', { date: Date.now() })}</p>
4. (optional) Update selected locale
// src/lib/components/language-picker.svelte
<script>
import { locale, locales, setLocale } from '@deckweiss/internationalization';
</script>
<select value={$locale} on:change={(event) => setLocale(event.target.value)}>
<option disabled>Choose language</option>
{#each locales as l}
<option value={l} selected={l === $locale}>{l}</option>
{/each}
</select>
JSON Format
This package is compatible with i18next JSON v4. Although, it currently does only support nested keys and interpolation with formatting.
Examples
{
"app": {
"name": "Example app",
"day": "Today is {{date, date(format: dddd)}}"
}
}
Usage | Output |
---|
$t('app.name') | "Example app" |
$t('app.day', { date: new Date(1997, 4, 22) })} | "Today is Thursday" |
Date format symbols can be found here.
Bundled date languages
- German
- English
- Italian
- Spanish
Supported Interpolations
Date and Time
{
"day": "Today is {{date, date(format: dddd, dd.mm.yyyy)}}"
}
Numbers and Currency
The options
argument gets directly passed into the Intl.NumberFormat
constructor. Hence, you can configure any option in JSON syntax.
{
"number": "{{count, number(options: {\"style\": \"decimal\", \"minimumFractionDigits\": 5})}}",
"currency": "{{amount, number(options: {\"style\": \"currency\", \"currency\": \"EUR\"})}}"
}
Contribution
Create a new release
- Update package version either by hand or with
pnpm version
. - The commit message onto main must begin with
chore: release