Ви не можете вибрати більше 25 тем Теми мають розпочинатися з літери або цифри, можуть містити дефіси (-) і не повинні перевищувати 35 символів.

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071
  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. * A lenient locale matcher to match language and dialect if possible.
  41. *
  42. * @private
  43. * @returns {string}
  44. */
  45. function _getSupportedLocale() {
  46. const i18nLocale = i18next.language;
  47. let supportedLocale;
  48. if (i18nLocale) {
  49. const localeRegexp = new RegExp('^([a-z]{2,2})(-)*([a-z]{2,2})*$');
  50. const localeResult = localeRegexp.exec(i18nLocale.toLowerCase());
  51. if (localeResult) {
  52. const currentLocaleRegexp
  53. = new RegExp(
  54. `^${localeResult[1]}(-)*${`(${localeResult[3]})*` || ''}`);
  55. supportedLocale
  56. = moment.locales().find(lang => currentLocaleRegexp.exec(lang));
  57. }
  58. }
  59. return supportedLocale || 'en';
  60. }