npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2026 – Pkg Stats / Ryan Hefner

svelte-component-i18n

v0.1.4

Published

Lightweight internationalization library for Svelte.

Readme

NPM Version

svelte-component-i18n

A lightweight internationalization library for Svelte.

It acts as a thin wrapper around native browser APIs, prioritizing type safety and component-level translation management.

Installation

npm i -S svelte-component-i18n

Usage

This library consists of two parts:

  1. Translator keeps track of the current locale and propagates changes to all components.
  2. Dictionary is a component level record of translations.

First should create just one global Translator instance and reuse it in every component.

// src/lib/i18n.ts

import { Translator } from 'svelte-component-i18n';

export const translator = new Translator({
  // All languages you want to support.
  supportedLanguages: ['en', 'de'],
  // The fallback language is the only one, where translations are required to be defined.
  // If a translation for a given language is missing, the fallback language is used instead.
  fallbackLanguage: 'en',
});

Next you should define a Dictionary in your components.

<!--
    You can define translations in a `module`-Block, as it is shared across instances of the
    same component.
-->
<script lang="ts" module>
  import { translator } from '$lib/i18n';

  // You can destruct the dictionary into the required translation functions.
  //   - `translate` and `t`: Translate a key
  //   - `pluralize` and `p`: Translate a key taking pluralization into account
  const { t } = translator.define({
    myFirstTranslation: {
      en: 'My first translation',
      de: 'Meine erste Übersetzung',
    },
  });
</script>

<!--
    The keys and parameters are type-checked.
    If the key is not defined, the component will not compile.
-->
<p>{t('myFirstTranslation')}</p>

Language Detection

Detecting the user's preferred language using navigator.languages or a previously selected language from the localStorage.

Browser globals are not defined when using server-side rendering in SvelteKit. The provided strategies and hooks skip execution, when navigator or localStorage are missing.

import { fromLocalStorage, fromNavigator, toLocalStorage, Translator } from 'svelte-component-i18n';

const localStorageKey = 'my-app-language';

const translator = new Translator({
  supportedLanguages: ['en', 'de'],
  fallbackLanguage: 'en',
  languageStrategies: [fromLocalStorage(localStorageKey), fromNavigator()],
  languageHooks: [toLocalStorage(localStorageKey)],
});

Language Selection

The Translator exposes both the currentLanguage and the initially provided supportedLanguages. You can use both to create a language selector. The currentLanguage is a rune and will automatically propagate changes to all components.

<script lang="ts" module>
  import { translator } from '$lib/i18n';
</script>

<select bind:value={translator.currentLanguage}>
  {#each translator.supportedLanguages as language (language)}
    <option value={language}>{language}</option>
  {/each}
</select>

Simple Translations

Basic key-value mapping for static text.

<script lang="ts" module>
  import { translator } from '$lib/i18n';

  const { t } = translator.define({
    simple: {
      en: 'Simple',
      de: 'Einfach',
    },
  });
</script>

{t('simple')}

Pluralization

Using Intl.PluralRules^1 logic to handle count-based text variations.

<script lang="ts" module>
  import { translator } from '$lib/i18n';

  const { p } = translator.define({
    pluralized: {
      en: {
        one: 'One thing',
        other: 'Many things',
      },
      de: {
        one: 'Ein Ding',
        other: 'Viele Dinge',
      },
    },
  });
</script>

{p('pluralized', 1)}
{p('pluralized', 42)}

Interpolation

Instead of defining static structures you can also define functions, which return the same structures. Template literals can be used to interpolate text.

<script lang="ts" module>
  import { translator } from '$lib/i18n';

  const { t } = translator.define({
    parameterized: (name: string) => ({
      en: `Hello ${name}`,
      de: `Hallo ${name}`,
    }),
  });
</script>

{t('parameterized', 'Svelte')}