Socket
Socket
Sign inDemoInstall

y18n

Package Overview
Dependencies
0
Maintainers
2
Versions
22
Alerts
File Explorer

Advanced tools

Install Socket

Detect and block malicious and high-risk dependencies

Install

    y18n

the bare-bones internationalization library used by yargs


Version published
Weekly downloads
61M
increased by1.66%
Maintainers
2
Install size
9.23 kB
Created
Weekly downloads
 

Package description

What is y18n?

The y18n npm package is a library that provides internationalization (i18n) support for your Node.js applications. It allows you to define translations for your application's text strings and switch between different languages at runtime based on user preferences or other criteria.

What are y18n's main functionalities?

Locale String Translation

This feature allows you to translate strings into different locales. You define a set of translations for each language, and y18n will replace placeholders with the appropriate translations.

{"en": {"My cat is %s": "My cat is %s"}, "es": {"My cat is %s": "Mi gato es %s"}}

Pluralization

y18n supports pluralization, which means it can handle different translations based on the number of items. This is useful for languages that have different word forms depending on the count.

{"en": {"cat": {"one": "%d cat", "other": "%d cats"}}, "es": {"cat": {"one": "%d gato", "other": "%d gatos"}}}

Locale Switching

With y18n, you can switch the active locale at runtime, which allows your application to change languages on the fly based on user input or other conditions.

y18n.setLocale('es');

Other packages similar to y18n

Readme

Source

y18n

Build Status Coverage Status NPM version js-standard-style

The bare-bones internationalization library used by yargs.

Inspired by i18n.

Examples

simple string translation:

var __ = require('y18n').__

console.log(__('my awesome string %s', 'foo'))

output:

my awesome string foo

pluralization support:

var __n = require('y18n').__n

console.log(__n('one fish %s', '%d fishes %s', 2, 'foo'))

output:

2 fishes foo

JSON Language Files

The JSON language files should be stored in a ./locales folder. File names correspond to locales, e.g., en.json, pirate.json.

When strings are observed for the first time they will be added to the JSON file corresponding to the current locale.

Methods

require('y18n')(config)

Create an instance of y18n with the config provided, options include:

  • directory: the locale directory, default ./locales.
  • updateFiles: should newly observed strings be updated in file, default true.
  • locale: what locale should be used.
  • fallbackToLanguage: should fallback to a language-only file (e.g. en.json) be allowed if a file matching the locale does not exist (e.g. en_US.json), default true.

y18n.__(str, arg, arg, arg)

Print a localized string, %s will be replaced with args.

y18n.__n(singularString, pluralString, count, arg, arg, arg)

Print a localized string with appropriate pluralization. If %d is provided in the string, the count will replace this placeholder.

y18n.setLocale(str)

Set the current locale being used.

y18n.getLocale()

What locale is currently being used?

y18n.updateLocale(obj)

Update the current locale with the key value pairs in obj.

License

ISC

Keywords

FAQs

Last updated on 17 Mar 2016

Did you know?

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

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc