Svelte wrapper for i18next
npm i svelte-i18next i18next
This library wraps an i18next instance in a Svelte Store to observe i18next events so your Svelte components re-render e.g. when language is changed or a new resource is loaded by i18next.
i18n.js
:
import i18next from "i18next";
import { createI18nStore } from "svelte-i18next";
i18next.init({
lng: 'en',
resources: {
en: {
translation: {
"key": "hello world"
}
}
},
interpolation: {
escapeValue: false, // not needed for svelte as it escapes by default
}
});
const i18n = createI18nStore(i18next);
export default i18n;
App.svelte
:
<script>
import i18n from './i18n.js';
</script>
<div>
{$i18n.t('key')}
</div>
Sveltekit shares stores across requests on server-side. This means that one users request could change the language setting of another users rendering if that is still ongoing. To avoid this issue, use setContext
to create request-scoped store instances:
i18n.js
:
import i18next from "i18next";
import { createI18nStore } from "svelte-i18next";
i18next.init({
lng: 'en',
resources: {
en: {
translation: {
"key": "hello world"
}
}
},
interpolation: {
escapeValue: false, // not needed for svelte as it escapes by default
}
});
export default () => createI18nStore(i18next);
routes/+layout.svelte
:
<script>
import getI18nStore from "i18n.js";
import { setContext } from "svelte";
setContext('i18n', getI18nStore());
</script>
routes/+page.svelte
:
<script>
import { getContext } from "svelte";
const i18n = getContext("i18n");
</script>
<div>
<h1>{ $i18n.t("key") }</h1>
</div>
See full example project: Svelte example