This project provides a flexible internationalization (i18n) solution for Svelte applications, with support for multiple languages and dynamic translation loading. It allows you to load translation files at runtime, switch languages dynamically, and retrieve translations with support for interpolation.
To get started, simply install the package using npm or yarn:
npm install svelte-translate-pro
or
yarn add svelte-translate-pro
You can define the languages supported by your application using the AppLanguages
enum:
import { AppLanguages } from 'svelte-translate-pro';
console.log(AppLanguages.EN); // "en"
Use the loadTranslation
function to dynamically load a translation data for a given language.
import { loadTranslation, AppLanguages } from 'svelte-translate-pro';
import en from "/lib/translations/en.json"
await loadTranslation(AppLanguages.EN, en);
fr.json
){
"navbar.title": "Bienvenue",
"footer.contact": "Contactez-nous"
}
You can retrieve the current language using the getActiveLanguage
function.
import { getActiveLanguage } from 'svelte-translate-pro';
const currentLang = getActiveLanguage();
console.log(currentLang); // "en"
Use setPageSpecificTranslations
to update translations for the current page. This allows you to set translations that are specific to the page content.
import { setPageSpecificTranslations, AppLanguages } from 'svelte-translate-pro';
setPageSpecificTranslations({
[AppLanguages.EN]: { "title": "Welcome to the page" },
[AppLanguages.FA]: { "title": "به صفحه خوش آمدید" }
});
You can use the $t$
store to retrieve translations reactively in your Svelte components.
<script lang="ts">
import { t$ } from 'svelte-translate-pro';
</script>
<h1>{$t$('navbar.title')}</h1> <!-- Displays the translation for "navbar.title" -->
You can also pass variables for interpolation:
const variables = { name: "John" };
const translatedText = $t$('greeting', variables);
greeting
key in JSON):{
"greeting": "Hello, {{name}}!"
}
You can also pass language-specific values for interpolation:
$t$('greeting', { name: {
[AppLanguages.EN]: "John",
[AppLanguages.FA]: "جان"
}});
If you need to provide inline translations directly in the code, you can use the following format:
$t$({
[AppLanguages.EN]: "Hello, World!",
[AppLanguages.FA]: "سلام دنیا!"
});
You can switch the language by calling the setActiveLanguage
function, which updates the application's language:
import { setActiveLanguage, AppLanguages } from 'svelte-translate-pro';
setActiveLanguage(AppLanguages.FA); // Switches to Persian (FA)
setActiveLanguage(AppLanguages.EN); // Switches to English (EN)
When loading translations in development mode (DEV
environment variable is true), you will see console logs indicating successful translation file loading.
DEBUG=vite-plugin-svelte:node-modules-onwarn pnpm build
AppLanguages
An enum that defines the supported languages:
export enum AppLanguages {
EN = "en",
FA = "fa",
ES = "es",
DE = "de",
FR = "fr",
IT = "it",
RU = "ru",
CN = "zh-cn",
JP = "ja",
AR = "ar"
}
getActiveLanguage(): AppLanguage
Gets the current language of the application.
const lang = getActiveLanguage(); // "en"
setPageSpecificTranslations(data: Record<AppLanguage, TranslationData>)
Sets page-specific translations for the current page.
setPageSpecificTranslations({
en: { "title": "Welcome" },
fa: { "title": "به صفحه خوش آمدید" }
});
t$
: ReadableA derived store that provides a reactive translation function.
<h1>{$t$('navbar.title')}</h1>
TranslationObject
Represents a translation object that can map language codes to translation strings.
type TranslationObject = Partial<Record<AppLanguage, string>>;
TranslationData
Represents translation data for a single language.
type TranslationData = Record<string, string>;
reactiveTranslate
: Reactive Translations for ArraysreactiveTranslate
is a utility function that provides a reactive array of translated items based on the current language.
import { reactiveTranslate } from "svelte-translate-pro";
const tabs = reactiveTranslate((t) => [
{ title: t("tabs.recieve"), id: 1 },
{ title: t("tabs.send"), id: 2 },
]);
<div>
{#each $tabs as tab}
<button>{tab.title}</button>
{/each}
</div>
This project is licensed under the MIT License - see the LICENSE file for details.
Contributions are welcome! Please fork this repository and submit your pull requests.
Happy coding and enjoy using the Svelte i18n system!