UNPKG

react-i18next

Version:

Internationalization for react done right. Using the i18next i18n ecosystem.

143 lines (120 loc) 3.98 kB
"use strict"; var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault"); Object.defineProperty(exports, "__esModule", { value: true }); exports.usedI18nextProvider = usedI18nextProvider; exports.getHasUsedI18nextProvider = getHasUsedI18nextProvider; exports.setDefaults = setDefaults; exports.getDefaults = getDefaults; exports.setI18n = setI18n; exports.getI18n = getI18n; exports.composeInitialProps = composeInitialProps; exports.getInitialProps = getInitialProps; exports.initReactI18next = exports.ReportNamespaces = exports.I18nContext = void 0; var _classCallCheck2 = _interopRequireDefault(require("@babel/runtime/helpers/classCallCheck")); var _createClass2 = _interopRequireDefault(require("@babel/runtime/helpers/createClass")); var _objectSpread2 = _interopRequireDefault(require("@babel/runtime/helpers/objectSpread")); var _react = _interopRequireDefault(require("react")); var defaultOptions = { bindI18n: 'languageChanged', bindI18nStore: '', transEmptyNodeValue: '', transSupportBasicHtmlNodes: true, transKeepBasicHtmlNodesFor: ['br', 'strong', 'i', 'p'], useSuspense: true }; var i18nInstance; var hasUsedI18nextProvider; var I18nContext = _react.default.createContext(); exports.I18nContext = I18nContext; function usedI18nextProvider(used) { hasUsedI18nextProvider = used; } function getHasUsedI18nextProvider() { return hasUsedI18nextProvider; } function setDefaults() { var options = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : {}; defaultOptions = (0, _objectSpread2.default)({}, defaultOptions, options); } function getDefaults() { return defaultOptions; } var ReportNamespaces = /*#__PURE__*/ function () { function ReportNamespaces() { (0, _classCallCheck2.default)(this, ReportNamespaces); this.usedNamespaces = {}; } (0, _createClass2.default)(ReportNamespaces, [{ key: "addUsedNamespaces", value: function addUsedNamespaces(namespaces) { var _this = this; namespaces.forEach(function (ns) { if (!_this.usedNamespaces[ns]) _this.usedNamespaces[ns] = true; }); } }, { key: "getUsedNamespaces", value: function getUsedNamespaces() { return Object.keys(this.usedNamespaces); } }]); return ReportNamespaces; }(); exports.ReportNamespaces = ReportNamespaces; function setI18n(instance) { i18nInstance = instance; } function getI18n() { return i18nInstance; } var initReactI18next = { type: '3rdParty', init: function init(instance) { setDefaults(instance.options.react); setI18n(instance); } }; exports.initReactI18next = initReactI18next; function composeInitialProps(ForComponent) { return function (ctx) { return new Promise(function (resolve) { var i18nInitialProps = getInitialProps(); if (ForComponent.getInitialProps) { ForComponent.getInitialProps(ctx).then(function (componentsInitialProps) { resolve((0, _objectSpread2.default)({}, componentsInitialProps, i18nInitialProps)); }); } else { resolve(i18nInitialProps); } }); }; // Avoid async for now - so we do not need to pull in regenerator // return async ctx => { // const componentsInitialProps = ForComponent.getInitialProps // ? await ForComponent.getInitialProps(ctx) // : {}; // const i18nInitialProps = getInitialProps(); // return { // ...componentsInitialProps, // ...i18nInitialProps, // }; // }; } function getInitialProps() { var i18n = getI18n(); var namespaces = i18n.reportNamespaces ? i18n.reportNamespaces.getUsedNamespaces() : []; var ret = {}; var initialI18nStore = {}; i18n.languages.forEach(function (l) { initialI18nStore[l] = {}; namespaces.forEach(function (ns) { initialI18nStore[l][ns] = i18n.getResourceBundle(l, ns) || {}; }); }); ret.initialI18nStore = initialI18nStore; ret.initialLanguage = i18n.language; return ret; }