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 🙏

© 2025 – Pkg Stats / Ryan Hefner

@profullstack/localizer

v0.8.0

Published

Simple localization and internationalization library with RTL support

Readme

@profullstack/localizer

A simple localization and internationalization library with RTL support.

Features

  • Simple API for translating text
  • Support for multiple languages with fallback
  • Interpolation of variables in translations
  • Support for pluralization
  • Right-to-Left (RTL) language support
  • DOM translation utilities for automatic UI updates
  • MutationObserver integration for dynamic content
  • Works in both browser and Node.js environments

Installation

npm install @profullstack/localizer
# or
yarn add @profullstack/localizer
# or
pnpm add @profullstack/localizer

Usage

Basic Usage

import { localizer, _t } from '@profullstack/localizer';

// Load translations
localizer.loadTranslations('en', {
  'hello': 'Hello',
  'welcome': 'Welcome, ${name}!'
});

localizer.loadTranslations('fr', {
  'hello': 'Bonjour',
  'welcome': 'Bienvenue, ${name}!'
});

localizer.loadTranslations('ar', {
  'hello': 'مرحبا',
  'welcome': 'مرحبًا، ${name}!'
});

// Set the current language
localizer.setLanguage('fr');

// Translate a key
console.log(_t('hello')); // Output: Bonjour

// Translate with interpolation
console.log(_t('welcome', { name: 'John' })); // Output: Bienvenue, John!

DOM Translation

The library provides utilities for automatically translating DOM elements:

// Add data-i18n attributes to your HTML
// <h1 data-i18n="welcome">Welcome</h1>
// <input placeholder="Enter your name" data-i18n-placeholder="enter_name">

// Translate all elements with i18n attributes in the document
localizer.translateDOM();

// Translate elements within a specific container
const container = document.querySelector('.my-container');
localizer.translateContainer(container);

// Set up automatic translation of dynamically added elements
localizer.observeDOM();

RTL Support

The library automatically detects Right-to-Left (RTL) languages and provides methods to check if a language is RTL:

// Check if the current language is RTL
const isRTL = localizer.isRTL();

// Check if a specific language is RTL
const isArabicRTL = localizer.isLanguageRTL('ar'); // true
const isEnglishRTL = localizer.isLanguageRTL('en'); // false

// Apply RTL direction to the document
localizer.applyRTLToDocument();

// The library dispatches an event when the language changes
window.addEventListener('language-changed', (event) => {
  const { language, previousLanguage, isRTL } = event.detail;
  
  // RTL direction is automatically applied when using applyRTLToDocument()
  // but you can also handle it manually:
  document.documentElement.dir = isRTL ? 'rtl' : 'ltr';
  
  if (isRTL) {
    document.body.classList.add('rtl');
  } else {
    document.body.classList.remove('rtl');
  }
});

Loading Translations from JSON Files

// In a browser environment
async function loadTranslations() {
  await localizer.loadTranslationsFromUrl('en', '/i18n/en.json');
  await localizer.loadTranslationsFromUrl('fr', '/i18n/fr.json');
  await localizer.loadTranslationsFromUrl('ar', '/i18n/ar.json');
  
  // Set initial language based on browser preference
  const browserLang = navigator.language.split('-')[0];
  if (localizer.getAvailableLanguages().includes(browserLang)) {
    localizer.setLanguage(browserLang);
  } else {
    localizer.setLanguage('en'); // fallback
  }
}

API Reference

Localizer Class

Constructor

const localizer = new Localizer(options);

Options:

  • defaultLanguage: The default language to use (default: 'en')
  • fallbackLanguage: The fallback language to use when a translation is not found (default: 'en')
  • translations: Initial translations object (default: {})
  • interpolationStart: The start delimiter for interpolation (default: '${')
  • interpolationEnd: The end delimiter for interpolation (default: '}')
  • rtlLanguages: Array of RTL language codes (default: ['ar', 'he', 'fa', 'ur'])

Methods

Core Methods
  • loadTranslations(language, translations): Load translations for a language
  • setLanguage(language): Set the current language
  • getLanguage(): Get the current language
  • getAvailableLanguages(): Get an array of available language codes
  • translate(key, options): Translate a key with optional interpolation and pluralization
  • loadTranslationsFromUrl(language, url): Load translations from a JSON file (browser only)
  • isRTL(): Check if the current language is RTL
  • isLanguageRTL(language): Check if a specific language is RTL
DOM Methods (Browser Only)
  • translateDOM(container?): Translate all elements with i18n attributes in the document or container
  • translateContainer(container): Translate all elements with i18n attributes in a specific container
  • observeDOM(rootElement?): Set up a MutationObserver to automatically translate new elements
  • stopObservingDOM(): Stop observing DOM changes
  • applyRTLToDocument(): Apply RTL direction to the document based on the current language

Translation Function

_t(key, options);

Parameters:

  • key: The translation key
  • options: Options for translation (interpolation values, count for pluralization, etc.)

DOM Translation Attributes

The library supports the following data attributes for DOM translation:

  • data-i18n: Translates the element's text content
  • data-i18n-placeholder: Translates the element's placeholder attribute
  • data-i18n-title: Translates the element's title attribute
  • data-i18n-html: Translates the element's innerHTML (use with caution)
  • data-i18n-params: JSON string of parameters for interpolation (used with data-i18n)

Example:

<h1 data-i18n="welcome">Welcome</h1>
<input placeholder="Enter your name" data-i18n-placeholder="enter_name">
<button title="Click me" data-i18n="button.click" data-i18n-title="button.tooltip">Click me</button>
<p data-i18n="greeting" data-i18n-params='{"name":"John"}'>Hello, John!</p>

License

MIT