context.js 2.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081
  1. "use strict";
  2. Object.defineProperty(exports, "__esModule", {
  3. value: true
  4. });
  5. exports.composeInitialProps = exports.ReportNamespaces = exports.I18nContext = void 0;
  6. Object.defineProperty(exports, "getDefaults", {
  7. enumerable: true,
  8. get: function () {
  9. return _defaults.getDefaults;
  10. }
  11. });
  12. Object.defineProperty(exports, "getI18n", {
  13. enumerable: true,
  14. get: function () {
  15. return _i18nInstance.getI18n;
  16. }
  17. });
  18. exports.getInitialProps = void 0;
  19. Object.defineProperty(exports, "initReactI18next", {
  20. enumerable: true,
  21. get: function () {
  22. return _initReactI18next.initReactI18next;
  23. }
  24. });
  25. Object.defineProperty(exports, "setDefaults", {
  26. enumerable: true,
  27. get: function () {
  28. return _defaults.setDefaults;
  29. }
  30. });
  31. Object.defineProperty(exports, "setI18n", {
  32. enumerable: true,
  33. get: function () {
  34. return _i18nInstance.setI18n;
  35. }
  36. });
  37. var _react = require("react");
  38. var _defaults = require("./defaults.js");
  39. var _i18nInstance = require("./i18nInstance.js");
  40. var _initReactI18next = require("./initReactI18next.js");
  41. const I18nContext = exports.I18nContext = (0, _react.createContext)();
  42. class ReportNamespaces {
  43. constructor() {
  44. this.usedNamespaces = {};
  45. }
  46. addUsedNamespaces(namespaces) {
  47. namespaces.forEach(ns => {
  48. if (!this.usedNamespaces[ns]) this.usedNamespaces[ns] = true;
  49. });
  50. }
  51. getUsedNamespaces() {
  52. return Object.keys(this.usedNamespaces);
  53. }
  54. }
  55. exports.ReportNamespaces = ReportNamespaces;
  56. const composeInitialProps = ForComponent => async ctx => {
  57. const componentsInitialProps = (await ForComponent.getInitialProps?.(ctx)) ?? {};
  58. const i18nInitialProps = getInitialProps();
  59. return {
  60. ...componentsInitialProps,
  61. ...i18nInitialProps
  62. };
  63. };
  64. exports.composeInitialProps = composeInitialProps;
  65. const getInitialProps = () => {
  66. const i18n = (0, _i18nInstance.getI18n)();
  67. const namespaces = i18n.reportNamespaces?.getUsedNamespaces() ?? [];
  68. const ret = {};
  69. const initialI18nStore = {};
  70. i18n.languages.forEach(l => {
  71. initialI18nStore[l] = {};
  72. namespaces.forEach(ns => {
  73. initialI18nStore[l][ns] = i18n.getResourceBundle(l, ns) || {};
  74. });
  75. });
  76. ret.initialI18nStore = initialI18nStore;
  77. ret.initialLanguage = i18n.language;
  78. return ret;
  79. };
  80. exports.getInitialProps = getInitialProps;