Security News
JSR Working Group Kicks Off with Ambitious Roadmap and Plans for Open Governance
At its inaugural meeting, the JSR Working Group outlined plans for an open governance model and a roadmap to enhance JavaScript package management.
@sveltekit-i18n/base
Advanced tools
Base functionality of sveltekit-i18n library with a support for external message parsers.
This repository contains the base functionality of sveltekit-i18n and provides support for external message parsers.
✅ SvelteKit ready
✅ SSR support
✅ Custom parsers – you can use any message syntax you need
✅ Custom data sources – no matter if you are using local files or remote API to get your translations
✅ Module-based – your translations are loaded for visited pages only (and only once!)
✅ Component-scoped translations – you can create multiple instances with custom definitions
✅ TS support
✅ No external dependencies
Setup translations.js
in your lib folder...
import i18n from '@sveltekit-i18n/base';
// use your preferred parser (or create your own)
import parser from '@sveltekit-i18n/parser-default';
// import parser from '@sveltekit-i18n/parser-icu';
/** @type {import('@sveltekit-i18n/parser-default').Config} */
const config = ({
parser: parser({/* Parser options */}),
loaders: [
{
locale: 'en',
key: 'common',
loader: async () => (
await import('./en/common.json')
).default,
},
{
locale: 'en',
key: 'home',
routes: ['/'], // you can use regexes as well!
loader: async () => (
await import('./en/home.json')
).default,
},
{
locale: 'en',
key: 'about',
routes: ['/about'],
loader: async () => (
await import('./en/about.json')
).default,
},
{
locale: 'cs',
key: 'common',
loader: async () => (
await import('./cs/common.json')
).default,
},
{
locale: 'cs',
key: 'home',
routes: ['/'],
loader: async () => (
await import('./cs/home.json')
).default,
},
{
locale: 'cs',
key: 'about',
routes: ['/about'],
loader: async () => (
await import('./cs/about.json')
).default,
},
],
});
export const { t, locale, locales, loading, loadTranslations } = new i18n(config);
...load your translations in __layout.svelte
...
<script context="module">
import { locale, loadTranslations } from '$lib/translations';
export const load = async ({ url }) => {
const { pathname } = url;
const defaultLocale = 'en'; // get from cookie, user session, ...
const initLocale = locale.get() || defaultLocale; // set default if no locale already set
await loadTranslations(initLocale, pathname); // keep this just before the `return`
return {};
}
</script>
...and include your translations within pages and components.
<script>
import { t } from '$lib/translations';
const pageName = 'This page is Home page!';
</script>
<div>
<h2>{$t('common.page', { pageName })}</h2>
<p>{$t('home.content')}</p>
</div>
Parsers
Docs
Examples
Changelog
If you are facing some issues related to the base functionality, create a ticket here.
FAQs
Base functionality of sveltekit-i18n library with a support for external message parsers.
The npm package @sveltekit-i18n/base receives a total of 8,113 weekly downloads. As such, @sveltekit-i18n/base popularity was classified as popular.
We found that @sveltekit-i18n/base demonstrated a not healthy version release cadence and project activity because the last version was released a year ago. It has 1 open source maintainer collaborating on the project.
Did you know?
Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.
Security News
At its inaugural meeting, the JSR Working Group outlined plans for an open governance model and a roadmap to enhance JavaScript package management.
Security News
Research
An advanced npm supply chain attack is leveraging Ethereum smart contracts for decentralized, persistent malware control, evading traditional defenses.
Security News
Research
Attackers are impersonating Sindre Sorhus on npm with a fake 'chalk-node' package containing a malicious backdoor to compromise developers' projects.