JSPM

@weirdscience/localization

1.1.1
  • ESM via JSPM
  • ES Module Entrypoint
  • Export Map
  • Keywords
  • License
  • Repository URL
  • TypeScript Types
  • README
  • Created
  • Published
  • 0
  • Score
    100M100P100Q66214F
  • License MIT

A TypeScript library for managing localization and translations, supporting multiple languages, dynamic language switching, and translation retrieval.

Package Exports

  • @weirdscience/localization
  • @weirdscience/localization/dist/index.js

This package does not declare an exports field, so the exports above have been automatically detected and optimized by JSPM instead. If any package subpath is missing, it is recommended to post an issue to the original package (@weirdscience/localization) to support the "exports" field. If that is not possible, create a JSPM override to customize the exports field for this package.

Readme

@weirdscience/localization

A TypeScript library designed for managing localization and translations. It supports multiple languages, dynamic language switching, and provides a straightforward API for translation retrieval and pluralization.

Features

  • Multiple Languages Support: Easily manage translations across multiple languages.
  • Dynamic Language Switching: Switch languages on the fly without reloading.
  • Pluralization: Automatically handle plural forms of words based on count.
  • Parameter Replacement: Dynamically insert values into translations.

Installation

Install the package using npm:

npm install @weirdscience/localization

Or using yarn:

yarn add @weirdscience/localization

Usage

First, import the localization object from the package:

import { localization } from "@weirdscience/localization";

Initialization

Initialize the localization system with your default language, translations, and optionally a fallback language:

localization.init({
  defaultLang: "en",
  languages: {
    en: {
      greeting: "Hello, {{name}}!",
    },
    fr: {
      greeting: "Bonjour, {{name}}!",
    },
  },
  fallbackLang: "en",
});

Adding Translations

Add or update translations for a specific language:

localization.addLanguage("es", {
  greeting: "Hola, {{name}}!",
});

Setting the Current Language

Switch the current language:

localization.setLanguage("fr");

Retrieving Translations

Get a translated string, with optional parameter replacement and pluralization:

// Simple translation
console.log(localization.t("greeting", { name: "John" }));

// Pluralization
console.log(localization.t("apple", {}, 5)); // apples

API Reference

  • init(options: { defaultLang?: string; languages: Languages; fallbackLang?: string; }): Initializes the localization system.
  • addLanguage(lang: string, translations: Translations): Adds or updates translations for a language.
  • setLanguage(lang: string): Sets the current language.
  • t(key: string, params?: Record<string, string | number>, count?: number): Retrieves and formats a translation string.

License

This project is licensed under the MIT License - see the LICENSE.md file for details.