sveltekit-i18n
December 2, 2025 ยท View on GitHub
Note
Looking for maintainers. Feel free to contact me if you want to take over the maintanance of this project.
sveltekit-i18n
A lightweight, powerful internationalization (i18n) library designed specifically for SvelteKit. This package combines @sveltekit-i18n/base with @sveltekit-i18n/parser-default to provide the quickest way to add multilingual support to your SvelteKit applications.
Why sveltekit-i18n?
- ๐ SvelteKit-optimized โ Built specifically for SvelteKit with full SSR support
- ๐ฆ Minimal dependencies โ Only ecosystem packages (base + parser-default)
- โก Smart loading โ Translations load only for visited pages (lazy loading)
- ๐ฏ Route-based โ Automatic translation loading based on your routes
- ๐ง Flexible โ Support for custom data sources (local files, APIs, databases)
- ๐ Multiple parsers โ Choose the syntax that fits your needs
- ๐ TypeScript โ Complete type definitions and typed API
- ๐จ Component-scoped โ Create multiple translation instances for different parts of your app
Installation
npm install sveltekit-i18n
Quick Start
1. Create your translation files
// src/lib/translations/en/common.json
{
"greeting": "Hello, {{name}}!",
"nav.home": "Home",
"nav.about": "About"
}
// src/lib/translations/cs/common.json
{
"greeting": "Ahoj, {{name}}!",
"nav.home": "Domลฏ",
"nav.about": "O nรกs"
}
2. Setup i18n configuration
// src/lib/translations/index.js
import i18n from 'sveltekit-i18n';
/** @type {import('sveltekit-i18n').Config} */
const config = {
loaders: [
{
locale: 'en',
key: 'common',
loader: async () => (await import('./en/common.json')).default,
},
{
locale: 'cs',
key: 'common',
loader: async () => (await import('./cs/common.json')).default,
},
],
};
export const { t, locale, locales, loading, loadTranslations } = new i18n(config);
3. Load translations in your layout
// src/routes/+layout.js
import { loadTranslations } from '$lib/translations';
/** @type {import('./$types').LayoutLoad} */
export const load = async ({ url }) => {
const { pathname } = url;
const initLocale = 'en'; // determine from cookie, user preference, etc.
await loadTranslations(initLocale, pathname);
return {};
};
4. Use translations in your components
<!-- src/routes/+page.svelte -->
<script>
import { t } from '$lib/translations';
</script>
<h1>{$t('common.greeting', { name: 'World' })}</h1>
<nav>
<a href="/">{$t('common.nav.home')}</a>
<a href="/about">{$t('common.nav.about')}</a>
</nav>
Key Features
Route-based Loading
Load translations only for specific routes to optimize performance:
const config = {
loaders: [
{
locale: 'en',
key: 'home',
routes: ['/'], // Load only on homepage
loader: async () => (await import('./en/home.json')).default,
},
{
locale: 'en',
key: 'about',
routes: ['/about'], // Load only on about page
loader: async () => (await import('./en/about.json')).default,
},
],
};
Placeholders and Modifiers
Use dynamic values in your translations:
{
"welcome": "Welcome, {{name}}!",
"items": "You have {{count:number;}} {{count; 1:item; default:items;}}."
}
<script>
import { t } from '$lib/translations';
</script>
<p>{$t('welcome', { name: 'Alice' })}</p>
<p>{$t('items', { count: 5 })}</p>
Documentation
๐ Complete Documentation Index โ Find everything in one place
Quick Links
- ๐ Getting Started Guide โ 15-minute tutorial
- ๐๏ธ Architecture Overview โ How everything works
- ๐ API Documentation โ Complete reference
- โจ Best Practices โ Production-ready patterns
- ๐ง Troubleshooting โ Common issues & FAQ
Examples
Explore working examples for different use cases:
- Multi-page app โ Most common setup
- Locale-based routing โ SEO-friendly URLs (e.g.,
/en/about) - Component-scoped translations โ Isolated translation contexts
- Custom parsers โ Using ICU message format
- All examples โ Complete list of examples
Advanced Usage
Need a different parser?
This library uses @sveltekit-i18n/parser-default. If you need ICU message format or want to create your own parser, use @sveltekit-i18n/base directly:
import i18n from '@sveltekit-i18n/base';
import parser from '@sveltekit-i18n/parser-icu';
const config = {
parser: parser(),
// ... rest of config
};
Learn more about parsers.
TypeScript Support
Full TypeScript support with complete type definitions for configuration and API:
import i18n, { type Config } from 'sveltekit-i18n';
const config: Config = {
loaders: [
// ... your loaders
],
};
export const { t, locale, locales, loading, loadTranslations } = new i18n(config);
Note: The library provides type definitions but does not automatically infer translation keys from your JSON files. You can create custom type-safe wrappers if needed (see Best Practices).
Contributing
We welcome contributions! Please read our Contributing Guide for details on:
- Development setup and workflow
- Git workflow (rebase-based, linear history)
- Commit guidelines (atomic commits)
- Pull request process
- Code standards and testing
Note: We're currently looking for maintainers. If you're interested in helping maintain this project, please reach out via this issue.
Changelog
See Releases for version history.
Related Packages
- @sveltekit-i18n/base โ Core functionality with custom parser support
- @sveltekit-i18n/parser-default โ Default message parser
- @sveltekit-i18n/parser-icu โ ICU message format parser
License
MIT