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.

преди 3 години
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115
  1. import _classCallCheck from "@babel/runtime/helpers/classCallCheck";
  2. import _createClass from "@babel/runtime/helpers/createClass";
  3. import _defineProperty from "@babel/runtime/helpers/defineProperty";
  4. function ownKeys(object, enumerableOnly) { var keys = Object.keys(object); if (Object.getOwnPropertySymbols) { var symbols = Object.getOwnPropertySymbols(object); if (enumerableOnly) symbols = symbols.filter(function (sym) { return Object.getOwnPropertyDescriptor(object, sym).enumerable; }); keys.push.apply(keys, symbols); } return keys; }
  5. function _objectSpread(target) { for (var i = 1; i < arguments.length; i++) { var source = arguments[i] != null ? arguments[i] : {}; if (i % 2) { ownKeys(source, true).forEach(function (key) { _defineProperty(target, key, source[key]); }); } else if (Object.getOwnPropertyDescriptors) { Object.defineProperties(target, Object.getOwnPropertyDescriptors(source)); } else { ownKeys(source).forEach(function (key) { Object.defineProperty(target, key, Object.getOwnPropertyDescriptor(source, key)); }); } } return target; }
  6. import React from 'react';
  7. var defaultOptions = {
  8. bindI18n: 'languageChanging languageChanged',
  9. bindI18nStore: '',
  10. // nsMode: 'fallback' // loop through all namespaces given to hook, HOC, render prop for key lookup
  11. transEmptyNodeValue: '',
  12. transSupportBasicHtmlNodes: true,
  13. transKeepBasicHtmlNodesFor: ['br', 'strong', 'i', 'p'],
  14. // hashTransKey: key => key // calculate a key for Trans component based on defaultValue
  15. useSuspense: true
  16. };
  17. var i18nInstance;
  18. var hasUsedI18nextProvider;
  19. export var I18nContext = React.createContext();
  20. export function usedI18nextProvider(used) {
  21. hasUsedI18nextProvider = used;
  22. }
  23. export function getHasUsedI18nextProvider() {
  24. return hasUsedI18nextProvider;
  25. }
  26. export function setDefaults() {
  27. var options = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : {};
  28. defaultOptions = _objectSpread({}, defaultOptions, {}, options);
  29. }
  30. export function getDefaults() {
  31. return defaultOptions;
  32. }
  33. export var ReportNamespaces =
  34. /*#__PURE__*/
  35. function () {
  36. function ReportNamespaces() {
  37. _classCallCheck(this, ReportNamespaces);
  38. this.usedNamespaces = {};
  39. }
  40. _createClass(ReportNamespaces, [{
  41. key: "addUsedNamespaces",
  42. value: function addUsedNamespaces(namespaces) {
  43. var _this = this;
  44. namespaces.forEach(function (ns) {
  45. if (!_this.usedNamespaces[ns]) _this.usedNamespaces[ns] = true;
  46. });
  47. }
  48. }, {
  49. key: "getUsedNamespaces",
  50. value: function getUsedNamespaces() {
  51. return Object.keys(this.usedNamespaces);
  52. }
  53. }]);
  54. return ReportNamespaces;
  55. }();
  56. export function setI18n(instance) {
  57. i18nInstance = instance;
  58. }
  59. export function getI18n() {
  60. return i18nInstance;
  61. }
  62. export var initReactI18next = {
  63. type: '3rdParty',
  64. init: function init(instance) {
  65. setDefaults(instance.options.react);
  66. setI18n(instance);
  67. }
  68. };
  69. export function composeInitialProps(ForComponent) {
  70. return function (ctx) {
  71. return new Promise(function (resolve) {
  72. var i18nInitialProps = getInitialProps();
  73. if (ForComponent.getInitialProps) {
  74. ForComponent.getInitialProps(ctx).then(function (componentsInitialProps) {
  75. resolve(_objectSpread({}, componentsInitialProps, {}, i18nInitialProps));
  76. });
  77. } else {
  78. resolve(i18nInitialProps);
  79. }
  80. });
  81. }; // Avoid async for now - so we do not need to pull in regenerator
  82. // return async ctx => {
  83. // const componentsInitialProps = ForComponent.getInitialProps
  84. // ? await ForComponent.getInitialProps(ctx)
  85. // : {};
  86. // const i18nInitialProps = getInitialProps();
  87. // return {
  88. // ...componentsInitialProps,
  89. // ...i18nInitialProps,
  90. // };
  91. // };
  92. }
  93. export function getInitialProps() {
  94. var i18n = getI18n();
  95. var namespaces = i18n.reportNamespaces ? i18n.reportNamespaces.getUsedNamespaces() : [];
  96. var ret = {};
  97. var initialI18nStore = {};
  98. i18n.languages.forEach(function (l) {
  99. initialI18nStore[l] = {};
  100. namespaces.forEach(function (ns) {
  101. initialI18nStore[l][ns] = i18n.getResourceBundle(l, ns) || {};
  102. });
  103. });
  104. ret.initialI18nStore = initialI18nStore;
  105. ret.initialLanguage = i18n.language;
  106. return ret;
  107. }