I was a bit tired of the issues I was facing with the existing svelte i18n library, version problems, no intellisense for existing translations and the enormous bundled size so I created a very basic "get the job done" i18n library for Svelte.
Pop this in your +layout.svelte
<script lang="ts">
import { setupi18n } from "$src/i18n"
// Add to this enum or change this value depending on your needs.
setupi18n(AvailableTranslationLocales.EN)
</script>
Use the $T
store to do your translations in your code, you get intellisense for the available keys.
<script lang="ts">
import { T } from "$src/i18n"
</script>
<h1>{$T("hello")}</h1>
I'm aware of the lack of support for arrays (although this is superficial, it will be a type error not a runtime error) and I will fix that at some point.
Create a JSON object (I use ChatGPT to transform my EN file into other languages) in the translations folder with the same name as the value in your enum and then use the addTranslation
function to fetch and register the new translation dictionary.
/
translations
fr.json
ch.json
<script lang="ts">
import { addTranslation, setupI18n, AvailableTranslationLocales } from "$src/i18n"
let selectedLang: AvailableTranslationLocales = AvailableTranslationLocales.EN
async function onLangChange() {
await addTranslation(selectedLang)
setupI18n(selectedLang)
}
</script>
<select bind:value={selectedLang} on:change={onLangChange}>
{#each Object.keys(AvailableTranslationLocales) as translation}
<option value={AvailableTranslationLocales[translation]}>{translation}</option>
{/each}
</select>