UNPKG

next-i18next

Version:

The easiest way to translate your NextJs apps.

102 lines (101 loc) 4.06 kB
Object.defineProperty(exports, Symbol.toStringTag, { value: "Module" }); const require_runtime = require("./_virtual/_rolldown/runtime.cjs"); const require_utils = require("./utils.cjs"); const require_config_createConfig = require("./config/createConfig.cjs"); const require_createClient_browser = require("./createClient/browser.cjs"); let react = require("react"); react = require_runtime.__toESM(react); let hoist_non_react_statics = require("hoist-non-react-statics"); hoist_non_react_statics = require_runtime.__toESM(hoist_non_react_statics); let react_i18next = require("react-i18next"); //#region src/pagesRouter/appWithTranslation.tsx let globalI18n = null; const addResourcesToI18next = (instance, resources) => { if (resources && instance.isInitialized) { for (const locale of Object.keys(resources)) for (const ns of Object.keys(resources[locale])) if (!instance?.store?.data || !instance.store.data[locale] || !instance.store.data[locale][ns]) instance.addResourceBundle(locale, ns, resources[locale][ns], true, true); } }; const appWithTranslation = (WrappedComponent, configOverride = null) => { const AppWithTranslation = (props) => { const { _nextI18Next } = props.pageProps || {}; let locale = _nextI18Next?.initialLocale ?? props?.router?.locale; const ns = _nextI18Next?.ns; const instanceRef = (0, react.useRef)(null); /** * Memoize i18n instance and reuse it rather than creating new instance. * When the locale or resources are changed after instance was created, * we will update the instance by calling addResourceBundle method on it. */ const i18n = (0, react.useMemo)(() => { if (!_nextI18Next && !configOverride) return null; const userConfig = configOverride ?? _nextI18Next?.userConfig; if (!userConfig) throw new Error("appWithTranslation was called without a next-i18next config"); if (!userConfig?.i18n) throw new Error("appWithTranslation was called without config.i18n"); if (!userConfig?.i18n?.defaultLocale) throw new Error("config.i18n does not include a defaultLocale property"); const { initialI18nStore } = _nextI18Next || {}; const resources = configOverride?.resources ?? initialI18nStore; if (!locale) locale = userConfig.i18n.defaultLocale; let instance = instanceRef.current; if (instance) addResourcesToI18next(instance, resources); else { instance = require_createClient_browser({ ...require_config_createConfig.createConfig({ ...userConfig, lng: locale }), lng: locale, ...ns && { ns }, resources }).i18n; addResourcesToI18next(instance, resources); globalI18n = instance; instanceRef.current = instance; } return instance; }, [ _nextI18Next, locale, ns ]); /** * Since calling changeLanguage method on existing i18n instance cause state update in react, * we need to call the method in `useLayoutEffect` to prevent state update in render phase. */ require_utils.useIsomorphicLayoutEffect(() => { if (!i18n || !locale) return; i18n.changeLanguage(locale); }, [i18n, locale]); return i18n !== null ? /* @__PURE__ */ react.default.createElement(react_i18next.I18nextProvider, { i18n }, /* @__PURE__ */ react.default.createElement(WrappedComponent, props)) : /* @__PURE__ */ react.default.createElement(WrappedComponent, { key: locale, ...props }); }; return (0, hoist_non_react_statics.default)(AppWithTranslation, WrappedComponent); }; //#endregion Object.defineProperty(exports, "Trans", { enumerable: true, get: function() { return react_i18next.Trans; } }); exports.appWithTranslation = appWithTranslation; Object.defineProperty(exports, "globalI18n", { enumerable: true, get: function() { return globalI18n; } }); Object.defineProperty(exports, "useTranslation", { enumerable: true, get: function() { return react_i18next.useTranslation; } }); Object.defineProperty(exports, "withTranslation", { enumerable: true, get: function() { return react_i18next.withTranslation; } }); //# sourceMappingURL=appWithTranslation.cjs.map