1 | # y18n
|
---|
2 |
|
---|
3 | [![Build Status][travis-image]][travis-url]
|
---|
4 | [![Coverage Status][coveralls-image]][coveralls-url]
|
---|
5 | [![NPM version][npm-image]][npm-url]
|
---|
6 | [![js-standard-style][standard-image]][standard-url]
|
---|
7 | [![Conventional Commits](https://img.shields.io/badge/Conventional%20Commits-1.0.0-yellow.svg)](https://conventionalcommits.org)
|
---|
8 |
|
---|
9 | The bare-bones internationalization library used by yargs.
|
---|
10 |
|
---|
11 | Inspired by [i18n](https://www.npmjs.com/package/i18n).
|
---|
12 |
|
---|
13 | ## Examples
|
---|
14 |
|
---|
15 | _simple string translation:_
|
---|
16 |
|
---|
17 | ```js
|
---|
18 | var __ = require('y18n').__
|
---|
19 |
|
---|
20 | console.log(__('my awesome string %s', 'foo'))
|
---|
21 | ```
|
---|
22 |
|
---|
23 | output:
|
---|
24 |
|
---|
25 | `my awesome string foo`
|
---|
26 |
|
---|
27 | _using tagged template literals_
|
---|
28 |
|
---|
29 | ```js
|
---|
30 | var __ = require('y18n').__
|
---|
31 | var str = 'foo'
|
---|
32 |
|
---|
33 | console.log(__`my awesome string ${str}`)
|
---|
34 | ```
|
---|
35 |
|
---|
36 | output:
|
---|
37 |
|
---|
38 | `my awesome string foo`
|
---|
39 |
|
---|
40 | _pluralization support:_
|
---|
41 |
|
---|
42 | ```js
|
---|
43 | var __n = require('y18n').__n
|
---|
44 |
|
---|
45 | console.log(__n('one fish %s', '%d fishes %s', 2, 'foo'))
|
---|
46 | ```
|
---|
47 |
|
---|
48 | output:
|
---|
49 |
|
---|
50 | `2 fishes foo`
|
---|
51 |
|
---|
52 | ## JSON Language Files
|
---|
53 |
|
---|
54 | The JSON language files should be stored in a `./locales` folder.
|
---|
55 | File names correspond to locales, e.g., `en.json`, `pirate.json`.
|
---|
56 |
|
---|
57 | When strings are observed for the first time they will be
|
---|
58 | added to the JSON file corresponding to the current locale.
|
---|
59 |
|
---|
60 | ## Methods
|
---|
61 |
|
---|
62 | ### require('y18n')(config)
|
---|
63 |
|
---|
64 | Create an instance of y18n with the config provided, options include:
|
---|
65 |
|
---|
66 | * `directory`: the locale directory, default `./locales`.
|
---|
67 | * `updateFiles`: should newly observed strings be updated in file, default `true`.
|
---|
68 | * `locale`: what locale should be used.
|
---|
69 | * `fallbackToLanguage`: should fallback to a language-only file (e.g. `en.json`)
|
---|
70 | be allowed if a file matching the locale does not exist (e.g. `en_US.json`),
|
---|
71 | default `true`.
|
---|
72 |
|
---|
73 | ### y18n.\_\_(str, arg, arg, arg)
|
---|
74 |
|
---|
75 | Print a localized string, `%s` will be replaced with `arg`s.
|
---|
76 |
|
---|
77 | This function can also be used as a tag for a template literal. You can use it
|
---|
78 | like this: <code>__`hello ${'world'}`</code>. This will be equivalent to
|
---|
79 | `__('hello %s', 'world')`.
|
---|
80 |
|
---|
81 | ### y18n.\_\_n(singularString, pluralString, count, arg, arg, arg)
|
---|
82 |
|
---|
83 | Print a localized string with appropriate pluralization. If `%d` is provided
|
---|
84 | in the string, the `count` will replace this placeholder.
|
---|
85 |
|
---|
86 | ### y18n.setLocale(str)
|
---|
87 |
|
---|
88 | Set the current locale being used.
|
---|
89 |
|
---|
90 | ### y18n.getLocale()
|
---|
91 |
|
---|
92 | What locale is currently being used?
|
---|
93 |
|
---|
94 | ### y18n.updateLocale(obj)
|
---|
95 |
|
---|
96 | Update the current locale with the key value pairs in `obj`.
|
---|
97 |
|
---|
98 | ## License
|
---|
99 |
|
---|
100 | ISC
|
---|
101 |
|
---|
102 | [travis-url]: https://travis-ci.org/yargs/y18n
|
---|
103 | [travis-image]: https://img.shields.io/travis/yargs/y18n.svg
|
---|
104 | [coveralls-url]: https://coveralls.io/github/yargs/y18n
|
---|
105 | [coveralls-image]: https://img.shields.io/coveralls/yargs/y18n.svg
|
---|
106 | [npm-url]: https://npmjs.org/package/y18n
|
---|
107 | [npm-image]: https://img.shields.io/npm/v/y18n.svg
|
---|
108 | [standard-image]: https://img.shields.io/badge/code%20style-standard-brightgreen.svg
|
---|
109 | [standard-url]: https://github.com/feross/standard
|
---|