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.

i18n.md 2.8 KiB

3 years ago
1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192
  1. # Internationalization
  2. ## Table of Contents
  3. - [Usage](#usage)
  4. - [Adding New Language](#adding-new-language)
  5. ## Usage
  6. There are just a few functions that support I18n:
  7. - [`format`](https://date-fns.org/docs/format)
  8. - [`formatDistance`](https://date-fns.org/docs/formatDistance)
  9. - [`formatDistanceStrict`](https://date-fns.org/docs/formatDistanceStrict)
  10. - [`formatRelative`](https://date-fns.org/docs/formatRelative)
  11. To use a locale, you need to require it and then pass
  12. as an option to a function:
  13. ```js
  14. import { formatDistance } from 'date-fns'
  15. // Require Esperanto locale
  16. import { eo } from 'date-fns/locale'
  17. const result = formatDistance(
  18. new Date(2016, 7, 1),
  19. new Date(2015, 0, 1),
  20. {locale: eo} // Pass the locale as an option
  21. )
  22. //=> 'pli ol 1 jaro'
  23. ```
  24. It might seem complicated to require and pass locales as options,
  25. but unlike Moment.js which bloats your build with all the locales
  26. by default date-fns forces developer to manually require locales when needed.
  27. To make API simple, we encourage you to write tiny wrappers and use those
  28. instead of original functions:
  29. ```js
  30. // app/_lib/format.js
  31. import { format } from 'date-fns'
  32. import { enGB, eo, ru } from 'date-fns/locale'
  33. const locales = {enGB, eo, ru}
  34. // by providing a default string of 'PP' or any of its variants for `formatStr`
  35. // it will format dates in whichever way is appropriate to the locale
  36. export default function (date, formatStr = 'PP') {
  37. return format(date, formatStr, {
  38. locale: locales[window.__localeId__] // or global.__localeId__
  39. })
  40. }
  41. // Later:
  42. import format from 'app/_lib/format'
  43. window.__localeId__ = 'en'
  44. format(friday13, 'EEEE d')
  45. //=> 'Friday 13'
  46. window.__localeId__ = 'eo'
  47. format(friday13, 'EEEE d')
  48. //=> 'vendredo 13'
  49. // If the format string is omitted, it will take the default for the locale.
  50. window.__localeId__ = 'en'
  51. format(friday13)
  52. //=> Jul 13, 2019
  53. window.__localeId__ = 'eo'
  54. format(friday13)
  55. //=> 2019-jul-13
  56. ```
  57. ## Adding New Language
  58. At the moment there is no definitive guide, so if you feel brave enough,
  59. use this quick guide:
  60. - First of all, [create an issue](https://github.com/date-fns/date-fns/issues/new?title=XXX%20language%20support)
  61. so you won't overlap with others.
  62. - A detailed explanation of how to [add a new locale](https://github.com/date-fns/date-fns/blob/master/docs/i18nContributionGuide.md#adding-a-new-locale).
  63. - Use [English locale](https://github.com/date-fns/date-fns/tree/master/src/locale/en-US)
  64. as the basis and then incrementally adjust the tests and the code.
  65. - Directions on [adding a locale with the same language as another locale](https://github.com/date-fns/date-fns/blob/master/docs/i18nContributionGuide.md#creating-a-locale-with-the-same-language-as-another-locale).
  66. - If you have questions or need guidance, leave a comment in the issue.
  67. Thank you for your support!