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.

dateUtil.js 3.0KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103
  1. // @flow
  2. import moment from 'moment';
  3. import i18next from './i18next';
  4. // allows for moment durations to be formatted
  5. import 'moment-duration-format';
  6. // MomentJS uses static language bundle loading, so in order to support dynamic
  7. // language selection in the app we need to load all bundles that we support in
  8. // the app.
  9. require('moment/locale/bg');
  10. require('moment/locale/de');
  11. require('moment/locale/eo');
  12. require('moment/locale/es');
  13. require('moment/locale/fa');
  14. require('moment/locale/fr');
  15. require('moment/locale/hy-am');
  16. require('moment/locale/it');
  17. require('moment/locale/nb');
  18. // OC is not available. Please submit OC translation to the MomentJS project.
  19. require('moment/locale/pl');
  20. require('moment/locale/pt');
  21. require('moment/locale/pt-br');
  22. require('moment/locale/ru');
  23. require('moment/locale/sk');
  24. require('moment/locale/sl');
  25. require('moment/locale/sv');
  26. require('moment/locale/tr');
  27. require('moment/locale/zh-cn');
  28. /**
  29. * Returns a localized date formatter initialized with a specific {@code Date}
  30. * or timestamp ({@code number}).
  31. *
  32. * @private
  33. * @param {Date | number} dateOrTimeStamp - The date or unix timestamp (ms)
  34. * to format.
  35. * @returns {Object}
  36. */
  37. export function getLocalizedDateFormatter(dateOrTimeStamp: Date | number) {
  38. return moment(dateOrTimeStamp).locale(_getSupportedLocale());
  39. }
  40. /**
  41. * Returns a localized duration formatter initialized with a
  42. * specific duration ({@code number}).
  43. *
  44. * @private
  45. * @param {number} duration - The duration (ms)
  46. * to format.
  47. * @returns {Object}
  48. */
  49. export function getLocalizedDurationFormatter(duration: number) {
  50. // FIXME The flow-type definition of moment is v2.3 while our package.json
  51. // states v2.19 so maybe locale on moment's duration was introduced in
  52. // between?
  53. //
  54. // If the conference is under an hour long we want to display it without
  55. // showing the hour and we want to include the hour if the conference is
  56. // more than an hour long
  57. if (moment.duration(duration).format('h') !== '0') {
  58. return moment.duration(duration).format('h:mm:ss');
  59. }
  60. return moment.duration(duration).format('mm:ss', { trim: false });
  61. }
  62. /**
  63. * A lenient locale matcher to match language and dialect if possible.
  64. *
  65. * @private
  66. * @returns {string}
  67. */
  68. function _getSupportedLocale() {
  69. const i18nLocale = i18next.language;
  70. let supportedLocale;
  71. if (i18nLocale) {
  72. const localeRegexp = new RegExp('^([a-z]{2,2})(-)*([a-z]{2,2})*$');
  73. const localeResult = localeRegexp.exec(i18nLocale.toLowerCase());
  74. if (localeResult) {
  75. const currentLocaleRegexp
  76. = new RegExp(
  77. `^${localeResult[1]}(-)*${`(${localeResult[3]})*` || ''}`);
  78. supportedLocale
  79. // FIXME The flow-type definition of moment is v2.3 while our
  80. // package.json states v2.19 so maybe locales on moment was
  81. // introduced in between?
  82. = moment.locales().find(lang => currentLocaleRegexp.exec(lang));
  83. }
  84. }
  85. return supportedLocale || 'en';
  86. }