"use strict"; var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault"); Object.defineProperty(exports, "__esModule", { value: true }); exports.PseudoLocaleWrapper = void 0; var _defineProperty2 = _interopRequireDefault(require("@babel/runtime/helpers/defineProperty")); var PropTypes = _interopRequireWildcard(require("prop-types")); var React = _interopRequireWildcard(require("react")); var _i18n = require("@kbn/i18n"); function _getRequireWildcardCache(nodeInterop) { if (typeof WeakMap !== "function") return null; var cacheBabelInterop = new WeakMap(); var cacheNodeInterop = new WeakMap(); return (_getRequireWildcardCache = function (nodeInterop) { return nodeInterop ? cacheNodeInterop : cacheBabelInterop; })(nodeInterop); } function _interopRequireWildcard(obj, nodeInterop) { if (!nodeInterop && obj && obj.__esModule) { return obj; } if (obj === null || typeof obj !== "object" && typeof obj !== "function") { return { default: obj }; } var cache = _getRequireWildcardCache(nodeInterop); if (cache && cache.has(obj)) { return cache.get(obj); } var newObj = {}; var hasPropertyDescriptor = Object.defineProperty && Object.getOwnPropertyDescriptor; for (var key in obj) { if (key !== "default" && Object.prototype.hasOwnProperty.call(obj, key)) { var desc = hasPropertyDescriptor ? Object.getOwnPropertyDescriptor(obj, key) : null; if (desc && (desc.get || desc.set)) { Object.defineProperty(newObj, key, desc); } else { newObj[key] = obj[key]; } } } newObj.default = obj; if (cache) { cache.set(obj, newObj); } return newObj; } /* * Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one * or more contributor license agreements. Licensed under the Elastic License * 2.0 and the Server Side Public License, v 1; you may not use this file except * in compliance with, at your election, the Elastic License 2.0 or the Server * Side Public License, v 1. */ /** * To translate label that includes nested `FormattedMessage` instances React Intl * replaces them with special placeholders (@__uid__@ELEMENT-uid-counter@__uid__@) * and maps them back with nested translations after `formatMessage` processes * original string, so we shouldn't modify these special placeholders with pseudo * translations otherwise React Intl won't be able to properly replace placeholders. * It's implementation detail of the React Intl, but since pseudo localization is dev * only feature we should be fine here. * @param message */ function translateFormattedMessageUsingPseudoLocale(message) { const formattedMessageDelimiter = message.match(/@__.{10}__@/); if (formattedMessageDelimiter !== null) { return message.split(formattedMessageDelimiter[0]).map(part => part.startsWith('ELEMENT-') ? part : _i18n.i18n.translateUsingPseudoLocale(part)).join(formattedMessageDelimiter[0]); } return _i18n.i18n.translateUsingPseudoLocale(message); } /** * If the locale is our pseudo locale (e.g. en-xa), we override the * intl.formatMessage function to display scrambled characters. We are * overriding the context rather than using injectI18n, because the * latter creates a new React component, which causes React diffs to * be inefficient in some cases, and can cause React hooks to lose * their state. */ class PseudoLocaleWrapper extends React.PureComponent { constructor(props, context) { super(props, context); if (_i18n.i18n.isPseudoLocale(_i18n.i18n.getLocale())) { const formatMessage = context.intl.formatMessage; context.intl.formatMessage = (...args) => translateFormattedMessageUsingPseudoLocale(formatMessage(...args)); } } render() { return this.props.children; } } exports.PseudoLocaleWrapper = PseudoLocaleWrapper; (0, _defineProperty2.default)(PseudoLocaleWrapper, "propTypes", { children: PropTypes.element.isRequired }); (0, _defineProperty2.default)(PseudoLocaleWrapper, "contextTypes", { intl: PropTypes.object.isRequired });