Nevar pievienot vairāk kā 25 tēmas Tēmai ir jāsākas ar burtu vai ciparu, tā var saturēt domu zīmes ('-') un var būt līdz 35 simboliem gara.

dateUtil.js 2.4KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384
  1. // @flow
  2. import moment from 'moment';
  3. import { i18next } from '../i18n';
  4. // MomentJS uses static language bundle loading, so in order to support dynamic
  5. // language selection in the app we need to load all bundles that we support in
  6. // the app.
  7. // FIXME: If we decide to support MomentJS in other features as well we may need
  8. // to move this import and the lenient matcher to the i18n feature.
  9. require('moment/locale/bg');
  10. require('moment/locale/de');
  11. require('moment/locale/eo');
  12. require('moment/locale/es');
  13. require('moment/locale/fr');
  14. require('moment/locale/hy-am');
  15. require('moment/locale/it');
  16. require('moment/locale/nb');
  17. // OC is not available. Please submit OC translation to the MomentJS project.
  18. require('moment/locale/pl');
  19. require('moment/locale/pt');
  20. require('moment/locale/pt-br');
  21. require('moment/locale/ru');
  22. require('moment/locale/sk');
  23. require('moment/locale/sl');
  24. require('moment/locale/sv');
  25. require('moment/locale/tr');
  26. require('moment/locale/zh-cn');
  27. /**
  28. * Returns a localized date formatter initialized with a specific {@code Date}
  29. * or time stamp ({@code number}).
  30. *
  31. * @private
  32. * @param {Date | number} dateOrTimeStamp - The date or unix timestamp (ms)
  33. * to format.
  34. * @returns {Object}
  35. */
  36. export function getLocalizedDateFormatter(dateOrTimeStamp: Date | number) {
  37. return moment(dateOrTimeStamp).locale(_getSupportedLocale());
  38. }
  39. /**
  40. * Returns a localized duration formatter initialized with a
  41. * specific duration ({@code number}).
  42. *
  43. * @private
  44. * @param {number} duration - The duration (ms)
  45. * to format.
  46. * @returns {Object}
  47. */
  48. export function getLocalizedDurationFormatter(duration: number) {
  49. return moment.duration(duration).locale(_getSupportedLocale());
  50. }
  51. /**
  52. * A lenient locale matcher to match language and dialect if possible.
  53. *
  54. * @private
  55. * @returns {string}
  56. */
  57. function _getSupportedLocale() {
  58. const i18nLocale = i18next.language;
  59. let supportedLocale;
  60. if (i18nLocale) {
  61. const localeRegexp = new RegExp('^([a-z]{2,2})(-)*([a-z]{2,2})*$');
  62. const localeResult = localeRegexp.exec(i18nLocale.toLowerCase());
  63. if (localeResult) {
  64. const currentLocaleRegexp
  65. = new RegExp(
  66. `^${localeResult[1]}(-)*${`(${localeResult[3]})*` || ''}`);
  67. supportedLocale
  68. = moment.locales().find(lang => currentLocaleRegexp.exec(lang));
  69. }
  70. }
  71. return supportedLocale || 'en';
  72. }