New Research: Supply Chain Attack on Axios Pulls Malicious Dependency from npm.Details →
Socket
Book a DemoSign in
Socket

language-observer

Package Overview
Dependencies
Maintainers
1
Versions
15
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install
Package was removed
Sorry, it seems this package was removed from the registry

language-observer

The LanguageObserver class provides a simple way to manage internationalization in your web application. It automates the process of applying translations and simplifies support for multiple languages.

latest
Source
npmnpm
Version
1.5.3
Version published
Weekly downloads
0
Maintainers
1
Weekly downloads
 
Created
Source

language-observer

LanguageObserver is a lightweight and flexible class designed to simplify internationalization in your web application. It automatically applies translations to elements on your page and supports dynamic content updates. By observing language changes via class modifications, it ensures seamless integration of nested translation structures and attribute-based localization.

npm GitHub package version NPM Downloads

1.2kB gzipped

Demo


Install

$ yarn add language-observer

Import

import LanguageObserver from 'language-observer';

Usage

const languageObserver = new LanguageObserver();

languageObserver.init({ lang: 'eu' });

translations.js

globalThis.translations = globalThis.translations || {};

globalThis.translations['en'] = {
  app: {
    title: {
      main: "Application",
      settings: "Settings",
    },
    menu: {
      profile: "Profile",
    },
  },
  buttons: {
    save: "Save",
  },
};

globalThis.translations['es'] = {
  app: {
    title: {
      main: "Aplicación",
      settings: "Configuraciones",
    },
    menu: {
      profile: "Perfil",
    },
  },
  buttons: {
    save: "Guardar",
  },
};

Switching

document.querySelector('#change-lang-to-es')?.addEventListener('click', () => {
  languageObserver.loadLanguage('es');
});

HTML example

<p data-i18n="title.welcome"></p>

<img data-i18n-attr='{"alt": "image.altText"}' src="image.jpg" alt="Default Alt Text">

Options

OptionTypeDefaultDescription
langstring'en'(Optional) The language code to initialize with. If not provided, the language is detected from the <body> element's class.

Methods

MethodParametersReturnsDescription
init(options?)options: { lang?: string }voidInitializes the observer. If a lang is provided in options, it loads and applies that language's translations.
loadLanguage(lang)lang: stringPromiseLoads and applies translations for the specified language. If translations are not found, falls back to the default language.
applyTranslations()nonePromiseApplies the current translations to all elements with data-i18n and data-i18n-attr attributes on the page. Useful for updating translations after dynamic content changes.
updateTranslations()nonevoidManually updates translations on the page. Call this method after adding new translations to globalThis.translations to apply them without changing the language or reloading the page.
loadTranslations(lang, loader)lang: string loader: (lang) => PromisePromiseAsynchronously loads translations for the specified language using the provided loader function, then applies them if the language matches the current language.
getNestedTranslation(obj, path) (static method)obj: object path: stringanyRetrieves a nested translation value from an object using a dot-separated key path. Returns undefined if the key does not exist.

Example of using methods

Using the loadTranslations method

async function fetchTranslations(lang) {
  const response = await fetch(`/locales/${lang}.json`);

  return response.json();
}

languageObserver.loadTranslations('fr', fetchTranslations);

Using the getNestedTranslation method

const nestedValue = LanguageObserver.getNestedTranslation(
  globalThis.translations['en'],
  'app.menu.profile'
);

console.log(nestedValue);

License

language-observer is released under MIT license

Keywords

i18n

FAQs

Package last updated on 26 Sep 2025

Did you know?

Socket

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.

Install

Related posts