Gathering detailed insights and metrics for svelte-phrase-chain
Gathering detailed insights and metrics for svelte-phrase-chain
Gathering detailed insights and metrics for svelte-phrase-chain
Gathering detailed insights and metrics for svelte-phrase-chain
npm install svelte-phrase-chain
Typescript
Module System
Node Version
NPM Version
Cumulative downloads
Total Downloads
Last Day
0%
NaN
Compared to previous day
Last Week
0%
NaN
Compared to previous week
Last Month
0%
NaN
Compared to previous month
Last Year
0%
NaN
Compared to previous year
4
A powerful, type-safe internationalization (i18n) library for Svelte applications. Built with Svelte 5 runes and TypeScript for a modern, reactive localization experience.
Svelte Phrase Chain uses a dedicated CLI for project initialization, ensuring a streamlined setup process.
Note: You can skip installing the package, as the CLI will effectively just copy paste all necessary files to your project.
1# Using bun 2bunx svelte-phrase-chain init 3 4# Using npm 5npx svelte-phrase-chain init 6 7# Using pnpm 8pnpx svelte-phrase-chain init 9
The CLI accepts several options to customize your setup:
1# Full example with all options 2bunx svelte-phrase-chain init \ 3 --locales en,fr,es,de \ 4 --fallbackLocale en \ 5 --persistLocale \ 6 --localStorageKey app_locale \ 7 --translationsDir src/lib/i18n/translations \ 8 --generateTranslations \ 9 --debug
Option | Description | Default |
---|---|---|
--locales | Supported locales (comma-separated) | en,es,fr |
--fallbackLocale | Fallback locale | en |
--persistLocale | Persist locale in localStorage | true |
--localStorageKey | LocalStorage key for locale | app_locale |
--translationsDir | Translations folder path | src/lib/translations |
--generateTranslations | Generate initial translation JSON files | true |
--debug | Enable debug logging | true |
The CLI will generate:
src/
└── lib/
├── i18n/
│ ├── i18n.ts # Main i18n configuration & exports
│ └── core/ # Core implementation files
│ ├── index.svelte.ts # Main i18n functionality
│ └── types.ts # TypeScript types
└── translations/ # Translation JSON files
├── en.json
├── es.json
└── fr.json
1<script lang="ts"> 2 import { t, setLocale, locale, initLocale } from '$lib/i18n/i18n'; 3 import type { Locale } from '$lib/i18n/i18n'; 4 5 // Initialize with browser detection 6 initLocale({ 7 preferBrowser: true, 8 preferStorage: true, 9 defaultLocale: 'en' 10 }); 11 12 // Example usage 13 let name = $state("User"); 14 let messageCount = $state(1); 15 16 function changeLanguage(lang: Locale) { 17 setLocale(lang); 18 } 19</script> 20 21<h1>{t('common.welcome')}</h1> 22<p>{t('common.greeting', { name })}</p> 23 24<p>{t('user.messageCount', { count: messageCount }, messageCount)}</p> 25 26<!-- Language switcher --> 27<div> 28 <p>Current locale: {locale()}</p> 29 <button onclick={() => changeLanguage('en')}>English</button> 30 <button onclick={() => changeLanguage('fr')}>Français</button> 31 <button onclick={() => changeLanguage('es')}>Español</button> 32</div>
Svelte Phrase Chain follows the "code is yours, do what you want with it" approach, similar to shadcn and other projects. It is designed with sensible defaults, but all you need to add is up to you:
When you run the CLI:
While there are several i18n solutions for Svelte like svelte-i18n and typesafe-i18n, Svelte Phrase Chain offers distinct advantages:
Hello, {name}!
)The library expects JSON files with translations for each locale:
1// src/lib/translations/en.json 2{ 3 "common": { 4 "welcome": "Welcome to our app", 5 "greeting": "Hello, {name}!", 6 "footer": "© 2025 Awesome App. All rights reserved." 7 }, 8 "user": { 9 "messageCount": { 10 "zero": "No messages", 11 "one": "You have {count} unread message", 12 "other": "You have {count} unread messages" 13 } 14 } 15}
1<script> 2 import { t } from '$lib/i18n/i18n'; 3 4 const joinDate = new Date('2023-01-15'); 5 const lastLoginDate = new Date(Date.now() - 3600000); // 1 hour ago 6</script> 7 8<!-- In your translation file: "user.joinDate": "Member since {date:date}" --> 9<p>{t('user.joinDate', { date: joinDate })}</p> 10 11<!-- In your translation file: "user.lastLogin": "Last login: {date:relative}" --> 12<p>{t('user.lastLogin', { date: lastLoginDate })}</p>
1<script> 2 import { t } from '$lib/i18n/i18n'; 3 4 let count = $state(1); 5 6 function increment() { 7 count++; 8 } 9</script> 10 11<!-- 12Translation structure in user.messageCount: 13{ 14 "zero": "No messages", 15 "one": "{count} message", 16 "other": "{count} messages" 17} 18--> 19 20<p>{t('user.messageCount', { count }, count)}</p> 21<button on:click={increment}>Add Message</button>
Use the provided schema tools to validate your translation files:
1// scripts/validate-translations.ts 2import { createI18nSchema } from 'svelte-phrase-chain/schema'; 3import en from '../src/lib/translations/en.json'; 4import es from '../src/lib/translations/es.json'; 5import fr from '../src/lib/translations/fr.json'; 6 7const mySchema = createI18nSchema({ 8 pluralKeyIdentifier: (key) => key.endsWith('Count'), 9 requiredPluralKeys: ['one', 'other'], 10 optionalPluralKeys: ['zero', 'few', 'many'], 11 allowedDateFormats: ['date', 'relative', 'fullDate'] 12}); 13 14// Validate all translation files 15try { 16 const enValid = mySchema.parse(en); 17 const esValid = mySchema.parse(es); 18 const frValid = mySchema.parse(fr); 19 console.log("✅ All translation files are valid!"); 20} catch (error) { 21 console.error("❌ Validation failed:", error); 22 process.exit(1); 23}
Supports all modern browsers with ECMAScript 2015 (ES6) support, with features relying on:
Intl
API for formattingContributions are welcome! Please feel free to submit a Pull Request.
This project is licensed under the MIT License - see the LICENSE file for details.
No vulnerabilities found.
No security vulnerabilities found.