1# y18n
2
3[![NPM version][npm-image]][npm-url]
4[![js-standard-style][standard-image]][standard-url]
5[![Conventional Commits](https://img.shields.io/badge/Conventional%20Commits-1.0.0-yellow.svg)](https://conventionalcommits.org)
6
7The bare-bones internationalization library used by yargs.
8
9Inspired by [i18n](https://www.npmjs.com/package/i18n).
10
11## Examples
12
13_simple string translation:_
14
15```js
16const __ = require('y18n')().__;
17
18console.log(__('my awesome string %s', 'foo'));
19```
20
21output:
22
23`my awesome string foo`
24
25_using tagged template literals_
26
27```js
28const __ = require('y18n')().__;
29
30const str = 'foo';
31
32console.log(__`my awesome string ${str}`);
33```
34
35output:
36
37`my awesome string foo`
38
39_pluralization support:_
40
41```js
42const __n = require('y18n')().__n;
43
44console.log(__n('one fish %s', '%d fishes %s', 2, 'foo'));
45```
46
47output:
48
49`2 fishes foo`
50
51## Deno Example
52
53As of `v5` `y18n` supports [Deno](https://github.com/denoland/deno):
54
55```typescript
56import y18n from "https://deno.land/x/y18n/deno.ts";
57
58const __ = y18n({
59  locale: 'pirate',
60  directory: './test/locales'
61}).__
62
63console.info(__`Hi, ${'Ben'} ${'Coe'}!`)
64```
65
66You will need to run with `--allow-read` to load alternative locales.
67
68## JSON Language Files
69
70The JSON language files should be stored in a `./locales` folder.
71File names correspond to locales, e.g., `en.json`, `pirate.json`.
72
73When strings are observed for the first time they will be
74added to the JSON file corresponding to the current locale.
75
76## Methods
77
78### require('y18n')(config)
79
80Create an instance of y18n with the config provided, options include:
81
82* `directory`: the locale directory, default `./locales`.
83* `updateFiles`: should newly observed strings be updated in file, default `true`.
84* `locale`: what locale should be used.
85* `fallbackToLanguage`: should fallback to a language-only file (e.g. `en.json`)
86  be allowed if a file matching the locale does not exist (e.g. `en_US.json`),
87  default `true`.
88
89### y18n.\_\_(str, arg, arg, arg)
90
91Print a localized string, `%s` will be replaced with `arg`s.
92
93This function can also be used as a tag for a template literal. You can use it
94like this: <code>__&#96;hello ${'world'}&#96;</code>. This will be equivalent to
95`__('hello %s', 'world')`.
96
97### y18n.\_\_n(singularString, pluralString, count, arg, arg, arg)
98
99Print a localized string with appropriate pluralization. If `%d` is provided
100in the string, the `count` will replace this placeholder.
101
102### y18n.setLocale(str)
103
104Set the current locale being used.
105
106### y18n.getLocale()
107
108What locale is currently being used?
109
110### y18n.updateLocale(obj)
111
112Update the current locale with the key value pairs in `obj`.
113
114## Supported Node.js Versions
115
116Libraries in this ecosystem make a best effort to track
117[Node.js' release schedule](https://nodejs.org/en/about/releases/). Here's [a
118post on why we think this is important](https://medium.com/the-node-js-collection/maintainers-should-consider-following-node-js-release-schedule-ab08ed4de71a).
119
120## License
121
122ISC
123
124[npm-url]: https://npmjs.org/package/y18n
125[npm-image]: https://img.shields.io/npm/v/y18n.svg
126[standard-image]: https://img.shields.io/badge/code%20style-standard-brightgreen.svg
127[standard-url]: https://github.com/feross/standard
128