🚀 Socket Launch Week 🚀 Day 4: Introducing Historical Analytics.Learn More
Socket
Sign inDemoInstall
Socket

node-polyglot

Package Overview
Dependencies
Maintainers
6
Versions
27
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

node-polyglot

Give your JavaScript the ability to speak many languages.

2.6.0
latest
Source
npm
Version published
Weekly downloads
142K
-27.85%
Maintainers
6
Weekly downloads
 
Created

What is node-polyglot?

node-polyglot is a lightweight internationalization library for JavaScript, designed to handle translation and pluralization of strings. It is particularly useful for applications that need to support multiple languages and locales.

What are node-polyglot's main functionalities?

Basic Translation

This feature allows you to define and retrieve translations for specific phrases. The `t` method is used to fetch the translated string.

const Polyglot = require('node-polyglot');
const polyglot = new Polyglot({ phrases: { 'hello': 'Hello' } });
console.log(polyglot.t('hello')); // Output: Hello

Interpolation

Interpolation allows you to insert dynamic values into your translated strings. The placeholders in the phrases are replaced with the provided values.

const Polyglot = require('node-polyglot');
const polyglot = new Polyglot({ phrases: { 'hello_name': 'Hello, %{name}' } });
console.log(polyglot.t('hello_name', { name: 'John' })); // Output: Hello, John

Pluralization

Pluralization allows you to handle singular and plural forms of words based on a count. The `smart_count` variable is used to determine which form to use.

const Polyglot = require('node-polyglot');
const polyglot = new Polyglot({ phrases: { 'num_cars': '%{smart_count} car |||| %{smart_count} cars' } });
console.log(polyglot.t('num_cars', { smart_count: 1 })); // Output: 1 car
console.log(polyglot.t('num_cars', { smart_count: 2 })); // Output: 2 cars

Locale Switching

Locale switching allows you to change the language of your application dynamically. You can extend the phrases for the new locale and switch to it using the `locale` method.

const Polyglot = require('node-polyglot');
const polyglot = new Polyglot({ phrases: { 'hello': 'Hello' }, locale: 'en' });
polyglot.locale('es');
polyglot.extend({ 'hello': 'Hola' });
console.log(polyglot.t('hello')); // Output: Hola

Other packages similar to node-polyglot

Keywords

i18n

FAQs

Package last updated on 11 Jul 2024

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