I18nProvider.js 3.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364
  1. "use strict";
  2. var __assign = (this && this.__assign) || function () {
  3. __assign = Object.assign || function(t) {
  4. for (var s, i = 1, n = arguments.length; i < n; i++) {
  5. s = arguments[i];
  6. for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p))
  7. t[p] = s[p];
  8. }
  9. return t;
  10. };
  11. return __assign.apply(this, arguments);
  12. };
  13. var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
  14. if (k2 === undefined) k2 = k;
  15. Object.defineProperty(o, k2, { enumerable: true, get: function() { return m[k]; } });
  16. }) : (function(o, m, k, k2) {
  17. if (k2 === undefined) k2 = k;
  18. o[k2] = m[k];
  19. }));
  20. var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
  21. Object.defineProperty(o, "default", { enumerable: true, value: v });
  22. }) : function(o, v) {
  23. o["default"] = v;
  24. });
  25. var __importStar = (this && this.__importStar) || function (mod) {
  26. if (mod && mod.__esModule) return mod;
  27. var result = {};
  28. if (mod != null) for (var k in mod) if (k !== "default" && Object.prototype.hasOwnProperty.call(mod, k)) __createBinding(result, mod, k);
  29. __setModuleDefault(result, mod);
  30. return result;
  31. };
  32. var __importDefault = (this && this.__importDefault) || function (mod) {
  33. return (mod && mod.__esModule) ? mod : { "default": mod };
  34. };
  35. Object.defineProperty(exports, "__esModule", { value: true });
  36. exports.InternalContext = void 0;
  37. var react_1 = __importStar(require("react"));
  38. var router_1 = require("next/router");
  39. var _context_1 = __importDefault(require("./_context"));
  40. var transCore_1 = __importDefault(require("./transCore"));
  41. var useTranslation_1 = __importDefault(require("./useTranslation"));
  42. exports.InternalContext = (0, react_1.createContext)({ ns: {}, config: {} });
  43. function I18nProvider(_a) {
  44. var lng = _a.lang, _b = _a.namespaces, namespaces = _b === void 0 ? {} : _b, children = _a.children, _c = _a.config, newConfig = _c === void 0 ? {} : _c;
  45. var parentLang = (0, useTranslation_1.default)().lang;
  46. var _d = (0, router_1.useRouter)() || {}, locale = _d.locale, defaultLocale = _d.defaultLocale;
  47. var internal = (0, react_1.useContext)(exports.InternalContext);
  48. var allNamespaces = __assign(__assign(__assign({}, initialBrowserNamespaces()), internal.ns), namespaces);
  49. var lang = lng || parentLang || locale || defaultLocale || '';
  50. var config = __assign(__assign({}, internal.config), newConfig);
  51. var localesToIgnore = config.localesToIgnore || ['default'];
  52. var ignoreLang = localesToIgnore.includes(lang);
  53. var pluralRules = new Intl.PluralRules(ignoreLang ? undefined : lang);
  54. var t = (0, transCore_1.default)({ config: config, allNamespaces: allNamespaces, pluralRules: pluralRules, lang: lang });
  55. return (react_1.default.createElement(_context_1.default.Provider, { value: { lang: lang, t: t } },
  56. react_1.default.createElement(exports.InternalContext.Provider, { value: { ns: allNamespaces, config: config } }, children)));
  57. }
  58. exports.default = I18nProvider;
  59. function initialBrowserNamespaces() {
  60. var _a, _b;
  61. if (typeof window === 'undefined')
  62. return {};
  63. return ((_b = (_a = window.__NEXT_DATA__) === null || _a === void 0 ? void 0 : _a.props) === null || _b === void 0 ? void 0 : _b.__namespaces) || {};
  64. }