react-multi-language
Version:
Helps you create multi-language pages
155 lines (122 loc) • 6.92 kB
JavaScript
/* In the Name of Allah */
;
Object.defineProperty(exports, '__esModule', { value: true });
function _interopDefault (ex) { return (ex && (typeof ex === 'object') && 'default' in ex) ? ex['default'] : ex; }
var React = _interopDefault(require('react'));
function _typeof(obj) {
if (typeof Symbol === "function" && typeof Symbol.iterator === "symbol") {
_typeof = function (obj) {
return typeof obj;
};
} else {
_typeof = function (obj) {
return obj && typeof Symbol === "function" && obj.constructor === Symbol && obj !== Symbol.prototype ? "symbol" : typeof obj;
};
}
return _typeof(obj);
}
function _classCallCheck(instance, Constructor) {
if (!(instance instanceof Constructor)) {
throw new TypeError("Cannot call a class as a function");
}
}
function _defineProperties(target, props) {
for (var i = 0; i < props.length; i++) {
var descriptor = props[i];
descriptor.enumerable = descriptor.enumerable || false;
descriptor.configurable = true;
if ("value" in descriptor) descriptor.writable = true;
Object.defineProperty(target, descriptor.key, descriptor);
}
}
function _createClass(Constructor, protoProps, staticProps) {
if (protoProps) _defineProperties(Constructor.prototype, protoProps);
if (staticProps) _defineProperties(Constructor, staticProps);
return Constructor;
}
function _defineProperty(obj, key, value) {
if (key in obj) {
Object.defineProperty(obj, key, {
value: value,
enumerable: true,
configurable: true,
writable: true
});
} else {
obj[key] = value;
}
return obj;
}
function _extends() {
_extends = Object.assign || function (target) {
for (var i = 1; i < arguments.length; i++) {
var source = arguments[i];
for (var key in source) {
if (Object.prototype.hasOwnProperty.call(source, key)) {
target[key] = source[key];
}
}
}
return target;
};
return _extends.apply(this, arguments);
}
function _objectSpread(target) {
for (var i = 1; i < arguments.length; i++) {
var source = arguments[i] != null ? arguments[i] : {};
var ownKeys = Object.keys(source);
if (typeof Object.getOwnPropertySymbols === 'function') {
ownKeys = ownKeys.concat(Object.getOwnPropertySymbols(source).filter(function (sym) {
return Object.getOwnPropertyDescriptor(source, sym).enumerable;
}));
}
ownKeys.forEach(function (key) {
_defineProperty(target, key, source[key]);
});
}
return target;
}
function _inherits(subClass, superClass) {
if (typeof superClass !== "function" && superClass !== null) {
throw new TypeError("Super expression must either be null or a function");
}
subClass.prototype = Object.create(superClass && superClass.prototype, {
constructor: {
value: subClass,
writable: true,
configurable: true
}
});
if (superClass) _setPrototypeOf(subClass, superClass);
}
function _getPrototypeOf(o) {
_getPrototypeOf = Object.setPrototypeOf ? Object.getPrototypeOf : function _getPrototypeOf(o) {
return o.__proto__ || Object.getPrototypeOf(o);
};
return _getPrototypeOf(o);
}
function _setPrototypeOf(o, p) {
_setPrototypeOf = Object.setPrototypeOf || function _setPrototypeOf(o, p) {
o.__proto__ = p;
return o;
};
return _setPrototypeOf(o, p);
}
function _assertThisInitialized(self) {
if (self === void 0) {
throw new ReferenceError("this hasn't been initialised - super() hasn't been called");
}
return self;
}
function _possibleConstructorReturn(self, call) {
if (call && (typeof call === "object" || typeof call === "function")) {
return call;
}
return _assertThisInitialized(self);
}
var Determinator=/*#__PURE__*/function(a){function b(a){var c;return _classCallCheck(this,b),c=_possibleConstructorReturn(this,_getPrototypeOf(b).call(this,a)),c.state={lang:document.body.lang,data:"object"===_typeof(c.props.children)?c.props.children:{}},c.handleLanguage.bind(_assertThisInitialized(_assertThisInitialized(c))),c.addData.bind(_assertThisInitialized(_assertThisInitialized(c))),c.listener=function(a){c.handleLanguage(a.detail),"function"==typeof c.props.children&&c.props.children(a.detail,c.addData(a.detail));},c}return _inherits(b,a),_createClass(b,[{key:"addData",value:function c(a){var b=this;return function(c){b.setState(function(b){return {data:_objectSpread({},b.data,_defineProperty({},a,c))}});}}},{key:"handleLanguage",value:function b(a){this.setState({lang:a});}},{key:"componentWillMount",value:function a(){document.addEventListener("language",this.listener);}},{key:"componentWillUnmount",value:function a(){document.removeEventListener("language",this.listener);}},{key:"render",value:function b(){var a=this.props.children;if("string"==typeof a)return a;if("object"!==_typeof(a)&&"function"!=typeof a)throw new TypeError("MultiLang: Expected an object or a function or a string but recieved "+_typeof(a));return this.state.data[this.state.lang]||this.props.till||null}}]),b}(React.Component);
/* In the Name of Allah */var MultiLang=function(a){return document.dispatchEvent(new CustomEvent("language",{detail:a.lang})),document.body.lang=a.lang,a.children||null};
var withLang=function(a){return function(b){return(/*#__PURE__*/function(c){function d(b){var c;return _classCallCheck(this,d),c=_possibleConstructorReturn(this,_getPrototypeOf(d).call(this,b)),c.state={lang:document.body.lang,data:"object"===_typeof(a)?a:{}},c.handleLanguage.bind(_assertThisInitialized(_assertThisInitialized(c))),c.addData.bind(_assertThisInitialized(_assertThisInitialized(c))),c.listener=function(b){c.handleLanguage(b.detail),"function"==typeof a&&a(b.detail,c.addData(b.detail));},c.determinator=function(a){var b=a.children,d=a.till;if("string"==typeof b)return b;if("object"!==_typeof(b))throw new TypeError("MultiLang: Expected an object or a string but recieved "+_typeof(b));return b[c.state.lang]||d||null},c}return _inherits(d,c),_createClass(d,[{key:"addData",value:function c(a){var b=this;return function(c){b.setState(function(b){return {data:_objectSpread({},b.data,_defineProperty({},a,c))}});}}},{key:"handleLanguage",value:function b(a){this.setState({lang:a});}},{key:"componentWillMount",value:function a(){document.addEventListener("language",this.listener);}},{key:"componentWillUnmount",value:function a(){document.removeEventListener("language",this.listener);}},{key:"render",value:function c(){if("object"!==_typeof(a)&&"function"!=typeof a)throw new TypeError("MultiLang: Expected an object or a function but recieved "+_typeof(a));return React.createElement(b,_extends({lang:this.state.lang,langProps:this.state.data[this.state.lang]||{},determinator:this.determinator},this.props))}}]),d}(React.Component))}};
exports.Determinator = Determinator;
exports.MultiLang = MultiLang;
exports.withLang = withLang;