I18nProvider.js 2.0 KB

1234567891011121314151617181920212223242526272829303132333435363738
  1. var __assign = (this && this.__assign) || function () {
  2. __assign = Object.assign || function(t) {
  3. for (var s, i = 1, n = arguments.length; i < n; i++) {
  4. s = arguments[i];
  5. for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p))
  6. t[p] = s[p];
  7. }
  8. return t;
  9. };
  10. return __assign.apply(this, arguments);
  11. };
  12. import React, { createContext, useContext } from 'react';
  13. import { useRouter } from 'next/router';
  14. import I18nContext from './_context';
  15. import transCore from './transCore';
  16. import useTranslation from './useTranslation';
  17. export var InternalContext = createContext({ ns: {}, config: {} });
  18. export default function I18nProvider(_a) {
  19. var lng = _a.lang, _b = _a.namespaces, namespaces = _b === void 0 ? {} : _b, children = _a.children, _c = _a.config, newConfig = _c === void 0 ? {} : _c;
  20. var parentLang = useTranslation().lang;
  21. var _d = useRouter() || {}, locale = _d.locale, defaultLocale = _d.defaultLocale;
  22. var internal = useContext(InternalContext);
  23. var allNamespaces = __assign(__assign(__assign({}, initialBrowserNamespaces()), internal.ns), namespaces);
  24. var lang = lng || parentLang || locale || defaultLocale || '';
  25. var config = __assign(__assign({}, internal.config), newConfig);
  26. var localesToIgnore = config.localesToIgnore || ['default'];
  27. var ignoreLang = localesToIgnore.includes(lang);
  28. var pluralRules = new Intl.PluralRules(ignoreLang ? undefined : lang);
  29. var t = transCore({ config: config, allNamespaces: allNamespaces, pluralRules: pluralRules, lang: lang });
  30. return (React.createElement(I18nContext.Provider, { value: { lang: lang, t: t } },
  31. React.createElement(InternalContext.Provider, { value: { ns: allNamespaces, config: config } }, children)));
  32. }
  33. function initialBrowserNamespaces() {
  34. var _a, _b;
  35. if (typeof window === 'undefined')
  36. return {};
  37. return ((_b = (_a = window.__NEXT_DATA__) === null || _a === void 0 ? void 0 : _a.props) === null || _b === void 0 ? void 0 : _b.__namespaces) || {};
  38. }