[6a3a178] | 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 |
|
---|
| 7 | The bare-bones internationalization library used by yargs.
|
---|
| 8 |
|
---|
| 9 | Inspired by [i18n](https://www.npmjs.com/package/i18n).
|
---|
| 10 |
|
---|
| 11 | ## Examples
|
---|
| 12 |
|
---|
| 13 | _simple string translation:_
|
---|
| 14 |
|
---|
| 15 | ```js
|
---|
| 16 | const __ = require('y18n')().__;
|
---|
| 17 |
|
---|
| 18 | console.log(__('my awesome string %s', 'foo'));
|
---|
| 19 | ```
|
---|
| 20 |
|
---|
| 21 | output:
|
---|
| 22 |
|
---|
| 23 | `my awesome string foo`
|
---|
| 24 |
|
---|
| 25 | _using tagged template literals_
|
---|
| 26 |
|
---|
| 27 | ```js
|
---|
| 28 | const __ = require('y18n')().__;
|
---|
| 29 |
|
---|
| 30 | const str = 'foo';
|
---|
| 31 |
|
---|
| 32 | console.log(__`my awesome string ${str}`);
|
---|
| 33 | ```
|
---|
| 34 |
|
---|
| 35 | output:
|
---|
| 36 |
|
---|
| 37 | `my awesome string foo`
|
---|
| 38 |
|
---|
| 39 | _pluralization support:_
|
---|
| 40 |
|
---|
| 41 | ```js
|
---|
| 42 | const __n = require('y18n')().__n;
|
---|
| 43 |
|
---|
| 44 | console.log(__n('one fish %s', '%d fishes %s', 2, 'foo'));
|
---|
| 45 | ```
|
---|
| 46 |
|
---|
| 47 | output:
|
---|
| 48 |
|
---|
| 49 | `2 fishes foo`
|
---|
| 50 |
|
---|
| 51 | ## Deno Example
|
---|
| 52 |
|
---|
| 53 | As of `v5` `y18n` supports [Deno](https://github.com/denoland/deno):
|
---|
| 54 |
|
---|
| 55 | ```typescript
|
---|
| 56 | import y18n from "https://deno.land/x/y18n/deno.ts";
|
---|
| 57 |
|
---|
| 58 | const __ = y18n({
|
---|
| 59 | locale: 'pirate',
|
---|
| 60 | directory: './test/locales'
|
---|
| 61 | }).__
|
---|
| 62 |
|
---|
| 63 | console.info(__`Hi, ${'Ben'} ${'Coe'}!`)
|
---|
| 64 | ```
|
---|
| 65 |
|
---|
| 66 | You will need to run with `--allow-read` to load alternative locales.
|
---|
| 67 |
|
---|
| 68 | ## JSON Language Files
|
---|
| 69 |
|
---|
| 70 | The JSON language files should be stored in a `./locales` folder.
|
---|
| 71 | File names correspond to locales, e.g., `en.json`, `pirate.json`.
|
---|
| 72 |
|
---|
| 73 | When strings are observed for the first time they will be
|
---|
| 74 | added to the JSON file corresponding to the current locale.
|
---|
| 75 |
|
---|
| 76 | ## Methods
|
---|
| 77 |
|
---|
| 78 | ### require('y18n')(config)
|
---|
| 79 |
|
---|
| 80 | Create 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 |
|
---|
| 91 | Print a localized string, `%s` will be replaced with `arg`s.
|
---|
| 92 |
|
---|
| 93 | This function can also be used as a tag for a template literal. You can use it
|
---|
| 94 | like this: <code>__`hello ${'world'}`</code>. This will be equivalent to
|
---|
| 95 | `__('hello %s', 'world')`.
|
---|
| 96 |
|
---|
| 97 | ### y18n.\_\_n(singularString, pluralString, count, arg, arg, arg)
|
---|
| 98 |
|
---|
| 99 | Print a localized string with appropriate pluralization. If `%d` is provided
|
---|
| 100 | in the string, the `count` will replace this placeholder.
|
---|
| 101 |
|
---|
| 102 | ### y18n.setLocale(str)
|
---|
| 103 |
|
---|
| 104 | Set the current locale being used.
|
---|
| 105 |
|
---|
| 106 | ### y18n.getLocale()
|
---|
| 107 |
|
---|
| 108 | What locale is currently being used?
|
---|
| 109 |
|
---|
| 110 | ### y18n.updateLocale(obj)
|
---|
| 111 |
|
---|
| 112 | Update the current locale with the key value pairs in `obj`.
|
---|
| 113 |
|
---|
| 114 | ## Supported Node.js Versions
|
---|
| 115 |
|
---|
| 116 | Libraries in this ecosystem make a best effort to track
|
---|
| 117 | [Node.js' release schedule](https://nodejs.org/en/about/releases/). Here's [a
|
---|
| 118 | post 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 |
|
---|
| 122 | ISC
|
---|
| 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
|
---|