You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

109 lines
2.8 KiB

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