@sveltekit-i18n/base
This repository contains the base functionality of sveltekit-i18n and provides support for external message parsers.
Key features
â
SvelteKit ready
â
SSR support
â
Custom parsers â you can use any message syntax you need
â
Custom data sources â no matter if you are using local files or remote API to get your translations
â
Module-based â your translations are loaded for visited pages only (and only once!)
â
Component-scoped translations â you can create multiple instances with custom definitions
â
TS support
â
No external dependencies
Usage
Setup translations.js
in your lib folder...
import i18n from '@sveltekit-i18n/base';
import parser from '@sveltekit-i18n/parser-default';
const config = ({
parser: parser({}),
loaders: [
{
locale: 'en',
key: 'common',
loader: async () => (
await import('./en/common.json')
).default,
},
{
locale: 'en',
key: 'home',
routes: ['/'],
loader: async () => (
await import('./en/home.json')
).default,
},
{
locale: 'en',
key: 'about',
routes: ['/about'],
loader: async () => (
await import('./en/about.json')
).default,
},
{
locale: 'cs',
key: 'common',
loader: async () => (
await import('./cs/common.json')
).default,
},
{
locale: 'cs',
key: 'home',
routes: ['/'],
loader: async () => (
await import('./cs/home.json')
).default,
},
{
locale: 'cs',
key: 'about',
routes: ['/about'],
loader: async () => (
await import('./cs/about.json')
).default,
},
],
});
export const { t, locale, locales, loading, loadTranslations } = new i18n(config);
...load your translations in +layout.js
...
import { loadTranslations } from '$lib/translations';
export const load = async ({ url }) => {
const { pathname } = url;
const initLocale = 'en';
await loadTranslations(initLocale, pathname);
return {};
}
...and include your translations within pages and components.
<script>
import { t } from '$lib/translations';
const pageName = 'This page is Home page!';
</script>
<div>
<h2>{$t('common.page', { pageName })}</h2>
<p>{$t('home.content')}</p>
</div>
More info
Parsers
Docs
Examples
Changelog
Issues
If you are facing some issues related to the base functionality, create a ticket here.