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.

README.md 2.3KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091
  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. The bare-bones internationalization library used by yargs.
  7. Inspired by [i18n](https://www.npmjs.com/package/i18n).
  8. ## Examples
  9. _simple string translation:_
  10. ```js
  11. var __ = require('y18n').__
  12. console.log(__('my awesome string %s', 'foo'))
  13. ```
  14. output:
  15. `my awesome string foo`
  16. _pluralization support:_
  17. ```js
  18. var __n = require('y18n').__n
  19. console.log(__n('one fish %s', '%d fishes %s', 2, 'foo'))
  20. ```
  21. output:
  22. `2 fishes foo`
  23. ## JSON Language Files
  24. The JSON language files should be stored in a `./locales` folder.
  25. File names correspond to locales, e.g., `en.json`, `pirate.json`.
  26. When strings are observed for the first time they will be
  27. added to the JSON file corresponding to the current locale.
  28. ## Methods
  29. ### require('y18n')(config)
  30. Create an instance of y18n with the config provided, options include:
  31. * `directory`: the locale directory, default `./locales`.
  32. * `updateFiles`: should newly observed strings be updated in file, default `true`.
  33. * `locale`: what locale should be used.
  34. * `fallbackToLanguage`: should fallback to a language-only file (e.g. `en.json`)
  35. be allowed if a file matching the locale does not exist (e.g. `en_US.json`),
  36. default `true`.
  37. ### y18n.\_\_(str, arg, arg, arg)
  38. Print a localized string, `%s` will be replaced with `arg`s.
  39. ### y18n.\_\_n(singularString, pluralString, count, arg, arg, arg)
  40. Print a localized string with appropriate pluralization. If `%d` is provided
  41. in the string, the `count` will replace this placeholder.
  42. ### y18n.setLocale(str)
  43. Set the current locale being used.
  44. ### y18n.getLocale()
  45. What locale is currently being used?
  46. ### y18n.updateLocale(obj)
  47. Update the current locale with the key value pairs in `obj`.
  48. ## License
  49. ISC
  50. [travis-url]: https://travis-ci.org/yargs/y18n
  51. [travis-image]: https://img.shields.io/travis/yargs/y18n.svg
  52. [coveralls-url]: https://coveralls.io/github/yargs/y18n
  53. [coveralls-image]: https://img.shields.io/coveralls/yargs/y18n.svg
  54. [npm-url]: https://npmjs.org/package/y18n
  55. [npm-image]: https://img.shields.io/npm/v/y18n.svg
  56. [standard-image]: https://img.shields.io/badge/code%20style-standard-brightgreen.svg
  57. [standard-url]: https://github.com/feross/standard