localizify is very light and performant library for translation and localization in
Node.js and the browser.
Goals and features
- ❗️ Written on TypeScript
- 🚀 No dependencies (only 2KB gziped)
- 👯 Translation and localization
- 🎁 Interpolation of values to translations
- 🐧 Detection user language in browser and in server requests
- 📣 Events when locale was changed or translation isn't found
- 🗿 Easy scope system (nested-object translations)
- 🎰 A lot of examples
- react: you can use react-localizify, which provide high order component and hook, or write it yourself by example (see it in ./docs/usage-with-react)
You can install library from npm:
npm install localizify --save# or using yarnyarn add localizify
or download file (full version or minify bundle) from
dist folder and add the script to the page (only for browsers):
localizify returns instance of
Localizify, so it's singelton. You can add translations in one module and use it in another (but you can get
First of all you need add locales with translations and set locale by default:
const localizify = ;const en = ;const fr = ;localizify;
You can't set unknown locale (without translations):
const localizify = ;localizify;localizify; // en, because 'es' is unknown locale// to check that it's available localelocalizify; // falselocalizify; // true
Now for get translation by key you can use
const t = ;; // Hello world!; // hello, Alexander Morgunov; // How are you, Sasha?localizify;// if we haven't translition, return default message; // hello, Alexander Morgunov// if have; // Bonjour tout le monde!; // Сomment êtes-vous, Sasha?
If locale don't contain appropriate translation, return source interpolated key (key may be equal message) and emit event.
Translation as nested object
Translation data is organized as a nested object using the top-level key as namespace (scope or context):
The key argument can be a dot-separated key. See examples below:
; // Bot was turned off by Alex.; // Active; // Registration
scope (namespace) option can be either a single key or a dot-separated key. You can combinate keys and scopes as you wish:
; // Bot was turned off by Alex.; // Active; // Active
When translation is missing, localizify emit an event about it. You can listen it:
setLocale method emits an event you can listen to:
Register default scope and interpolations
You can set scope for your module by default:
localizify;; // Go to messenger; // Registrationlocalizify; // clear default scope
You can add translations for certain scope:
You can register default interpolations using the
registerInterpolations method. Interpolations you give as options to the translate method take precedence over registered interpolations.
localizify;localizify;; // Hello Alex in My Awesome App!; // Hello Alex in The Bar App!
See library API in index.d.ts.