GSI - Employe Self Service Mobile
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.

127 lines
3.1 KiB

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