UNPKG

react-native-localize-ext

Version:
49 lines (48 loc) 2.2 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.I18nProvider = void 0; var tslib_1 = require("tslib"); var react_1 = tslib_1.__importStar(require("react")); var hoist_non_react_statics_1 = tslib_1.__importDefault(require("hoist-non-react-statics")); var util_1 = require("./util"); exports.I18nProvider = function () { var i18nList = []; for (var _i = 0; _i < arguments.length; _i++) { i18nList[_i] = arguments[_i]; } return function (WrappedComponent) { var I18nComponent = /** @class */ (function (_super) { tslib_1.__extends(I18nComponent, _super); function I18nComponent() { var _this = _super !== null && _super.apply(this, arguments) || this; _this.state = { sign: util_1.getSignatures(i18nList), }; _this.listeners = []; return _this; } I18nComponent.prototype.componentDidMount = function () { var _this = this; this.listeners = i18nList.map(function (i18n, index) { return i18n._.listen(function () { var data = _this.state.sign.split(util_1.SPLIT_STR); data[index] = util_1.getSignature(i18n); _this.setState({ sign: data.join(util_1.SPLIT_STR), }); }); }); }; I18nComponent.prototype.componentWillUnmount = function () { this.listeners.forEach(function (unListen) { return unListen(); }); }; I18nComponent.prototype.render = function () { var sign = this.state.sign; return react_1.default.createElement(WrappedComponent, tslib_1.__assign({}, this.props, { "$i18n$": sign })); }; I18nComponent.displayName = "I18n(" + (WrappedComponent.displayName || WrappedComponent.name) + ")"; return I18nComponent; }(react_1.PureComponent)); return hoist_non_react_statics_1.default(I18nComponent, WrappedComponent); }; };