UNPKG

ra-core

Version:

Core components of react-admin, a frontend Framework for building admin applications on top of REST services, using ES6, React

85 lines 3.63 kB
"use strict"; var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) { if (k2 === undefined) k2 = k; var desc = Object.getOwnPropertyDescriptor(m, k); if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) { desc = { enumerable: true, get: function() { return m[k]; } }; } Object.defineProperty(o, k2, desc); }) : (function(o, m, k, k2) { if (k2 === undefined) k2 = k; o[k2] = m[k]; })); var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) { Object.defineProperty(o, "default", { enumerable: true, value: v }); }) : function(o, v) { o["default"] = v; }); var __importStar = (this && this.__importStar) || (function () { var ownKeys = function(o) { ownKeys = Object.getOwnPropertyNames || function (o) { var ar = []; for (var k in o) if (Object.prototype.hasOwnProperty.call(o, k)) ar[ar.length] = k; return ar; }; return ownKeys(o); }; return function (mod) { if (mod && mod.__esModule) return mod; var result = {}; if (mod != null) for (var k = ownKeys(mod), i = 0; i < k.length; i++) if (k[i] !== "default") __createBinding(result, mod, k[i]); __setModuleDefault(result, mod); return result; }; })(); Object.defineProperty(exports, "__esModule", { value: true }); exports.I18nContextProvider = void 0; const React = __importStar(require("react")); const react_1 = require("react"); const I18nContext_1 = require("./I18nContext.cjs"); const useStore_1 = require("../store/useStore.cjs"); const notification_1 = require("../notification/index.cjs"); /** * Store the i18nProvider in a context, and rerender children when the locale changes */ const I18nContextProvider = ({ value = defaulti18nContext, children, }) => { const [locale] = (0, useStore_1.useStore)('locale'); const notify = (0, notification_1.useNotify)(); const [key, setKey] = (0, react_1.useState)(0); // to avoid blinking effect, delay first render if the user has a non-default locale const [isInitialized, setInitialized] = (0, react_1.useState)(locale === value.getLocale()); // watch store for locale changes (0, react_1.useEffect)(() => { if (locale && value.getLocale() !== locale) { new Promise(resolve => { // so we systematically return a Promise for the messages // i18nProvider may return a Promise for language changes, resolve(value.changeLocale(locale)); }) .then(() => { // Force full page re-render. // This is slow on locale change, but it's a good way // to avoid having one subscription to the locale // for each call to translate() setKey(key => key + 1); setInitialized(true); }) .catch(error => { setInitialized(true); notify('ra.notification.i18n_error', { type: 'error' }); console.error(error); }); } else { setInitialized(true); } }, [value, locale, notify]); return isInitialized ? (React.createElement(I18nContext_1.I18nContext.Provider, { value: value, key: key }, children)) : null; }; exports.I18nContextProvider = I18nContextProvider; const defaulti18nContext = { translate: x => x, changeLocale: () => Promise.resolve(), getLocale: () => 'en', }; //# sourceMappingURL=I18nContextProvider.js.map