svelte-translate-pro
v0.0.8
Published
A powerful and flexible internationalization (i18n) library for dynamic language management, real-time translations, and localization. Supports multiple languages, interpolation, and page-specific translations with an intuitive API.
Maintainers
Readme
Svelte Translate Pro
Svelte Internationalization (i18n) with Dynamic Language Loading
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.

Features
- Support for multiple languages.
- Dynamic loading of translation files.
- Page-specific translations.
- Interpolation of variables in translation strings.
- Reactive translation system using Svelte stores.
Installation
To get started, simply install the package using npm or yarn:
npm install svelte-translate-proor
yarn add svelte-translate-proUsage
1. Define Supported Languages
You can define the languages supported by your application using the AppLanguages enum:
import { AppLanguages } from 'svelte-translate-pro';
console.log(AppLanguages.EN); // "en"2. Load Translation Files
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);
Translation File Example (fr.json)
{
"navbar.title": "Bienvenue",
"footer.contact": "Contactez-nous"
}3. Get the Current Language
You can retrieve the current language using the getActiveLanguage function.
import { getActiveLanguage } from 'svelte-translate-pro';
const currentLang = getActiveLanguage();
console.log(currentLang); // "en"4. Set Page-Specific Translations
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": "به صفحه خوش آمدید" }
});5. Retrieve Translations
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);Example with Interpolation (greeting key in JSON):
{
"greeting": "Hello, {{name}}!"
}Example with Language-Specific Variables
You can also pass language-specific values for interpolation:
$t$('greeting', { name: {
[AppLanguages.EN]: "John",
[AppLanguages.FA]: "جان"
}});Inline Translation
If you need to provide inline translations directly in the code, you can use the following format:
$t$({
[AppLanguages.EN]: "Hello, World!",
[AppLanguages.FA]: "سلام دنیا!"
});6. Language Switching
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)7. Debugging
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 buildAPI Reference
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$: Readable
A 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 Arrays
reactiveTranslateis 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>License
This project is licensed under the MIT License - see the LICENSE file for details.
Contributing
Contributions are welcome! Please fork this repository and submit your pull requests.
Happy coding and enjoy using the Svelte i18n system!
