@react-lang/language
Advanced tools
| /* add css module styles here (optional) */ | ||
| ._styles-module__test__3ybTi { | ||
| margin: 2em; | ||
| padding: 0.5em; | ||
| border: 2px solid #000; | ||
| font-size: 2em; | ||
| text-align: center; | ||
| } |
+386
| function _interopDefault (ex) { return (ex && (typeof ex === 'object') && 'default' in ex) ? ex['default'] : ex; } | ||
| var React = require('react'); | ||
| var React__default = _interopDefault(React); | ||
| 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 _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 _inheritsLoose(subClass, superClass) { | ||
| subClass.prototype = Object.create(superClass.prototype); | ||
| subClass.prototype.constructor = subClass; | ||
| _setPrototypeOf(subClass, superClass); | ||
| } | ||
| function _setPrototypeOf(o, p) { | ||
| _setPrototypeOf = Object.setPrototypeOf || function _setPrototypeOf(o, p) { | ||
| o.__proto__ = p; | ||
| return o; | ||
| }; | ||
| return _setPrototypeOf(o, p); | ||
| } | ||
| var ERROR_FEATURE_FORMAT = "Feature format text want's to be used but is disable. see setFeatures() documentation."; | ||
| var ERROR_SET_DEFAULT = "setDefault function value must be string"; | ||
| var ERROR_SET_TRANSLATIONS = "setTranslations object is not a instance of an object or it haves 0 keys"; | ||
| var ERROR_DEFAULT_LANG_MISSING = "default lang is empty, please use setDefault function to set language to a default value"; | ||
| var ERROR_LANG_INVALID = "The language '{lang}' is invalid. key not exists"; | ||
| var ERROR_KEY_NOT_AVAILABLE = "the key is not available on object or file"; | ||
| var ERROR_VALUE_IS_NOT_STRING = "value is not string. please provide a string or a content"; | ||
| var ERROR_GET_VALUES_INVALID = "Some values given in get(..., value0, value1) are not string."; | ||
| var ErrorMessage = /*#__PURE__*/function () { | ||
| function ErrorMessage() {} | ||
| _createClass(ErrorMessage, null, [{ | ||
| key: "ERROR_FEATURE_FORMAT", | ||
| get: function get() { | ||
| return ERROR_FEATURE_FORMAT; | ||
| } | ||
| }, { | ||
| key: "ERROR_SET_DEFAULT", | ||
| get: function get() { | ||
| return ERROR_SET_DEFAULT; | ||
| } | ||
| }, { | ||
| key: "ERROR_SET_TRANSLATIONS", | ||
| get: function get() { | ||
| return ERROR_SET_TRANSLATIONS; | ||
| } | ||
| }, { | ||
| key: "ERROR_DEFAULT_LANG_MISSING", | ||
| get: function get() { | ||
| return ERROR_DEFAULT_LANG_MISSING; | ||
| } | ||
| }, { | ||
| key: "ERROR_LANG_INVALID", | ||
| get: function get() { | ||
| return ERROR_LANG_INVALID; | ||
| } | ||
| }, { | ||
| key: "ERROR_KEY_NOT_AVAILABLE", | ||
| get: function get() { | ||
| return ERROR_KEY_NOT_AVAILABLE; | ||
| } | ||
| }, { | ||
| key: "ERROR_VALUE_IS_NOT_STRING", | ||
| get: function get() { | ||
| return ERROR_VALUE_IS_NOT_STRING; | ||
| } | ||
| }, { | ||
| key: "ERROR_GET_VALUES_INVALID", | ||
| get: function get() { | ||
| return ERROR_GET_VALUES_INVALID; | ||
| } | ||
| }]); | ||
| return ErrorMessage; | ||
| }(); | ||
| var Utils = /*#__PURE__*/function () { | ||
| function Utils() {} | ||
| Utils.mergeFeatures = function mergeFeatures(current, toMerge) { | ||
| Object.keys(toMerge).forEach(function (toMergekey) { | ||
| var valuetoMerge = toMerge[toMergekey]; | ||
| if (!(toMergekey in current)) { | ||
| throw new Error(ErrorMessage.ERROR_KEY_NOT_AVAILABLE); | ||
| } | ||
| current[toMergekey] = valuetoMerge; | ||
| }); | ||
| }; | ||
| Utils.isGlobalFile = function isGlobalFile(lang) { | ||
| for (var _len = arguments.length, values = new Array(_len > 1 ? _len - 1 : 0), _key = 1; _key < _len; _key++) { | ||
| values[_key - 1] = arguments[_key]; | ||
| } | ||
| var value = values[0]; | ||
| if (Utils.isParamObject.apply(Utils, ['0'].concat(values))) { | ||
| if (lang in value) { | ||
| return false; | ||
| } else { | ||
| throw new Error(Utils.format(ErrorMessage.ERROR_LANG_INVALID, { | ||
| lang: lang | ||
| })); | ||
| } | ||
| } else if (Utils.isNotString(value)) { | ||
| throw new Error(ErrorMessage.ERROR_VALUE_IS_NOT_STRING); | ||
| } | ||
| return true; | ||
| }; | ||
| Utils.isParamObject = function isParamObject(index) { | ||
| for (var _len2 = arguments.length, values = new Array(_len2 > 1 ? _len2 - 1 : 0), _key2 = 1; _key2 < _len2; _key2++) { | ||
| values[_key2 - 1] = arguments[_key2]; | ||
| } | ||
| for (var k in values) { | ||
| var value = values[k]; | ||
| var isIndexParam = k === index; | ||
| if (isIndexParam) { | ||
| if (Utils.isObject(value)) { | ||
| return true; | ||
| } | ||
| } | ||
| } | ||
| return false; | ||
| }; | ||
| Utils.isObject = function isObject(value) { | ||
| if (typeof value === 'object') { | ||
| return true; | ||
| } else { | ||
| return false; | ||
| } | ||
| }; | ||
| Utils.isString = function isString(value) { | ||
| if (typeof value === 'string') { | ||
| return true; | ||
| } else { | ||
| return false; | ||
| } | ||
| }; | ||
| Utils.isNotString = function isNotString(value) { | ||
| return !Utils.isString(value); | ||
| }; | ||
| Utils.getValuesFormatFeature = function getValuesFormatFeature(isGlobalFile) { | ||
| for (var _len3 = arguments.length, values = new Array(_len3 > 1 ? _len3 - 1 : 0), _key3 = 1; _key3 < _len3; _key3++) { | ||
| values[_key3 - 1] = arguments[_key3]; | ||
| } | ||
| if (!isGlobalFile) { | ||
| if (Utils.isParamObject.apply(Utils, ['2'].concat(values))) { | ||
| return values[2]; | ||
| } else { | ||
| var array = []; | ||
| for (var key in values) { | ||
| var value = values[key]; | ||
| if (key !== '0' && key !== '1' && value !== undefined) { | ||
| if (Utils.isNotString(value)) { | ||
| throw new Error(ErrorMessage.ERROR_GET_VALUES_INVALID); | ||
| } | ||
| array.push(value); | ||
| } | ||
| } | ||
| return array; | ||
| } | ||
| } else { | ||
| if (Utils.isParamObject.apply(Utils, ['1'].concat(values))) { | ||
| return values[1]; | ||
| } else { | ||
| var _array = []; | ||
| for (var _key4 in values) { | ||
| var _value = values[_key4]; | ||
| if (_key4 !== '0' && _value !== undefined) { | ||
| if (Utils.isNotString(_value)) { | ||
| throw new Error(ErrorMessage.ERROR_GET_VALUES_INVALID); | ||
| } | ||
| _array.push(_value); | ||
| } | ||
| } | ||
| return _array; | ||
| } | ||
| } | ||
| }; | ||
| Utils.format = function format(value, values) { | ||
| for (var k in values) { | ||
| value = value.replace("{" + k + "}", values[k]); | ||
| } | ||
| return value; | ||
| }; | ||
| return Utils; | ||
| }(); | ||
| var defaultContext = { | ||
| lang: '' | ||
| }; | ||
| var defaultFeatures = { | ||
| showErrorOnJsonKeyInvalid: false, | ||
| formatText: true | ||
| }; | ||
| var translations = {}; | ||
| var currentContext = Object.assign({}, defaultContext); | ||
| var currentFeatures = Object.assign({}, defaultFeatures); | ||
| var setDefault = function setDefault(value) { | ||
| if (typeof value !== 'string') { | ||
| throw new Error(ErrorMessage.ERROR_SET_DEFAULT); | ||
| } | ||
| currentContext.lang = value; | ||
| }; | ||
| var setFeatures = function setFeatures(value, checkEnviroment) { | ||
| if (typeof checkEnviroment === 'function') { | ||
| if (checkEnviroment()) { | ||
| Utils.mergeFeatures(currentFeatures, value); | ||
| } | ||
| } else { | ||
| Utils.mergeFeatures(currentFeatures, value); | ||
| } | ||
| }; | ||
| var setTranslations = function setTranslations(object) { | ||
| if (object instanceof Object && Object.keys(object).length > 0) { | ||
| translations = object; | ||
| } else { | ||
| throw new Error(ErrorMessage.ERROR_SET_TRANSLATIONS); | ||
| } | ||
| }; | ||
| var LanguageContext = React__default.createContext(); | ||
| var Language = /*#__PURE__*/function (_Component) { | ||
| _inheritsLoose(Language, _Component); | ||
| function Language(props) { | ||
| var _this; | ||
| _this = _Component.call(this, props) || this; | ||
| _this.state = {}; | ||
| _this.get = function () { | ||
| for (var _len = arguments.length, values = new Array(_len), _key = 0; _key < _len; _key++) { | ||
| values[_key] = arguments[_key]; | ||
| } | ||
| var isGlobalFile = Utils.isGlobalFile.apply(Utils, [_this.state.lang].concat(values)); | ||
| var translation = ''; | ||
| var pathKey = ''; | ||
| var translationObj = {}; | ||
| if (!isGlobalFile) { | ||
| translationObj = values[0][_this.state.lang]; | ||
| pathKey = values[1]; | ||
| } else { | ||
| translationObj = translations[_this.state.lang]; | ||
| pathKey = values[0]; | ||
| } | ||
| var translationKeys = pathKey.split('.'); | ||
| translationKeys.forEach(function (key) { | ||
| if (currentFeatures.showErrorOnJsonKeyInvalid) { | ||
| if (!(key in translationObj)) { | ||
| throw new Error(ErrorMessage.ERROR_KEY_NOT_AVAILABLE); | ||
| } | ||
| } | ||
| var temp = translationObj[key]; | ||
| if (Utils.isObject(temp)) { | ||
| translationObj = temp; | ||
| } else if (Utils.isString(temp)) { | ||
| if (currentFeatures.formatText) { | ||
| var valuesFormatFeature = Utils.getValuesFormatFeature.apply(Utils, [isGlobalFile].concat(values)); | ||
| translation = Utils.format(temp, valuesFormatFeature); | ||
| } else { | ||
| if (!isGlobalFile) { | ||
| if (values[2] !== undefined) { | ||
| throw new Error(ErrorMessage.ERROR_FEATURE_FORMAT); | ||
| } | ||
| } else { | ||
| if (values[1] !== undefined) { | ||
| throw new Error(ErrorMessage.ERROR_FEATURE_FORMAT); | ||
| } | ||
| } | ||
| translation = temp; | ||
| } | ||
| } | ||
| }); | ||
| return translation; | ||
| }; | ||
| _this.handleSetLanguage = function (value) { | ||
| var state = _extends({}, _this.state); | ||
| state.lang = value; | ||
| _this.setState(state); | ||
| }; | ||
| if (currentContext.lang === undefined || currentContext.lang === '') { | ||
| throw new Error(ErrorMessage.ERROR_DEFAULT_LANG_MISSING); | ||
| } else if (props.lang !== undefined && props.lang !== '' && props.lang !== currentContext.lang) { | ||
| _this.state.lang = props.lang; | ||
| } else { | ||
| _this.state.lang = currentContext.lang; | ||
| } | ||
| _this.state.handleSetLanguage = _this.handleSetLanguage; | ||
| _this.state.get = _this.get; | ||
| return _this; | ||
| } | ||
| var _proto = Language.prototype; | ||
| _proto.render = function render() { | ||
| return /*#__PURE__*/React__default.createElement(LanguageContext.Provider, { | ||
| value: this.state | ||
| }, this.props.children); | ||
| }; | ||
| return Language; | ||
| }(React.Component); | ||
| Language.Consumer = LanguageContext.Consumer; | ||
| exports.Language = Language; | ||
| exports.setDefault = setDefault; | ||
| exports.setFeatures = setFeatures; | ||
| exports.setTranslations = setTranslations; | ||
| //# sourceMappingURL=index.js.map |
| {"version":3,"file":"index.js","sources":["../src/utils/errors.js","../src/utils/utils.js","../src/index.js"],"sourcesContent":["const ERROR_FEATURE_FORMAT = \"Feature format text want's to be used but is disable. see setFeatures() documentation.\"\nconst ERROR_SET_DEFAULT = \"setDefault function value must be string\"\nconst ERROR_SET_TRANSLATIONS = \"setTranslations object is not a instance of an object or it haves 0 keys\"\nconst ERROR_DEFAULT_LANG_MISSING = \"default lang is empty, please use setDefault function to set language to a default value\"\nconst ERROR_LANG_INVALID = \"The language '{lang}' is invalid. key not exists\"\nconst ERROR_KEY_NOT_AVAILABLE = \"the key is not available on object or file\"\nconst ERROR_VALUE_IS_NOT_STRING = \"value is not string. please provide a string or a content\"\nconst ERROR_GET_VALUES_INVALID = \"Some values given in get(..., value0, value1) are not string.\"\n\nclass ErrorMessage {\n static get ERROR_FEATURE_FORMAT() { return ERROR_FEATURE_FORMAT }\n static get ERROR_SET_DEFAULT() { return ERROR_SET_DEFAULT }\n static get ERROR_SET_TRANSLATIONS() { return ERROR_SET_TRANSLATIONS }\n static get ERROR_DEFAULT_LANG_MISSING() { return ERROR_DEFAULT_LANG_MISSING }\n static get ERROR_LANG_INVALID() { return ERROR_LANG_INVALID }\n static get ERROR_KEY_NOT_AVAILABLE() { return ERROR_KEY_NOT_AVAILABLE }\n static get ERROR_VALUE_IS_NOT_STRING() { return ERROR_VALUE_IS_NOT_STRING }\n static get ERROR_GET_VALUES_INVALID() { return ERROR_GET_VALUES_INVALID }\n}\n\nexport { ErrorMessage }","\nimport { ErrorMessage } from './errors'\n\nclass Utils {\n\n static mergeFeatures(current, toMerge) {\n Object.keys(toMerge).forEach((toMergekey) => {\n let valuetoMerge = toMerge[toMergekey]\n if (!(toMergekey in current)) {\n throw new Error(ErrorMessage.ERROR_KEY_NOT_AVAILABLE);\n }\n current[toMergekey] = valuetoMerge\n })\n }\n\n static isGlobalFile(lang, ...values) {\n let value = values[0]\n\n if (Utils.isParamObject('0', ...values)) {\n if (lang in value) {\n return false;\n } else {\n throw new Error(Utils.format(ErrorMessage.ERROR_LANG_INVALID, { lang: lang }));\n }\n } else if (Utils.isNotString(value)) {\n throw new Error(ErrorMessage.ERROR_VALUE_IS_NOT_STRING);\n }\n\n\n return true;\n }\n\n static isParamObject(index, ...values) {\n for (let k in values) {\n let value = values[k]\n let isIndexParam = (k === index)\n if(isIndexParam) {\n if (Utils.isObject(value)) {\n return true;\n }\n }\n }\n return false;\n }\n\n static isObject(value) {\n if (typeof value === 'object') {\n return true;\n } else {\n return false;\n }\n }\n\n static isString(value) {\n if (typeof value === 'string') {\n return true;\n } else {\n return false;\n }\n }\n\n static isNotString(value) {\n return (!Utils.isString(value))\n }\n\n static getValuesFormatFeature(isGlobalFile, ...values) {\n if (!isGlobalFile) {\n if (Utils.isParamObject('2', ...values)) {\n return values[2]\n } else {\n let array = []\n\n for (let key in values) {\n let value = values[key]\n if(key !== '0' && key !== '1' && value !== undefined) {\n if(Utils.isNotString(value)) {\n throw new Error(ErrorMessage.ERROR_GET_VALUES_INVALID)\n }\n array.push(value)\n }\n }\n return array\n }\n } else {\n if (Utils.isParamObject('1', ...values)) {\n return values[1]\n } else {\n let array = []\n\n for (let key in values) {\n let value = values[key]\n if(key !== '0' && value !== undefined) {\n if(Utils.isNotString(value)) {\n throw new Error(ErrorMessage.ERROR_GET_VALUES_INVALID)\n }\n array.push(value)\n }\n }\n return array\n }\n }\n }\n\n static format(value, values) {\n for (let k in values) {\n value = value.replace(\"{\" + k + \"}\", values[k])\n }\n return value;\n }\n}\n\nexport { Utils }","import React, { Component } from \"react\";\nimport { Utils } from './utils/utils'\nimport { ErrorMessage } from './utils/errors'\n\nconst defaultContext = {\n lang: ''\n}\nconst defaultFeatures = {\n showErrorOnJsonKeyInvalid: false,\n formatText: true\n}\nlet translations = {};\n\nlet currentContext = Object.assign({}, defaultContext)\nlet currentFeatures = Object.assign({}, defaultFeatures)\n\nconst setDefault = function(value) {\n if (typeof value !== 'string') {\n throw new Error(ErrorMessage.ERROR_SET_DEFAULT)\n }\n currentContext.lang = value\n}\n\nconst setFeatures = function(value, checkEnviroment) {\n if(typeof checkEnviroment === 'function') {\n if(checkEnviroment()) {\n Utils.mergeFeatures(currentFeatures, value)\n }\n } else {\n Utils.mergeFeatures(currentFeatures, value)\n }\n}\n\nconst setTranslations = function (object) {\n if(object instanceof Object && Object.keys(object).length > 0) {\n translations = object;\n } else {\n throw new Error(ErrorMessage.ERROR_SET_TRANSLATIONS);\n }\n}\n\nconst LanguageContext = React.createContext();\n\nclass Language extends Component {\n\n state = {}\n\n constructor(props) {\n super(props);\n if(currentContext.lang === undefined || currentContext.lang === '') {\n throw new Error(ErrorMessage.ERROR_DEFAULT_LANG_MISSING)\n } else if (props.lang !== undefined && props.lang !== '' && props.lang !== currentContext.lang) {\n this.state.lang = props.lang\n } else {\n this.state.lang = currentContext.lang\n }\n this.state.handleSetLanguage = this.handleSetLanguage\n this.state.get = this.get\n }\n\n\n get = (...values) => {\n let isGlobalFile = Utils.isGlobalFile(this.state.lang, ...values)\n\n let translation = '';\n let pathKey = ''\n let translationObj = {}\n\n if (!isGlobalFile) {\n translationObj = values[0][this.state.lang];\n pathKey = values[1];\n } else {\n translationObj = translations[this.state.lang];\n pathKey = values[0];\n }\n\n let translationKeys = pathKey.split('.')\n translationKeys.forEach((key) => {\n if(currentFeatures.showErrorOnJsonKeyInvalid) {\n if (!(key in translationObj)) {\n throw new Error(ErrorMessage.ERROR_KEY_NOT_AVAILABLE);\n }\n }\n const temp = translationObj[key];\n if (Utils.isObject(temp)) {\n translationObj = temp;\n } else if (Utils.isString(temp)) {\n if (currentFeatures.formatText) {\n let valuesFormatFeature = Utils.getValuesFormatFeature(isGlobalFile, ...values)\n translation = Utils.format(temp, valuesFormatFeature);\n } else {\n if (!isGlobalFile) {\n if (values[2] !== undefined) {\n throw new Error(ErrorMessage.ERROR_FEATURE_FORMAT)\n }\n } else {\n if (values[1] !== undefined) {\n throw new Error(ErrorMessage.ERROR_FEATURE_FORMAT)\n }\n }\n translation = temp;\n }\n }\n });\n\n return translation\n }\n\n handleSetLanguage = (value) => {\n let state = {...this.state}\n state.lang = value\n this.setState(state)\n }\n\n render() {\n return(\n <LanguageContext.Provider value={this.state}>\n {this.props.children}\n </LanguageContext.Provider>\n )\n }\n}\n\nLanguage.Consumer = LanguageContext.Consumer\n\nexport { Language, setDefault, setFeatures, setTranslations }"],"names":["ERROR_FEATURE_FORMAT","ERROR_SET_DEFAULT","ERROR_SET_TRANSLATIONS","ERROR_DEFAULT_LANG_MISSING","ERROR_LANG_INVALID","ERROR_KEY_NOT_AVAILABLE","ERROR_VALUE_IS_NOT_STRING","ERROR_GET_VALUES_INVALID","ErrorMessage","Utils","mergeFeatures","current","toMerge","Object","keys","forEach","toMergekey","valuetoMerge","Error","isGlobalFile","lang","values","value","isParamObject","format","isNotString","index","k","isIndexParam","isObject","isString","getValuesFormatFeature","array","key","undefined","push","replace","defaultContext","defaultFeatures","showErrorOnJsonKeyInvalid","formatText","translations","currentContext","assign","currentFeatures","setDefault","setFeatures","checkEnviroment","setTranslations","object","length","LanguageContext","React","createContext","Language","props","state","get","translation","pathKey","translationObj","translationKeys","split","temp","valuesFormatFeature","handleSetLanguage","setState","render","children","Component","Consumer"],"mappings":";;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;AAAA,IAAMA,oBAAoB,GAAG,wFAA7B;AACA,IAAMC,iBAAiB,GAAG,0CAA1B;AACA,IAAMC,sBAAsB,GAAG,0EAA/B;AACA,IAAMC,0BAA0B,GAAG,0FAAnC;AACA,IAAMC,kBAAkB,GAAG,kDAA3B;AACA,IAAMC,uBAAuB,GAAG,4CAAhC;AACA,IAAMC,yBAAyB,GAAG,2DAAlC;AACA,IAAMC,wBAAwB,GAAG,+DAAjC;;IAEMC;;;;;SACF,eAAkC;AAAE,aAAOR,oBAAP;AAA6B;;;SACjE,eAA+B;AAAE,aAAOC,iBAAP;AAA0B;;;SAC3D,eAAoC;AAAE,aAAOC,sBAAP;AAA+B;;;SACrE,eAAwC;AAAE,aAAOC,0BAAP;AAAmC;;;SAC7E,eAAgC;AAAE,aAAOC,kBAAP;AAA2B;;;SAC7D,eAAqC;AAAE,aAAOC,uBAAP;AAAgC;;;SACvE,eAAuC;AAAE,aAAOC,yBAAP;AAAkC;;;SAC3E,eAAsC;AAAE,aAAOC,wBAAP;AAAiC;;;;;;ICdvEE;;;QAEKC,gBAAP,uBAAqBC,OAArB,EAA8BC,OAA9B,EAAuC;AACnCC,IAAAA,MAAM,CAACC,IAAP,CAAYF,OAAZ,EAAqBG,OAArB,CAA6B,UAACC,UAAD,EAAgB;AACzC,UAAIC,YAAY,GAAGL,OAAO,CAACI,UAAD,CAA1B;;AACA,UAAI,EAAEA,UAAU,IAAIL,OAAhB,CAAJ,EAA8B;AAC1B,cAAM,IAAIO,KAAJ,CAAUV,YAAY,CAACH,uBAAvB,CAAN;AACH;;AACDM,MAAAA,OAAO,CAACK,UAAD,CAAP,GAAsBC,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KAAD,EAAW;AAC3B,UAAIkC,KAAK,gBAAO,MAAKA,KAAZ,CAAT;;AACAA,MAAAA,KAAK,CAACpC,IAAN,GAAaE,KAAb;;AACA,YAAK4C,QAAL,CAAcV,KAAd;AACH,KAjEkB;;AAEf,QAAGd,cAAc,CAACtB,IAAf,KAAwBc,SAAxB,IAAqCQ,cAAc,CAACtB,IAAf,KAAwB,EAAhE,EAAoE;AAChE,YAAM,IAAIF,KAAJ,CAAUV,YAAY,CAACL,0BAAvB,CAAN;AACH,KAFD,MAEO,IAAIoD,KAAK,CAACnC,IAAN,KAAec,SAAf,IAA4BqB,KAAK,CAACnC,IAAN,KAAe,EAA3C,IAAiDmC,KAAK,CAACnC,IAAN,KAAesB,cAAc,CAACtB,IAAnF,EAAyF;AAC5F,YAAKoC,KAAL,CAAWpC,IAAX,GAAkBmC,KAAK,CAACnC,IAAxB;AACH,KAFM,MAEA;AACH,YAAKoC,KAAL,CAAWpC,IAAX,GAAkBsB,cAAc,CAACtB,IAAjC;AACH;;AACD,UAAKoC,KAAL,CAAWS,iBAAX,GAA+B,MAAKA,iBAApC;AACA,UAAKT,KAAL,CAAWC,GAAX,GAAiB,MAAKA,GAAtB;AAVe;AAWlB;;;;SAwDDU,SAAA,kBAAS;AACL,wBACIf,6BAAC,eAAD,CAAiB,QAAjB;AAA0B,MAAA,KAAK,EAAE,KAAKI;AAAtC,OACK,KAAKD,KAAL,CAAWa,QADhB,CADJ;AAKH;;;EA7EkBC;;AAgFvBf,QAAQ,CAACgB,QAAT,GAAoBnB,eAAe,CAACmB,QAApC;;;;;;;"} |
| import React, { Component } from 'react'; | ||
| 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 _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 _inheritsLoose(subClass, superClass) { | ||
| subClass.prototype = Object.create(superClass.prototype); | ||
| subClass.prototype.constructor = subClass; | ||
| _setPrototypeOf(subClass, superClass); | ||
| } | ||
| function _setPrototypeOf(o, p) { | ||
| _setPrototypeOf = Object.setPrototypeOf || function _setPrototypeOf(o, p) { | ||
| o.__proto__ = p; | ||
| return o; | ||
| }; | ||
| return _setPrototypeOf(o, p); | ||
| } | ||
| var ERROR_FEATURE_FORMAT = "Feature format text want's to be used but is disable. see setFeatures() documentation."; | ||
| var ERROR_SET_DEFAULT = "setDefault function value must be string"; | ||
| var ERROR_SET_TRANSLATIONS = "setTranslations object is not a instance of an object or it haves 0 keys"; | ||
| var ERROR_DEFAULT_LANG_MISSING = "default lang is empty, please use setDefault function to set language to a default value"; | ||
| var ERROR_LANG_INVALID = "The language '{lang}' is invalid. key not exists"; | ||
| var ERROR_KEY_NOT_AVAILABLE = "the key is not available on object or file"; | ||
| var ERROR_VALUE_IS_NOT_STRING = "value is not string. please provide a string or a content"; | ||
| var ERROR_GET_VALUES_INVALID = "Some values given in get(..., value0, value1) are not string."; | ||
| var ErrorMessage = /*#__PURE__*/function () { | ||
| function ErrorMessage() {} | ||
| _createClass(ErrorMessage, null, [{ | ||
| key: "ERROR_FEATURE_FORMAT", | ||
| get: function get() { | ||
| return ERROR_FEATURE_FORMAT; | ||
| } | ||
| }, { | ||
| key: "ERROR_SET_DEFAULT", | ||
| get: function get() { | ||
| return ERROR_SET_DEFAULT; | ||
| } | ||
| }, { | ||
| key: "ERROR_SET_TRANSLATIONS", | ||
| get: function get() { | ||
| return ERROR_SET_TRANSLATIONS; | ||
| } | ||
| }, { | ||
| key: "ERROR_DEFAULT_LANG_MISSING", | ||
| get: function get() { | ||
| return ERROR_DEFAULT_LANG_MISSING; | ||
| } | ||
| }, { | ||
| key: "ERROR_LANG_INVALID", | ||
| get: function get() { | ||
| return ERROR_LANG_INVALID; | ||
| } | ||
| }, { | ||
| key: "ERROR_KEY_NOT_AVAILABLE", | ||
| get: function get() { | ||
| return ERROR_KEY_NOT_AVAILABLE; | ||
| } | ||
| }, { | ||
| key: "ERROR_VALUE_IS_NOT_STRING", | ||
| get: function get() { | ||
| return ERROR_VALUE_IS_NOT_STRING; | ||
| } | ||
| }, { | ||
| key: "ERROR_GET_VALUES_INVALID", | ||
| get: function get() { | ||
| return ERROR_GET_VALUES_INVALID; | ||
| } | ||
| }]); | ||
| return ErrorMessage; | ||
| }(); | ||
| var Utils = /*#__PURE__*/function () { | ||
| function Utils() {} | ||
| Utils.mergeFeatures = function mergeFeatures(current, toMerge) { | ||
| Object.keys(toMerge).forEach(function (toMergekey) { | ||
| var valuetoMerge = toMerge[toMergekey]; | ||
| if (!(toMergekey in current)) { | ||
| throw new Error(ErrorMessage.ERROR_KEY_NOT_AVAILABLE); | ||
| } | ||
| current[toMergekey] = valuetoMerge; | ||
| }); | ||
| }; | ||
| Utils.isGlobalFile = function isGlobalFile(lang) { | ||
| for (var _len = arguments.length, values = new Array(_len > 1 ? _len - 1 : 0), _key = 1; _key < _len; _key++) { | ||
| values[_key - 1] = arguments[_key]; | ||
| } | ||
| var value = values[0]; | ||
| if (Utils.isParamObject.apply(Utils, ['0'].concat(values))) { | ||
| if (lang in value) { | ||
| return false; | ||
| } else { | ||
| throw new Error(Utils.format(ErrorMessage.ERROR_LANG_INVALID, { | ||
| lang: lang | ||
| })); | ||
| } | ||
| } else if (Utils.isNotString(value)) { | ||
| throw new Error(ErrorMessage.ERROR_VALUE_IS_NOT_STRING); | ||
| } | ||
| return true; | ||
| }; | ||
| Utils.isParamObject = function isParamObject(index) { | ||
| for (var _len2 = arguments.length, values = new Array(_len2 > 1 ? _len2 - 1 : 0), _key2 = 1; _key2 < _len2; _key2++) { | ||
| values[_key2 - 1] = arguments[_key2]; | ||
| } | ||
| for (var k in values) { | ||
| var value = values[k]; | ||
| var isIndexParam = k === index; | ||
| if (isIndexParam) { | ||
| if (Utils.isObject(value)) { | ||
| return true; | ||
| } | ||
| } | ||
| } | ||
| return false; | ||
| }; | ||
| Utils.isObject = function isObject(value) { | ||
| if (typeof value === 'object') { | ||
| return true; | ||
| } else { | ||
| return false; | ||
| } | ||
| }; | ||
| Utils.isString = function isString(value) { | ||
| if (typeof value === 'string') { | ||
| return true; | ||
| } else { | ||
| return false; | ||
| } | ||
| }; | ||
| Utils.isNotString = function isNotString(value) { | ||
| return !Utils.isString(value); | ||
| }; | ||
| Utils.getValuesFormatFeature = function getValuesFormatFeature(isGlobalFile) { | ||
| for (var _len3 = arguments.length, values = new Array(_len3 > 1 ? _len3 - 1 : 0), _key3 = 1; _key3 < _len3; _key3++) { | ||
| values[_key3 - 1] = arguments[_key3]; | ||
| } | ||
| if (!isGlobalFile) { | ||
| if (Utils.isParamObject.apply(Utils, ['2'].concat(values))) { | ||
| return values[2]; | ||
| } else { | ||
| var array = []; | ||
| for (var key in values) { | ||
| var value = values[key]; | ||
| if (key !== '0' && key !== '1' && value !== undefined) { | ||
| if (Utils.isNotString(value)) { | ||
| throw new Error(ErrorMessage.ERROR_GET_VALUES_INVALID); | ||
| } | ||
| array.push(value); | ||
| } | ||
| } | ||
| return array; | ||
| } | ||
| } else { | ||
| if (Utils.isParamObject.apply(Utils, ['1'].concat(values))) { | ||
| return values[1]; | ||
| } else { | ||
| var _array = []; | ||
| for (var _key4 in values) { | ||
| var _value = values[_key4]; | ||
| if (_key4 !== '0' && _value !== undefined) { | ||
| if (Utils.isNotString(_value)) { | ||
| throw new Error(ErrorMessage.ERROR_GET_VALUES_INVALID); | ||
| } | ||
| _array.push(_value); | ||
| } | ||
| } | ||
| return _array; | ||
| } | ||
| } | ||
| }; | ||
| Utils.format = function format(value, values) { | ||
| for (var k in values) { | ||
| value = value.replace("{" + k + "}", values[k]); | ||
| } | ||
| return value; | ||
| }; | ||
| return Utils; | ||
| }(); | ||
| var defaultContext = { | ||
| lang: '' | ||
| }; | ||
| var defaultFeatures = { | ||
| showErrorOnJsonKeyInvalid: false, | ||
| formatText: true | ||
| }; | ||
| var translations = {}; | ||
| var currentContext = Object.assign({}, defaultContext); | ||
| var currentFeatures = Object.assign({}, defaultFeatures); | ||
| var setDefault = function setDefault(value) { | ||
| if (typeof value !== 'string') { | ||
| throw new Error(ErrorMessage.ERROR_SET_DEFAULT); | ||
| } | ||
| currentContext.lang = value; | ||
| }; | ||
| var setFeatures = function setFeatures(value, checkEnviroment) { | ||
| if (typeof checkEnviroment === 'function') { | ||
| if (checkEnviroment()) { | ||
| Utils.mergeFeatures(currentFeatures, value); | ||
| } | ||
| } else { | ||
| Utils.mergeFeatures(currentFeatures, value); | ||
| } | ||
| }; | ||
| var setTranslations = function setTranslations(object) { | ||
| if (object instanceof Object && Object.keys(object).length > 0) { | ||
| translations = object; | ||
| } else { | ||
| throw new Error(ErrorMessage.ERROR_SET_TRANSLATIONS); | ||
| } | ||
| }; | ||
| var LanguageContext = React.createContext(); | ||
| var Language = /*#__PURE__*/function (_Component) { | ||
| _inheritsLoose(Language, _Component); | ||
| function Language(props) { | ||
| var _this; | ||
| _this = _Component.call(this, props) || this; | ||
| _this.state = {}; | ||
| _this.get = function () { | ||
| for (var _len = arguments.length, values = new Array(_len), _key = 0; _key < _len; _key++) { | ||
| values[_key] = arguments[_key]; | ||
| } | ||
| var isGlobalFile = Utils.isGlobalFile.apply(Utils, [_this.state.lang].concat(values)); | ||
| var translation = ''; | ||
| var pathKey = ''; | ||
| var translationObj = {}; | ||
| if (!isGlobalFile) { | ||
| translationObj = values[0][_this.state.lang]; | ||
| pathKey = values[1]; | ||
| } else { | ||
| translationObj = translations[_this.state.lang]; | ||
| pathKey = values[0]; | ||
| } | ||
| var translationKeys = pathKey.split('.'); | ||
| translationKeys.forEach(function (key) { | ||
| if (currentFeatures.showErrorOnJsonKeyInvalid) { | ||
| if (!(key in translationObj)) { | ||
| throw new Error(ErrorMessage.ERROR_KEY_NOT_AVAILABLE); | ||
| } | ||
| } | ||
| var temp = translationObj[key]; | ||
| if (Utils.isObject(temp)) { | ||
| translationObj = temp; | ||
| } else if (Utils.isString(temp)) { | ||
| if (currentFeatures.formatText) { | ||
| var valuesFormatFeature = Utils.getValuesFormatFeature.apply(Utils, [isGlobalFile].concat(values)); | ||
| translation = Utils.format(temp, valuesFormatFeature); | ||
| } else { | ||
| if (!isGlobalFile) { | ||
| if (values[2] !== undefined) { | ||
| throw new Error(ErrorMessage.ERROR_FEATURE_FORMAT); | ||
| } | ||
| } else { | ||
| if (values[1] !== undefined) { | ||
| throw new Error(ErrorMessage.ERROR_FEATURE_FORMAT); | ||
| } | ||
| } | ||
| translation = temp; | ||
| } | ||
| } | ||
| }); | ||
| return translation; | ||
| }; | ||
| _this.handleSetLanguage = function (value) { | ||
| var state = _extends({}, _this.state); | ||
| state.lang = value; | ||
| _this.setState(state); | ||
| }; | ||
| if (currentContext.lang === undefined || currentContext.lang === '') { | ||
| throw new Error(ErrorMessage.ERROR_DEFAULT_LANG_MISSING); | ||
| } else if (props.lang !== undefined && props.lang !== '' && props.lang !== currentContext.lang) { | ||
| _this.state.lang = props.lang; | ||
| } else { | ||
| _this.state.lang = currentContext.lang; | ||
| } | ||
| _this.state.handleSetLanguage = _this.handleSetLanguage; | ||
| _this.state.get = _this.get; | ||
| return _this; | ||
| } | ||
| var _proto = Language.prototype; | ||
| _proto.render = function render() { | ||
| return /*#__PURE__*/React.createElement(LanguageContext.Provider, { | ||
| value: this.state | ||
| }, this.props.children); | ||
| }; | ||
| return Language; | ||
| }(Component); | ||
| Language.Consumer = LanguageContext.Consumer; | ||
| export { Language, setDefault, setFeatures, setTranslations }; | ||
| //# sourceMappingURL=index.modern.js.map |
| {"version":3,"file":"index.modern.js","sources":["../src/utils/errors.js","../src/utils/utils.js","../src/index.js"],"sourcesContent":["const ERROR_FEATURE_FORMAT = \"Feature format text want's to be used but is disable. see setFeatures() documentation.\"\nconst ERROR_SET_DEFAULT = \"setDefault function value must be string\"\nconst ERROR_SET_TRANSLATIONS = \"setTranslations object is not a instance of an object or it haves 0 keys\"\nconst ERROR_DEFAULT_LANG_MISSING = \"default lang is empty, please use setDefault function to set language to a default value\"\nconst ERROR_LANG_INVALID = \"The language '{lang}' is invalid. key not exists\"\nconst ERROR_KEY_NOT_AVAILABLE = \"the key is not available on object or file\"\nconst ERROR_VALUE_IS_NOT_STRING = \"value is not string. please provide a string or a content\"\nconst ERROR_GET_VALUES_INVALID = \"Some values given in get(..., value0, value1) are not string.\"\n\nclass ErrorMessage {\n static get ERROR_FEATURE_FORMAT() { return ERROR_FEATURE_FORMAT }\n static get ERROR_SET_DEFAULT() { return ERROR_SET_DEFAULT }\n static get ERROR_SET_TRANSLATIONS() { return ERROR_SET_TRANSLATIONS }\n static get ERROR_DEFAULT_LANG_MISSING() { return ERROR_DEFAULT_LANG_MISSING }\n static get ERROR_LANG_INVALID() { return ERROR_LANG_INVALID }\n static get ERROR_KEY_NOT_AVAILABLE() { return ERROR_KEY_NOT_AVAILABLE }\n static get ERROR_VALUE_IS_NOT_STRING() { return ERROR_VALUE_IS_NOT_STRING }\n static get ERROR_GET_VALUES_INVALID() { return ERROR_GET_VALUES_INVALID }\n}\n\nexport { ErrorMessage }","\nimport { ErrorMessage } from './errors'\n\nclass Utils {\n\n static mergeFeatures(current, toMerge) {\n Object.keys(toMerge).forEach((toMergekey) => {\n let valuetoMerge = toMerge[toMergekey]\n if (!(toMergekey in current)) {\n throw new Error(ErrorMessage.ERROR_KEY_NOT_AVAILABLE);\n }\n current[toMergekey] = valuetoMerge\n })\n }\n\n static isGlobalFile(lang, ...values) {\n let value = values[0]\n\n if (Utils.isParamObject('0', ...values)) {\n if (lang in value) {\n return false;\n } else {\n throw new Error(Utils.format(ErrorMessage.ERROR_LANG_INVALID, { lang: lang }));\n }\n } else if (Utils.isNotString(value)) {\n throw new Error(ErrorMessage.ERROR_VALUE_IS_NOT_STRING);\n }\n\n\n return true;\n }\n\n static isParamObject(index, ...values) {\n for (let k in values) {\n let value = values[k]\n let isIndexParam = (k === index)\n if(isIndexParam) {\n if (Utils.isObject(value)) {\n return true;\n }\n }\n }\n return false;\n }\n\n static isObject(value) {\n if (typeof value === 'object') {\n return true;\n } else {\n return false;\n }\n }\n\n static isString(value) {\n if (typeof value === 'string') {\n return true;\n } else {\n return false;\n }\n }\n\n static isNotString(value) {\n return (!Utils.isString(value))\n }\n\n static getValuesFormatFeature(isGlobalFile, ...values) {\n if (!isGlobalFile) {\n if (Utils.isParamObject('2', ...values)) {\n return values[2]\n } else {\n let array = []\n\n for (let key in values) {\n let value = values[key]\n if(key !== '0' && key !== '1' && value !== undefined) {\n if(Utils.isNotString(value)) {\n throw new Error(ErrorMessage.ERROR_GET_VALUES_INVALID)\n }\n array.push(value)\n }\n }\n return array\n }\n } else {\n if (Utils.isParamObject('1', ...values)) {\n return values[1]\n } else {\n let array = []\n\n for (let key in values) {\n let value = values[key]\n if(key !== '0' && value !== undefined) {\n if(Utils.isNotString(value)) {\n throw new Error(ErrorMessage.ERROR_GET_VALUES_INVALID)\n }\n array.push(value)\n }\n }\n return array\n }\n }\n }\n\n static format(value, values) {\n for (let k in values) {\n value = value.replace(\"{\" + k + \"}\", values[k])\n }\n return value;\n }\n}\n\nexport { Utils }","import React, { Component } from \"react\";\nimport { Utils } from './utils/utils'\nimport { ErrorMessage } from './utils/errors'\n\nconst defaultContext = {\n lang: ''\n}\nconst defaultFeatures = {\n showErrorOnJsonKeyInvalid: false,\n formatText: true\n}\nlet translations = {};\n\nlet currentContext = Object.assign({}, defaultContext)\nlet currentFeatures = Object.assign({}, defaultFeatures)\n\nconst setDefault = function(value) {\n if (typeof value !== 'string') {\n throw new Error(ErrorMessage.ERROR_SET_DEFAULT)\n }\n currentContext.lang = value\n}\n\nconst setFeatures = function(value, checkEnviroment) {\n if(typeof checkEnviroment === 'function') {\n if(checkEnviroment()) {\n Utils.mergeFeatures(currentFeatures, value)\n }\n } else {\n Utils.mergeFeatures(currentFeatures, value)\n }\n}\n\nconst setTranslations = function (object) {\n if(object instanceof Object && Object.keys(object).length > 0) {\n translations = object;\n } else {\n throw new Error(ErrorMessage.ERROR_SET_TRANSLATIONS);\n }\n}\n\nconst LanguageContext = React.createContext();\n\nclass Language extends Component {\n\n state = {}\n\n constructor(props) {\n super(props);\n if(currentContext.lang === undefined || currentContext.lang === '') {\n throw new Error(ErrorMessage.ERROR_DEFAULT_LANG_MISSING)\n } else if (props.lang !== undefined && props.lang !== '' && props.lang !== currentContext.lang) {\n this.state.lang = props.lang\n } else {\n this.state.lang = currentContext.lang\n }\n this.state.handleSetLanguage = this.handleSetLanguage\n this.state.get = this.get\n }\n\n\n get = (...values) => {\n let isGlobalFile = Utils.isGlobalFile(this.state.lang, ...values)\n\n let translation = '';\n let pathKey = ''\n let translationObj = {}\n\n if (!isGlobalFile) {\n translationObj = values[0][this.state.lang];\n pathKey = values[1];\n } else {\n translationObj = translations[this.state.lang];\n pathKey = values[0];\n }\n\n let translationKeys = pathKey.split('.')\n translationKeys.forEach((key) => {\n if(currentFeatures.showErrorOnJsonKeyInvalid) {\n if (!(key in translationObj)) {\n throw new Error(ErrorMessage.ERROR_KEY_NOT_AVAILABLE);\n }\n }\n const temp = translationObj[key];\n if (Utils.isObject(temp)) {\n translationObj = temp;\n } else if (Utils.isString(temp)) {\n if (currentFeatures.formatText) {\n let valuesFormatFeature = Utils.getValuesFormatFeature(isGlobalFile, ...values)\n translation = Utils.format(temp, valuesFormatFeature);\n } else {\n if (!isGlobalFile) {\n if (values[2] !== undefined) {\n throw new Error(ErrorMessage.ERROR_FEATURE_FORMAT)\n }\n } else {\n if (values[1] !== undefined) {\n throw new Error(ErrorMessage.ERROR_FEATURE_FORMAT)\n }\n }\n translation = temp;\n }\n }\n });\n\n return translation\n }\n\n handleSetLanguage = (value) => {\n let state = {...this.state}\n state.lang = value\n this.setState(state)\n }\n\n render() {\n return(\n <LanguageContext.Provider value={this.state}>\n {this.props.children}\n </LanguageContext.Provider>\n )\n }\n}\n\nLanguage.Consumer = LanguageContext.Consumer\n\nexport { Language, setDefault, setFeatures, setTranslations }"],"names":["ERROR_FEATURE_FORMAT","ERROR_SET_DEFAULT","ERROR_SET_TRANSLATIONS","ERROR_DEFAULT_LANG_MISSING","ERROR_LANG_INVALID","ERROR_KEY_NOT_AVAILABLE","ERROR_VALUE_IS_NOT_STRING","ERROR_GET_VALUES_INVALID","ErrorMessage","Utils","mergeFeatures","current","toMerge","Object","keys","forEach","toMergekey","valuetoMerge","Error","isGlobalFile","lang","values","value","isParamObject","format","isNotString","index","k","isIndexParam","isObject","isString","getValuesFormatFeature","array","key","undefined","push","replace","defaultContext","defaultFeatures","showErrorOnJsonKeyInvalid","formatText","translations","currentContext","assign","currentFeatures","setDefault","setFeatures","checkEnviroment","setTranslations","object","length","LanguageContext","React","createContext","Language","props","state","get","translation","pathKey","translationObj","translationKeys","split","temp","valuesFormatFeature","handleSetLanguage","setState","render","children","Component","Consumer"],"mappings":";;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;AAAA,IAAMA,oBAAoB,GAAG,wFAA7B;AACA,IAAMC,iBAAiB,GAAG,0CAA1B;AACA,IAAMC,sBAAsB,GAAG,0EAA/B;AACA,IAAMC,0BAA0B,GAAG,0FAAnC;AACA,IAAMC,kBAAkB,GAAG,kDAA3B;AACA,IAAMC,uBAAuB,GAAG,4CAAhC;AACA,IAAMC,yBAAyB,GAAG,2DAAlC;AACA,IAAMC,wBAAwB,GAAG,+DAAjC;;IAEMC;;;;;SACF,eAAkC;AAAE,aAAOR,oBAAP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AxB;AACH,KAFM,MAEA;AACH,YAAKoC,KAAL,CAAWpC,IAAX,GAAkBsB,cAAc,CAACtB,IAAjC;AACH;;AACD,UAAKoC,KAAL,CAAWS,iBAAX,GAA+B,MAAKA,iBAApC;AACA,UAAKT,KAAL,CAAWC,GAAX,GAAiB,MAAKA,GAAtB;AAVe;AAWlB;;;;SAwDDU,SAAA,kBAAS;AACL,wBACI,oBAAC,eAAD,CAAiB,QAAjB;AAA0B,MAAA,KAAK,EAAE,KAAKX;AAAtC,OACK,KAAKD,KAAL,CAAWa,QADhB,CADJ;AAKH;;;EA7EkBC;;AAgFvBf,QAAQ,CAACgB,QAAT,GAAoBnB,eAAe,CAACmB,QAApC;;;;"} |
+45
-7
| { | ||
| "name": "@react-lang/language", | ||
| "version": "1.0.2", | ||
| "version": "1.0.3", | ||
| "description": "This is a React library for changing the language within your application.", | ||
| "main": "index.js", | ||
| "scripts": { | ||
| "test": "echo \"Error: no test specified\" && exit 1" | ||
| }, | ||
| "author": "Martu Amengual <martuu.amengual@gmail.com>", | ||
| "license": "MIT", | ||
| "repository": { | ||
@@ -18,5 +16,44 @@ "type": "git", | ||
| "homepage": "https://github.com/martuuamengual/-react-lang-language#readme", | ||
| "main": "dist/index.js", | ||
| "module": "dist/index.modern.js", | ||
| "source": "src/index.js", | ||
| "engines": { | ||
| "node": ">=10" | ||
| }, | ||
| "scripts": { | ||
| "build": "microbundle-crl --no-compress --format modern,cjs", | ||
| "start": "microbundle-crl watch --no-compress --format modern,cjs", | ||
| "prepare": "run-s build", | ||
| "test": "run-s test:unit test:lint test:build", | ||
| "test:build": "run-s build", | ||
| "test:lint": "eslint .", | ||
| "test:unit": "cross-env CI=1 react-scripts test --env=jsdom", | ||
| "test:watch": "react-scripts test --env=jsdom", | ||
| "predeploy": "cd example && npm install && npm run build", | ||
| "deploy": "gh-pages -d example/build" | ||
| }, | ||
| "peerDependencies": { | ||
| "react": "^17.0.1" | ||
| }, | ||
| "devDependencies": { | ||
| "babel-eslint": "^10.0.3", | ||
| "cross-env": "^7.0.2", | ||
| "eslint": "^6.8.0", | ||
| "eslint-config-prettier": "^6.7.0", | ||
| "eslint-config-standard": "^14.1.0", | ||
| "eslint-config-standard-react": "^9.2.0", | ||
| "eslint-plugin-import": "^2.18.2", | ||
| "eslint-plugin-node": "^11.0.0", | ||
| "eslint-plugin-prettier": "^3.1.1", | ||
| "eslint-plugin-promise": "^4.2.1", | ||
| "eslint-plugin-react": "^7.17.0", | ||
| "eslint-plugin-standard": "^4.0.1", | ||
| "gh-pages": "^2.2.0", | ||
| "microbundle-crl": "^0.13.10", | ||
| "npm-run-all": "^4.1.5", | ||
| "prettier": "^2.0.4", | ||
| "react": "^16.13.1", | ||
| "react-dom": "^16.13.1", | ||
| "react-scripts": "^3.4.1" | ||
| }, | ||
| "keywords": [ | ||
@@ -36,4 +73,5 @@ "react", | ||
| ], | ||
| "author": "Martu Amengual <martuu.amengual@gmail.com>", | ||
| "license": "MIT" | ||
| "files": [ | ||
| "dist" | ||
| ] | ||
| } |
+54
-54
@@ -44,11 +44,11 @@ # @react-lang/language | ||
| ```jsx | ||
| import React from "react"; | ||
| import ReactDOM from "react-dom"; | ||
| import React from 'react' | ||
| import ReactDOM from 'react-dom' | ||
| import { Language, setDefault, setTranslations } from "@react-lang/language"; | ||
| import en from "./en.json"; | ||
| import en from "./es.json"; | ||
| import { Language, setDefault, setTranslations } from '@react-lang/language' | ||
| import en from './en.json' | ||
| import en from './es.json' | ||
| setDefault("en"); | ||
| setTranslations({ en, es }); | ||
| setDefault('en') | ||
| setTranslations({ en, es }) | ||
@@ -61,4 +61,4 @@ ReactDOM.render( | ||
| </React.StrictMode>, | ||
| document.getElementById("root") | ||
| ); | ||
| document.getElementById('root') | ||
| ) | ||
| ``` | ||
@@ -69,3 +69,3 @@ | ||
| ```jsx | ||
| import { Language } from "@react-lang/language"; | ||
| import { Language } from '@react-lang/language' | ||
@@ -75,5 +75,5 @@ function App() { | ||
| <Language.Consumer> | ||
| {({ get, handleSetLanguage, lang }) => <p>{get("message")}</p>} | ||
| {({ get, handleSetLanguage, lang }) => <p>{get('message')}</p>} | ||
| </Language.Consumer> | ||
| ); | ||
| ) | ||
| } | ||
@@ -111,13 +111,13 @@ ``` | ||
| ```jsx | ||
| import { Language } from "@react-lang/language"; | ||
| import { Language } from '@react-lang/language' | ||
| function ShowMessage() { | ||
| let name = "Jhon"; | ||
| let surname = "Felix"; | ||
| let name = 'Jhon' | ||
| let surname = 'Felix' | ||
| return ( | ||
| <Language.Consumer> | ||
| {({ get }) => <p>{get("message", { name: name, surname: surname })}</p>} | ||
| {({ get }) => <p>{get('message', { name: name, surname: surname })}</p>} | ||
| </Language.Consumer> | ||
| ); | ||
| ) | ||
| } | ||
@@ -129,13 +129,13 @@ ``` | ||
| ```jsx | ||
| import { Language } from "@react-lang/language"; | ||
| import { Language } from '@react-lang/language' | ||
| function ShowMessage() { | ||
| let name = "Jhon"; | ||
| let surname = "Felix"; | ||
| let name = 'Jhon' | ||
| let surname = 'Felix' | ||
| return ( | ||
| <Language.Consumer> | ||
| {({ get }) => <p>{get("message", name, surname)}</p>} | ||
| {({ get }) => <p>{get('message', name, surname)}</p>} | ||
| </Language.Consumer> | ||
| ); | ||
| ) | ||
| } | ||
@@ -152,12 +152,12 @@ ``` | ||
| export default class UserInfo extends Component { | ||
| state = {}; | ||
| state = {} | ||
| content = { | ||
| en: { | ||
| message: "Hello world how are you?", | ||
| message: 'Hello world how are you?' | ||
| }, | ||
| es: { | ||
| message: "Hola mundo como estas?", | ||
| }, | ||
| }; | ||
| message: 'Hola mundo como estas?' | ||
| } | ||
| } | ||
@@ -167,5 +167,5 @@ render() { | ||
| <Language.Consumer> | ||
| {({ get }) => <p>{get(this.content, "message")}</p>} | ||
| {({ get }) => <p>{get(this.content, 'message')}</p>} | ||
| </Language.Consumer> | ||
| ); | ||
| ) | ||
| } | ||
@@ -186,3 +186,3 @@ } | ||
| ```jsx | ||
| import { Language } from "@react-lang/language"; | ||
| import { Language } from '@react-lang/language' | ||
@@ -195,4 +195,4 @@ ReactDOM.render( | ||
| </React.StrictMode>, | ||
| document.getElementById("root") | ||
| ); | ||
| document.getElementById('root') | ||
| ) | ||
| ``` | ||
@@ -212,3 +212,3 @@ | ||
| ```jsx | ||
| import { Language } from "@react-lang/language"; | ||
| import { Language } from '@react-lang/language' | ||
@@ -218,5 +218,5 @@ function SomeComponent() { | ||
| <Language.Consumer> | ||
| {({ get }) => <p>{get("someKey")}</p>} | ||
| {({ get }) => <p>{get('someKey')}</p>} | ||
| </Language.Consumer> | ||
| ); | ||
| ) | ||
| } | ||
@@ -232,6 +232,6 @@ ``` | ||
| ```jsx | ||
| import { setTranslations } from "@react-lang/language"; | ||
| import en from "./en.json"; | ||
| import { setTranslations } from '@react-lang/language' | ||
| import en from './en.json' | ||
| setTranslations({ en }); | ||
| setTranslations({ en }) | ||
| ``` | ||
@@ -246,13 +246,13 @@ | ||
| ```jsx | ||
| import { setFeatures } from "@react-lang/language"; | ||
| import { setFeatures } from '@react-lang/language' | ||
| setFeatures({ formatText: false }); | ||
| setFeatures({ formatText: false }) | ||
| setFeatures({ showErrorOnJsonKeyInvalid: true }, () => { | ||
| if (env === "dev") { | ||
| return true; | ||
| if (env === 'dev') { | ||
| return true | ||
| } else { | ||
| return false; | ||
| return false | ||
| } | ||
| }); | ||
| }) | ||
| ``` | ||
@@ -263,5 +263,5 @@ | ||
| ```jsx | ||
| import { setFeatures } from "@react-lang/language"; | ||
| import { setFeatures } from '@react-lang/language' | ||
| setFeatures({ showErrorOnJsonKeyInvalid: true }); | ||
| setFeatures({ showErrorOnJsonKeyInvalid: true }) | ||
| ``` | ||
@@ -296,3 +296,3 @@ | ||
| <p> | ||
| {get("user.name")} {get("user.surname")} | ||
| {get('user.name')} {get('user.surname')} | ||
| </p> | ||
@@ -307,3 +307,3 @@ )} | ||
| <Language.Consumer> | ||
| {({ get }) => <p>{get(this.content, "someKey")}</p>} | ||
| {({ get }) => <p>{get(this.content, 'someKey')}</p>} | ||
| </Language.Consumer> | ||
@@ -316,3 +316,3 @@ ``` | ||
| <Language.Consumer> | ||
| {({ get }) => <p>{get(this.content, "someKey", { key: value })}</p>} | ||
| {({ get }) => <p>{get(this.content, 'someKey', { key: value })}</p>} | ||
| </Language.Consumer> | ||
@@ -325,3 +325,3 @@ ``` | ||
| <Language.Consumer> | ||
| {({ get }) => <p>{get("someKey", { key: value })}</p>} | ||
| {({ get }) => <p>{get('someKey', { key: value })}</p>} | ||
| </Language.Consumer> | ||
@@ -334,3 +334,3 @@ ``` | ||
| <Language.Consumer> | ||
| {({ get }) => <p>{get("someKey", value0, value1)}</p>} | ||
| {({ get }) => <p>{get('someKey', value0, value1)}</p>} | ||
| </Language.Consumer> | ||
@@ -343,3 +343,3 @@ ``` | ||
| <Language.Consumer> | ||
| {({ get }) => <p>{get("someKey", ...values)}</p>} | ||
| {({ get }) => <p>{get('someKey', ...values)}</p>} | ||
| </Language.Consumer> | ||
@@ -358,3 +358,3 @@ ``` | ||
| <p>{get('someKey')}</p> | ||
| <button onClick={handleSetLanguage('fr')}>Change language to FR</button> | ||
| <button onClick={() => handleSetLanguage('fr')}>Change language to FR</button> | ||
| )} | ||
@@ -374,3 +374,3 @@ </Language.Consumer> | ||
| <p>Current language is: {lang}</p> | ||
| <button onClick={handleSetLanguage('fr')}>Change language to FR</button> | ||
| <button onClick={() => handleSetLanguage('fr')}>Change language to FR</button> | ||
| )} | ||
@@ -377,0 +377,0 @@ </Language.Consumer> |
-126
| import React, { Component } from "react"; | ||
| import { Utils } from './src/utils' | ||
| import { ErrorMessage } from './src/errors' | ||
| const defaultContext = { | ||
| lang: '' | ||
| } | ||
| const defaultFeatures = { | ||
| showErrorOnJsonKeyInvalid: false, | ||
| formatText: true | ||
| } | ||
| let translations = {}; | ||
| let currentContext = Object.assign({}, defaultContext) | ||
| let currentFeatures = Object.assign({}, defaultFeatures) | ||
| const setDefault = function(value) { | ||
| if (typeof value !== 'string') { | ||
| throw new Error(ErrorMessage.ERROR_SET_DEFAULT) | ||
| } | ||
| currentContext.lang = value | ||
| } | ||
| const setFeatures = function(value, checkEnviroment) { | ||
| if(typeof checkEnviroment === 'function') { | ||
| if(checkEnviroment()) { | ||
| Utils.mergeFeatures(currentFeatures, value) | ||
| } | ||
| } else { | ||
| Utils.mergeFeatures(currentFeatures, value) | ||
| } | ||
| } | ||
| const setTranslations = function (object) { | ||
| if(object instanceof Object && Object.keys(object).length > 0) { | ||
| translations = object; | ||
| } else { | ||
| throw new Error(ErrorMessage.ERROR_SET_TRANSLATIONS); | ||
| } | ||
| } | ||
| const LanguageContext = React.createContext(currentContext); | ||
| class Language extends Component { | ||
| state = {} | ||
| constructor(props) { | ||
| super(props); | ||
| if(currentContext.lang === undefined || currentContext.lang === '') { | ||
| throw new Error(ErrorMessage.ERROR_DEFAULT_LANG_MISSING) | ||
| } else if (props.lang !== undefined && props.lang !== '' && props.lang !== currentContext.lang) { | ||
| this.state.lang = props.lang | ||
| } else { | ||
| this.state.lang = currentContext.lang | ||
| } | ||
| this.state.handleSetLanguage = this.handleSetLanguage | ||
| this.state.get = this.get | ||
| } | ||
| get = (...values) => { | ||
| let isGlobalFile = Utils.isGlobalFile(this.state.lang, ...values) | ||
| let translation = ''; | ||
| let pathKey = '' | ||
| let translationObj = {} | ||
| if (!isGlobalFile) { | ||
| translationObj = values[0][this.state.lang]; | ||
| pathKey = values[1]; | ||
| } else { | ||
| translationObj = translations[this.state.lang]; | ||
| pathKey = values[0]; | ||
| } | ||
| let translationKeys = pathKey.split('.') | ||
| translationKeys.forEach((key) => { | ||
| if(currentFeatures.showErrorOnJsonKeyInvalid) { | ||
| if (!(key in translationObj)) { | ||
| throw new Error(ErrorMessage.ERROR_KEY_NOT_AVAILABLE); | ||
| } | ||
| } | ||
| const temp = translationObj[key]; | ||
| if (Utils.isObject(temp)) { | ||
| translationObj = temp; | ||
| } else if (Utils.isString(temp)) { | ||
| if (currentFeatures.formatText) { | ||
| let valuesFormatFeature = Utils.getValuesFormatFeature(isGlobalFile, ...values) | ||
| translation = Utils.format(temp, valuesFormatFeature); | ||
| } else { | ||
| if (!isGlobalFile) { | ||
| if (values[2] !== undefined) { | ||
| throw new Error(ErrorMessage.ERROR_FEATURE_FORMAT) | ||
| } | ||
| } else { | ||
| if (values[1] !== undefined) { | ||
| throw new Error(ErrorMessage.ERROR_FEATURE_FORMAT) | ||
| } | ||
| } | ||
| translation = temp; | ||
| } | ||
| } | ||
| }); | ||
| return translation | ||
| } | ||
| handleSetLanguage = (value) => { | ||
| let state = {...this.state} | ||
| state.lang = value | ||
| this.setState(state) | ||
| } | ||
| render() { | ||
| return( | ||
| <LanguageContext.Provider value={this.state}> | ||
| {this.props.children} | ||
| </LanguageContext.Provider> | ||
| ) | ||
| } | ||
| } | ||
| Language.Consumer = LanguageContext.Consumer | ||
| export { Language, setDefault, setFeatures, setTranslations } |
| const ERROR_FEATURE_FORMAT = "Feature format text want's to be used but is disable. see setFeatures() documentation." | ||
| const ERROR_SET_DEFAULT = "setDefault function value must be string" | ||
| const ERROR_SET_TRANSLATIONS = "setTranslations object is not a instance of an object or it haves 0 keys" | ||
| const ERROR_DEFAULT_LANG_MISSING = "default lang is empty, please use setDefault function to set language to a default value" | ||
| const ERROR_LANG_INVALID = "The language '{lang}' is invalid. key not exists" | ||
| const ERROR_KEY_NOT_AVAILABLE = "the key is not available on object or file" | ||
| const ERROR_VALUE_IS_NOT_STRING = "value is not string. please provide a string or a content" | ||
| const ERROR_GET_VALUES_INVALID = "Some values given in get(..., value0, value1) are not string." | ||
| class ErrorMessage { | ||
| static get ERROR_FEATURE_FORMAT() { return ERROR_FEATURE_FORMAT } | ||
| static get ERROR_SET_DEFAULT() { return ERROR_SET_DEFAULT } | ||
| static get ERROR_SET_TRANSLATIONS() { return ERROR_SET_TRANSLATIONS } | ||
| static get ERROR_DEFAULT_LANG_MISSING() { return ERROR_DEFAULT_LANG_MISSING } | ||
| static get ERROR_LANG_INVALID() { return ERROR_LANG_INVALID } | ||
| static get ERROR_KEY_NOT_AVAILABLE() { return ERROR_KEY_NOT_AVAILABLE } | ||
| static get ERROR_VALUE_IS_NOT_STRING() { return ERROR_VALUE_IS_NOT_STRING } | ||
| static get ERROR_GET_VALUES_INVALID() { return ERROR_GET_VALUES_INVALID } | ||
| } | ||
| export { ErrorMessage } |
-112
| import { ErrorMessage } from './errors' | ||
| class Utils { | ||
| static mergeFeatures(current, toMerge) { | ||
| Object.keys(toMerge).forEach((toMergekey) => { | ||
| let valuetoMerge = toMerge[toMergekey] | ||
| if (!(toMergekey in current)) { | ||
| throw new Error(ErrorMessage.ERROR_KEY_NOT_AVAILABLE); | ||
| } | ||
| current[toMergekey] = valuetoMerge | ||
| }) | ||
| } | ||
| static isGlobalFile(lang, ...values) { | ||
| let value = values[0] | ||
| if (Utils.isParamObject('0', ...values)) { | ||
| if (lang in value) { | ||
| return false; | ||
| } else { | ||
| throw new Error(Utils.format(ErrorMessage.ERROR_LANG_INVALID, { lang: lang })); | ||
| } | ||
| } else if (Utils.isNotString(value)) { | ||
| throw new Error(ErrorMessage.ERROR_VALUE_IS_NOT_STRING); | ||
| } | ||
| return true; | ||
| } | ||
| static isParamObject(index, ...values) { | ||
| for (let k in values) { | ||
| let value = values[k] | ||
| let isIndexParam = (k === index) | ||
| if(isIndexParam) { | ||
| if (Utils.isObject(value)) { | ||
| return true; | ||
| } | ||
| } | ||
| } | ||
| return false; | ||
| } | ||
| static isObject(value) { | ||
| if (typeof value === 'object') { | ||
| return true; | ||
| } else { | ||
| return false; | ||
| } | ||
| } | ||
| static isString(value) { | ||
| if (typeof value === 'string') { | ||
| return true; | ||
| } else { | ||
| return false; | ||
| } | ||
| } | ||
| static isNotString(value) { | ||
| return (!Utils.isString(value)) | ||
| } | ||
| static getValuesFormatFeature(isGlobalFile, ...values) { | ||
| if (!isGlobalFile) { | ||
| if (Utils.isParamObject('2', ...values)) { | ||
| return values[2] | ||
| } else { | ||
| let array = [] | ||
| for (let key in values) { | ||
| let value = values[key] | ||
| if(key !== '0' && key !== '1' && value !== undefined) { | ||
| if(Utils.isNotString(value)) { | ||
| throw new Error(ErrorMessage.ERROR_GET_VALUES_INVALID) | ||
| } | ||
| array.push(value) | ||
| } | ||
| } | ||
| return array | ||
| } | ||
| } else { | ||
| if (Utils.isParamObject('1', ...values)) { | ||
| return values[1] | ||
| } else { | ||
| let array = [] | ||
| for (let key in values) { | ||
| let value = values[key] | ||
| if(key !== '0' && value !== undefined) { | ||
| if(Utils.isNotString(value)) { | ||
| throw new Error(ErrorMessage.ERROR_GET_VALUES_INVALID) | ||
| } | ||
| array.push(value) | ||
| } | ||
| } | ||
| return array | ||
| } | ||
| } | ||
| } | ||
| static format(value, values) { | ||
| for (let k in values) { | ||
| value = value.replace("{" + k + "}", values[k]) | ||
| } | ||
| return value; | ||
| } | ||
| } | ||
| export { Utils } |
Major refactor
Supply chain riskPackage has recently undergone a major refactor. It may be unstable or indicate significant internal changes. Use caution when updating to versions that include significant changes.
Found 1 instance in 1 package
No tests
QualityPackage does not have any tests. This is a strong signal of a poorly maintained or low quality package.
Found 1 instance in 1 package
66775
247.44%7
40%637
190.87%0
-100%19
Infinity%1
Infinity%