UNPKG

@kineticdata/react

Version:
188 lines (187 loc) 8.2 kB
"use strict"; var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault")["default"]; Object.defineProperty(exports, "__esModule", { value: true }); exports.I18nTranslate = exports.I18n = void 0; var _objectSpread2 = _interopRequireDefault(require("@babel/runtime/helpers/esm/objectSpread2")); var _classCallCheck2 = _interopRequireDefault(require("@babel/runtime/helpers/esm/classCallCheck")); var _createClass2 = _interopRequireDefault(require("@babel/runtime/helpers/esm/createClass")); var _inherits2 = _interopRequireDefault(require("@babel/runtime/helpers/esm/inherits")); var _createSuper2 = _interopRequireDefault(require("@babel/runtime/helpers/esm/createSuper")); var _react = _interopRequireDefault(require("react")); var _immutable = require("immutable"); var _isarray = _interopRequireDefault(require("isarray")); var _apis = require("../../../apis"); var _I18nContext = require("./I18nContext"); var submissionContexts = {}; var I18n = exports.I18n = /*#__PURE__*/function (_React$Component) { (0, _inherits2["default"])(I18n, _React$Component); var _super = (0, _createSuper2["default"])(I18n); function I18n(props) { var _this; (0, _classCallCheck2["default"])(this, I18n); _this = _super.call(this, props); _this.state = { loading: !!props.submissionId, context: props.context }; return _this; } (0, _createClass2["default"])(I18n, [{ key: "componentDidMount", value: function componentDidMount() { var _this2 = this; if (this.state.loading) { // If submissionId is passed in, we need to fetch the submission to build the context if (this.props.submissionId) { // If submission was already fetched, use previous result if (submissionContexts.hasOwnProperty(this.props.submissionId)) { this.setState(function (state) { return { loading: false, context: submissionContexts[_this2.props.submissionId] || _this2.state.context }; }); } else { // Otherwise fetch the submission (0, _apis.fetchSubmission)({ id: this.props.submissionId, datastore: !!this.props.datastore, include: 'form,form.kapp' }).then(function (_ref) { var submission = _ref.submission; // Build context using submission data var context = submission ? !submission.form.kapp.slug ? "forms.".concat(submission.form.slug) : "kapps.".concat(submission.form.kapp.slug, ".forms.").concat(submission.form.slug) : null; // Store the context for the submissionId submissionContexts[_this2.props.submissionId] = context; // Update loading state to false and set correct context _this2.setState(function (state) { return { loading: false, context: context || _this2.state.context }; }); }); } } else { this.setState(function (state) { return (0, _objectSpread2["default"])((0, _objectSpread2["default"])({}, state), {}, { loading: false }); }); } } } }, { key: "render", value: function render() { var _this3 = this; return !this.state.loading ? /*#__PURE__*/_react["default"].createElement(_I18nContext.I18nContext.Consumer, null, function (_ref2) { var context = _ref2.context, locale = _ref2.locale, translations = _ref2.translations, loadTranslations = _ref2.loadTranslations; // If render funtion is passed, or children is a string or array of strings, // return the translation of the children string if (typeof _this3.props.render === 'function' || typeof _this3.props.children === 'string' || (0, _isarray["default"])(_this3.props.children) && _this3.props.children.every(function (c) { return typeof c === 'string'; })) { return /*#__PURE__*/_react["default"].createElement(I18nTranslate, { context: _this3.state.context || context, locale: locale, translations: translations, loadTranslations: loadTranslations, "public": _this3.props["public"], render: _this3.props.render }, _this3.props.children); } // Otherwise wrap children in a new instance of I18nProvider with the new context else if (_this3.state.context) { return /*#__PURE__*/_react["default"].createElement(_I18nContext.I18nContext.Provider, { value: { context: _this3.state.context, locale: locale, translations: translations, loadTranslations: loadTranslations } }, /*#__PURE__*/_react["default"].createElement(I18nTranslate, { context: _this3.state.context, locale: locale, translations: translations, loadTranslations: loadTranslations, "public": _this3.props["public"] }, _this3.props.children)); } // Otherwise return children else { return _this3.props.children; } }) : null; } }]); return I18n; }(_react["default"].Component); var I18nTranslate = exports.I18nTranslate = /*#__PURE__*/function (_React$Component2) { (0, _inherits2["default"])(I18nTranslate, _React$Component2); var _super2 = (0, _createSuper2["default"])(I18nTranslate); function I18nTranslate() { (0, _classCallCheck2["default"])(this, I18nTranslate); return _super2.apply(this, arguments); } (0, _createClass2["default"])(I18nTranslate, [{ key: "componentDidMount", value: function componentDidMount() { this.props.loadTranslations(this.props.locale, this.props.context, this.props["public"]); } }, { key: "componentDidUpdate", value: function componentDidUpdate(prevProps) { if (this.props.context !== prevProps.context || this.props.locale !== prevProps.locale) { this.props.loadTranslations(this.props.locale, this.props.context, this.props["public"]); } } }, { key: "render", value: function render() { if (this.props.translations.hasIn([this.props.locale, this.props.context])) { if (typeof this.props.render === 'function') { return this.props.render(translate(this.props)); } else if (typeof this.props.children === 'string') { return translate(this.props)(this.props.children); } else if ((0, _isarray["default"])(this.props.children) && this.props.children.every(function (c) { return typeof c === 'string'; })) { return translate(this.props)(this.props.children.join('')); } else { return this.props.children; } } else { //TODO:I changed this because translations doesn't exist in the SPUI yet. return typeof this.props.children === 'string' ? this.props.children : null; } } }]); return I18nTranslate; }(_react["default"].Component); var translate = function translate(_ref3) { var context = _ref3.context, locale = _ref3.locale, translations = _ref3.translations; return function (key) { trackKeys(context, key); if (window.highlightTranslations) { // Surround translated text with asterisks to easily see what's wrapped return "*".concat(translations.getIn([locale, context, key]) || translations.getIn([locale, 'shared', key]) || key, "*"); } else { return translations.getIn([locale, context, key]) || translations.getIn([locale, 'shared', key]) || key; } }; }; var trackedKeys = (0, _immutable.Map)(); var trackKeys = function trackKeys(context, key) { trackedKeys = trackedKeys.has(key) ? trackedKeys.set(key, trackedKeys.get(key).add(context)) : trackedKeys.set(key, (0, _immutable.Set)([context])); window.bundle = window.bundle || {}; window.bundle.config = window.bundle.config || {}; window.bundle.config.translationKeys = trackedKeys.toJS(); };