Important: This documentation covers Yarn 1 (Classic).
For Yarn 2+ docs and migration guide, see yarnpkg.com.

Package detail

y18n

yargs297.7mISC5.0.8TypeScript support: definitely-typed

the bare-bones internationalization library used by yargs

i18n, internationalization, yargs

readme

y18n

NPM version js-standard-style Conventional Commits

The bare-bones internationalization library used by yargs.

Inspired by i18n.

Examples

simple string translation:

const __ = require('y18n')().__;

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

output:

my awesome string foo

using tagged template literals

const __ = require('y18n')().__;

const str = 'foo';

console.log(__`my awesome string ${str}`);

output:

my awesome string foo

pluralization support:

const __n = require('y18n')().__n;

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

output:

2 fishes foo

Deno Example

As of v5 y18n supports Deno:

import y18n from "https://deno.land/x/y18n/deno.ts";

const __ = y18n({
  locale: 'pirate',
  directory: './test/locales'
}).__

console.info(__`Hi, ${'Ben'} ${'Coe'}!`)

You will need to run with --allow-read to load alternative locales.

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.

This function can also be used as a tag for a template literal. You can use it like this: `hello ${'world'}`. This will be equivalent to `('hello %s', 'world')`.

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.

Supported Node.js Versions

Libraries in this ecosystem make a best effort to track Node.js' release schedule. Here's a post on why we think this is important.

License

ISC

changelog

Change Log

All notable changes to this project will be documented in this file. See standard-version for commit guidelines.

5.0.8 (2021-04-07)

Bug Fixes

  • deno: force modern release for Deno (b1c215a)

5.0.7 (2021-04-07)

Bug Fixes

5.0.6 (2021-04-05)

Bug Fixes

  • webpack: skip readFileSync if not defined (#117) (6966fa9)

5.0.5 (2020-10-25)

Bug Fixes

5.0.4 (2020-10-16)

Bug Fixes

  • exports: node 13.0 and 13.1 require the dotted object form with a string fallback (#105) (4f85d80)

5.0.3 (2020-10-16)

Bug Fixes

  • exports: node 13.0-13.6 require a string fallback (#103) (e39921e)

5.0.2 (2020-10-01)

Bug Fixes

5.0.1 (2020-09-05)

Bug Fixes

5.0.0 (2020-09-05)

⚠ BREAKING CHANGES

  • exports maps are now used, which modifies import behavior.
  • drops Node 6 and 4. begin following Node.js LTS schedule (#89)

Features

Build System

  • drops Node 6 and 4. begin following Node.js LTS schedule (#89) (3cc0c28)

4.0.1 (2020-10-25)

Bug Fixes

4.0.0 (2017-10-10)

Bug Fixes

  • allow support for falsy values like 0 in tagged literal (#45) (c926123)

Features

  • __: added tagged template literal support (#44) (0598daf)

BREAKING CHANGES

  • __: dropping Node 0.10/Node 0.12 support