From 55f0ff5d57f2f6635c35d4f59a37fe38e3d170b2 Mon Sep 17 00:00:00 2001 From: Gert Hengeveld Date: Fri, 26 Oct 2018 14:08:13 +0200 Subject: [PATCH 1/2] Add prettier settings to package.json so VS Code will honor them. --- package.json | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/package.json b/package.json index 1144898a8..dbb53db2b 100644 --- a/package.json +++ b/package.json @@ -117,5 +117,10 @@ "!**/example/**" ] }, + "prettier": { + "trailingComma": "es5", + "singleQuote": true, + "printWidth": 100 + }, "lock": false } From c0757421f2ac91def6c8c4f05955f074bf451c53 Mon Sep 17 00:00:00 2001 From: Gert Hengeveld Date: Fri, 26 Oct 2018 14:09:01 +0200 Subject: [PATCH 2/2] Ensure withI18n hoists static members. Fixes #583. --- react-i18next.js | 141 +++++++++++++++++++++++++++++++++++-------- react-i18next.min.js | 2 +- src/context.js | 14 ++++- 3 files changed, 129 insertions(+), 28 deletions(-) diff --git a/react-i18next.js b/react-i18next.js index 7d4ec5f82..0f00db85f 100644 --- a/react-i18next.js +++ b/react-i18next.js @@ -379,14 +379,14 @@ } function unwrapExports (x) { - return x && x.__esModule && Object.prototype.hasOwnProperty.call(x, 'default') ? x['default'] : x; + return x && x.__esModule && Object.prototype.hasOwnProperty.call(x, 'default') ? x.default : x; } function createCommonjsModule(fn, module) { return module = { exports: {} }, fn(module, module.exports), module.exports; } - /** @license React v16.5.2 + /** @license React v16.6.0 * react-is.production.min.js * * Copyright (c) Facebook, Inc. and its affiliates. @@ -405,11 +405,13 @@ g = b ? Symbol.for("react.profiler") : 60114, h = b ? Symbol.for("react.provider") : 60109, k = b ? Symbol.for("react.context") : 60110, - l = b ? Symbol.for("react.async_mode") : 60111, + l = b ? Symbol.for("react.concurrent_mode") : 60111, m = b ? Symbol.for("react.forward_ref") : 60112, - n = b ? Symbol.for("react.placeholder") : 60113; + n = b ? Symbol.for("react.suspense") : 60113, + q = b ? Symbol.for("react.memo") : 60115, + r = b ? Symbol.for("react.lazy") : 60116; - function q(a) { + function t(a) { if ("object" === _typeof(a) && null !== a) { var p = a.$$typeof; @@ -441,8 +443,13 @@ } } - exports.typeOf = q; + function u(a) { + return t(a) === l; + } + + exports.typeOf = t; exports.AsyncMode = l; + exports.ConcurrentMode = l; exports.ContextConsumer = k; exports.ContextProvider = h; exports.Element = c; @@ -453,19 +460,21 @@ exports.StrictMode = f; exports.isValidElementType = function (a) { - return "string" === typeof a || "function" === typeof a || a === e || a === l || a === g || a === f || a === n || "object" === _typeof(a) && null !== a && ("function" === typeof a.then || a.$$typeof === h || a.$$typeof === k || a.$$typeof === m); + return "string" === typeof a || "function" === typeof a || a === e || a === l || a === g || a === f || a === n || "object" === _typeof(a) && null !== a && (a.$$typeof === r || a.$$typeof === q || a.$$typeof === h || a.$$typeof === k || a.$$typeof === m); }; exports.isAsyncMode = function (a) { - return q(a) === l; + return u(a); }; + exports.isConcurrentMode = u; + exports.isContextConsumer = function (a) { - return q(a) === k; + return t(a) === k; }; exports.isContextProvider = function (a) { - return q(a) === h; + return t(a) === h; }; exports.isElement = function (a) { @@ -473,30 +482,30 @@ }; exports.isForwardRef = function (a) { - return q(a) === m; + return t(a) === m; }; exports.isFragment = function (a) { - return q(a) === e; + return t(a) === e; }; exports.isProfiler = function (a) { - return q(a) === g; + return t(a) === g; }; exports.isPortal = function (a) { - return q(a) === d; + return t(a) === d; }; exports.isStrictMode = function (a) { - return q(a) === f; + return t(a) === f; }; var reactIs_production_min = /*#__PURE__*/Object.freeze({ }); - /** @license React v16.5.2 + /** @license React v16.6.0 * react-is.development.js * * Copyright (c) Facebook, Inc. and its affiliates. @@ -521,14 +530,71 @@ var REACT_PROFILER_TYPE = hasSymbol ? Symbol.for('react.profiler') : 0xead2; var REACT_PROVIDER_TYPE = hasSymbol ? Symbol.for('react.provider') : 0xeacd; var REACT_CONTEXT_TYPE = hasSymbol ? Symbol.for('react.context') : 0xeace; - var REACT_ASYNC_MODE_TYPE = hasSymbol ? Symbol.for('react.async_mode') : 0xeacf; + var REACT_CONCURRENT_MODE_TYPE = hasSymbol ? Symbol.for('react.concurrent_mode') : 0xeacf; var REACT_FORWARD_REF_TYPE = hasSymbol ? Symbol.for('react.forward_ref') : 0xead0; - var REACT_PLACEHOLDER_TYPE = hasSymbol ? Symbol.for('react.placeholder') : 0xead1; + var REACT_SUSPENSE_TYPE = hasSymbol ? Symbol.for('react.suspense') : 0xead1; + var REACT_MEMO_TYPE = hasSymbol ? Symbol.for('react.memo') : 0xead3; + var REACT_LAZY_TYPE = hasSymbol ? Symbol.for('react.lazy') : 0xead4; function isValidElementType(type) { return typeof type === 'string' || typeof type === 'function' || // Note: its typeof might be other than 'symbol' or 'number' if it's a polyfill. - type === REACT_FRAGMENT_TYPE || type === REACT_ASYNC_MODE_TYPE || type === REACT_PROFILER_TYPE || type === REACT_STRICT_MODE_TYPE || type === REACT_PLACEHOLDER_TYPE || _typeof(type) === 'object' && type !== null && (typeof type.then === 'function' || type.$$typeof === REACT_PROVIDER_TYPE || type.$$typeof === REACT_CONTEXT_TYPE || type.$$typeof === REACT_FORWARD_REF_TYPE); + type === REACT_FRAGMENT_TYPE || type === REACT_CONCURRENT_MODE_TYPE || type === REACT_PROFILER_TYPE || type === REACT_STRICT_MODE_TYPE || type === REACT_SUSPENSE_TYPE || _typeof(type) === 'object' && type !== null && (type.$$typeof === REACT_LAZY_TYPE || type.$$typeof === REACT_MEMO_TYPE || type.$$typeof === REACT_PROVIDER_TYPE || type.$$typeof === REACT_CONTEXT_TYPE || type.$$typeof === REACT_FORWARD_REF_TYPE); } + /** + * Forked from fbjs/warning: + * https://github.com/facebook/fbjs/blob/e66ba20ad5be433eb54423f2b097d829324d9de6/packages/fbjs/src/__forks__/warning.js + * + * Only change is we use console.warn instead of console.error, + * and do nothing when 'console' is not supported. + * This really simplifies the code. + * --- + * Similar to invariant but only logs a warning if the condition is not met. + * This can be used to log issues in development environments in critical + * paths. Removing the logging code for production environments will keep the + * same logic and follow the same code paths. + */ + + + var lowPriorityWarning = function lowPriorityWarning() {}; + + { + var printWarning = function printWarning(format) { + for (var _len = arguments.length, args = Array(_len > 1 ? _len - 1 : 0), _key = 1; _key < _len; _key++) { + args[_key - 1] = arguments[_key]; + } + + var argIndex = 0; + var message = 'Warning: ' + format.replace(/%s/g, function () { + return args[argIndex++]; + }); + + if (typeof console !== 'undefined') { + console.warn(message); + } + + try { + // --- Welcome to debugging React --- + // This error was thrown as a convenience so that you can use this stack + // to find the callsite that caused this warning to fire. + throw new Error(message); + } catch (x) {} + }; + + lowPriorityWarning = function lowPriorityWarning(condition, format) { + if (format === undefined) { + throw new Error('`lowPriorityWarning(condition, format, ...args)` requires a warning ' + 'message argument'); + } + + if (!condition) { + for (var _len2 = arguments.length, args = Array(_len2 > 2 ? _len2 - 2 : 0), _key2 = 2; _key2 < _len2; _key2++) { + args[_key2 - 2] = arguments[_key2]; + } + + printWarning.apply(undefined, [format].concat(args)); + } + }; + } + var lowPriorityWarning$1 = lowPriorityWarning; function typeOf(object) { if (_typeof(object) === 'object' && object !== null) { @@ -539,7 +605,7 @@ var type = object.type; switch (type) { - case REACT_ASYNC_MODE_TYPE: + case REACT_CONCURRENT_MODE_TYPE: case REACT_FRAGMENT_TYPE: case REACT_PROFILER_TYPE: case REACT_STRICT_MODE_TYPE: @@ -566,9 +632,11 @@ } return undefined; - } + } // AsyncMode alias is deprecated along with isAsyncMode - var AsyncMode = REACT_ASYNC_MODE_TYPE; + + var AsyncMode = REACT_CONCURRENT_MODE_TYPE; + var ConcurrentMode = REACT_CONCURRENT_MODE_TYPE; var ContextConsumer = REACT_CONTEXT_TYPE; var ContextProvider = REACT_PROVIDER_TYPE; var Element = REACT_ELEMENT_TYPE; @@ -577,9 +645,20 @@ var Profiler = REACT_PROFILER_TYPE; var Portal = REACT_PORTAL_TYPE; var StrictMode = REACT_STRICT_MODE_TYPE; + var hasWarnedAboutDeprecatedIsAsyncMode = false; // AsyncMode should be deprecated function isAsyncMode(object) { - return typeOf(object) === REACT_ASYNC_MODE_TYPE; + { + if (!hasWarnedAboutDeprecatedIsAsyncMode) { + hasWarnedAboutDeprecatedIsAsyncMode = true; + lowPriorityWarning$1(false, 'The ReactIs.isAsyncMode() alias has been deprecated, ' + 'and will be removed in React 17+. Update your code to use ' + 'ReactIs.isConcurrentMode() instead. It has the exact same API.'); + } + } + return isConcurrentMode(object); + } + + function isConcurrentMode(object) { + return typeOf(object) === REACT_CONCURRENT_MODE_TYPE; } function isContextConsumer(object) { @@ -616,6 +695,7 @@ exports.typeOf = typeOf; exports.AsyncMode = AsyncMode; + exports.ConcurrentMode = ConcurrentMode; exports.ContextConsumer = ContextConsumer; exports.ContextProvider = ContextProvider; exports.Element = Element; @@ -626,6 +706,7 @@ exports.StrictMode = StrictMode; exports.isValidElementType = isValidElementType; exports.isAsyncMode = isAsyncMode; + exports.isConcurrentMode = isConcurrentMode; exports.isContextConsumer = isContextConsumer; exports.isContextProvider = isContextProvider; exports.isElement = isElement; @@ -1063,8 +1144,13 @@ return WithContext; }; } + + function getDisplayName(component) { + return component.displayName || component.name || 'Component'; + } /* eslint-disable react/no-multi-comp */ + function withI18n() { return function Wrapper(WrappedComponent) { var WithMergedOptions = @@ -1118,7 +1204,10 @@ return WithMergedOptions; }(React.Component); - return withContext()(WithMergedOptions); + var WithMergedOptionsWithContext = withContext()(WithMergedOptions); + WithMergedOptionsWithContext.WrappedComponent = WrappedComponent; + WithMergedOptionsWithContext.displayName = "WithMergedOptions(".concat(getDisplayName(WrappedComponent), ")"); + return hoistNonReactStatics_cjs(WithMergedOptionsWithContext, WrappedComponent); }; } @@ -1328,7 +1417,7 @@ return React__default.createElement(NamespacesConsumer, props); } - function getDisplayName(component) { + function getDisplayName$1(component) { return component.displayName || component.name || 'Component'; } @@ -1394,7 +1483,7 @@ var LoadNamespaceWithContext = withI18n()(LoadNamespace); LoadNamespaceWithContext.WrappedComponent = WrappedComponent; - LoadNamespaceWithContext.displayName = "LoadNamespace(".concat(getDisplayName(WrappedComponent), ")"); + LoadNamespaceWithContext.displayName = "LoadNamespace(".concat(getDisplayName$1(WrappedComponent), ")"); LoadNamespaceWithContext.namespaces = namespaceArg; return hoistNonReactStatics_cjs(LoadNamespaceWithContext, WrappedComponent); }; diff --git a/react-i18next.min.js b/react-i18next.min.js index a863f67f8..7534fcd04 100644 --- a/react-i18next.min.js +++ b/react-i18next.min.js @@ -1 +1 @@ -!function(e,t){"object"==typeof exports&&"undefined"!=typeof module?t(exports,require("react")):"function"==typeof define&&define.amd?define(["exports","react"],t):t(e["react-i18next"]={},e.React)}(this,function(e,t){"use strict";var n="default"in t?t.default:t;function r(e){return(r="function"==typeof Symbol&&"symbol"==typeof Symbol.iterator?function(e){return typeof e}:function(e){return e&&"function"==typeof Symbol&&e.constructor===Symbol&&e!==Symbol.prototype?"symbol":typeof e})(e)}function o(e,t){if(!(e instanceof t))throw new TypeError("Cannot call a class as a function")}function i(e,t){for(var n=0;n=0||(o[n]=e[n]);return o}(e,t);if(Object.getOwnPropertySymbols){var i=Object.getOwnPropertySymbols(e);for(r=0;r=0||Object.prototype.propertyIsEnumerable.call(e,n)&&(o[n]=e[n])}return o}function d(e){if(void 0===e)throw new ReferenceError("this hasn't been initialised - super() hasn't been called");return e}function y(e,t){return!t||"object"!=typeof t&&"function"!=typeof t?d(e):t}function h(e,t){return function(e){if(Array.isArray(e))return e}(e)||function(e,t){var n=[],r=!0,o=!1,i=void 0;try{for(var a,s=e[Symbol.iterator]();!(r=(a=s.next()).done)&&(n.push(a.value),!t||n.length!==t);r=!0);}catch(e){o=!0,i=e}finally{try{r||null==s.return||s.return()}finally{if(o)throw i}}return n}(e,t)||function(){throw new TypeError("Invalid attempt to destructure non-iterable instance")}()}function m(){if(console&&console.warn){for(var e=arguments.length,t=new Array(e),n=0;n1&&void 0!==arguments[1]?arguments[1]:{};return function(s){var p,d=function(p){function d(){return o(this,d),y(this,l(d).apply(this,arguments))}return u(d,t.Component),a(d,[{key:"shouldComponentUpdate",value:function(e){return!this.props.i18nOptions.usePureComponent&&!i.usePureComponent||!function(e,t){if(x(e,t))return!0;if("object"!==r(e)||null===e||"object"!==r(t)||null===t)return!1;var n=Object.keys(e),o=Object.keys(t);if(n.length!==o.length)return!1;for(var i=0;i0?a:null}),function(e,r){var o=r.ready,i=f(r,["ready"]);return n.createElement(s,c({tReady:o},t.props,u,i))})}}]),d}(),h=Oe()(d);return h.WrappedComponent=s,h.displayName="LoadNamespace(".concat((p=s).displayName||p.name||"Component",")"),h.namespaces=e,X(h,s)}}xe.setDefaults=ye,xe.setI18n=me;var Ee={area:!0,base:!0,br:!0,col:!0,embed:!0,hr:!0,img:!0,input:!0,keygen:!0,link:!0,menuitem:!0,meta:!0,param:!0,source:!0,track:!0,wbr:!0},Pe=/([\w-]+)|=|(['"])([.\s\S]*?)\2/g,Ne=/(?:|<(?:"[^"]*"['"]*|'[^']*'['"]*|[^'">])+>)/g,Ce=Object.create?Object.create(null):{};function Ie(e,t,n,r,o){var i=t.indexOf("<",r),a=t.slice(r,-1===i?void 0:i);/^\s*$/.test(a)&&(a=" "),(!o&&i>-1&&n+e.length>=0||" "!==a)&&e.push({type:"text",content:a})}function _e(e,t){switch(t.type){case"text":return e+t.content;case"tag":return e+="<"+t.name+(t.attrs?function(e){var t=[];for(var n in e)t.push(n+'="'+e[n]+'"');return t.length?" "+t.join(" "):""}(t.attrs):"")+(t.voidElement?"/>":">"),t.voidElement?e:e+t.children.reduce(_e,"")+""}}var ke={parse:function(e,t){t||(t={}),t.components||(t.components=Ce);var n,r=[],o=-1,i=[],a={},s=!1;return e.replace(Ne,function(c,u){if(s){if(c!=="")return;s=!1}var l,p="/"!==c.charAt(1),f=0===c.indexOf("\x3c!--"),d=u+c.length,y=e.charAt(d);p&&!f&&(o++,"tag"===(n=function(e){var t,n=0,r=!0,o={type:"tag",name:"",voidElement:!1,attrs:{},children:[]};return e.replace(Pe,function(i){if("="===i)return r=!0,void n++;r?0===n?((Ee[i]||"/"===e.charAt(e.length-2))&&(o.voidElement=!0),o.name=i):(o.attrs[t]=i.replace(/^['"]|['"]$/g,""),t=void 0):(t&&(o.attrs[t]=t),t=i),n++,r=!1}),o}(c)).type&&t.components[n.name]&&(n.type="component",s=!0),n.voidElement||s||!y||"<"===y||Ie(n.children,e,o,d,t.ignoreWhitespace),a[n.tagName]=n,0===o&&r.push(n),(l=i[o-1])&&l.children.push(n),i[o]=n),(f||!p||n.voidElement)&&(f||o--,!s&&"<"!==y&&y&&Ie(l=-1===o?r:i[o].children,e,o,d,t.ignoreWhitespace))}),!r.length&&e.length&&Ie(r,e,0,0,t.ignoreWhitespace),r},stringify:function(e){return e.reduce(function(e,t){return e+_e("",t)},"")}};function $e(e){return e&&(e.children||e.props&&e.props.children)}function Re(e){return e&&e.children?e.children:e.props&&e.props.children}function Te(e,t,o){if(""===t)return[];if(!e)return[t];var i={};!function e(t){"[object Array]"!==Object.prototype.toString.call(t)&&(t=[t]),t.forEach(function(t){"string"!=typeof t&&($e(t)?e(Re(t)):"object"!==r(t)||n.isValidElement(t)||Object.assign(i,t))})}(e),t=o.services.interpolator.interpolate(t,i,o.language);var a=function e(t,o){return"[object Array]"!==Object.prototype.toString.call(t)&&(t=[t]),"[object Array]"!==Object.prototype.toString.call(o)&&(o=[o]),o.reduce(function(o,i,a){if("tag"===i.type){var s=t[parseInt(i.name,10)]||{},u=n.isValidElement(s);if("string"==typeof s)o.push(s);else if($e(s)){var l=e(Re(s),i.children);s.dummy&&(s.children=l),o.push(n.cloneElement(s,c({},s.props,{key:a}),l))}else if("object"!==r(s)||u)o.push(s);else{var p=i.children[0]?i.children[0].content:null;p&&o.push(p)}}else"text"===i.type&&o.push(i.content);return o},[])}([{dummy:!0,children:e}],ke.parse("<0>".concat(t,"")));return Re(a[0])}var Me=function(e){function t(){return o(this,t),y(this,l(t).apply(this,arguments))}return u(t,n.Component),a(t,[{key:"render",value:function(){var e=this.props,t=e.children,o=e.count,i=e.parent,a=e.i18nKey,s=e.tOptions,u=e.values,l=e.defaults,p=e.components,d=e.ns,y=e.i18n,h=e.t,v=(e.defaultNS,e.reportNS,e.lng,e.i18nOptions,f(e,["children","count","parent","i18nKey","tOptions","values","defaults","components","ns","i18n","t","defaultNS","reportNS","lng","i18nOptions"])),b=h||y.t.bind(y),g=y.options&&y.options.react||{},O=void 0!==i?i:g.defaultTransParent,S=l||function e(t,o,i){return o?("[object Array]"!==Object.prototype.toString.call(o)&&(o=[o]),o.forEach(function(o,i){var a="".concat(i);if("string"==typeof o)t="".concat(t).concat(o);else if($e(o))t="".concat(t,"<").concat(a,">").concat(e("",Re(o),i+1),"");else if(n.isValidElement(o))t="".concat(t,"<").concat(a,">");else if("object"===r(o)){var s=c({},o),u=s.format;delete s.format;var l=Object.keys(s);u&&1===l.length?t="".concat(t,"{{").concat(l[0],", ").concat(u,"}}"):1===l.length?t="".concat(t,"{{").concat(l[0],"}}"):m("react-i18next: the passed in object contained more than one variable - the object should look like {{ value, format }} where format is optional.",o)}else m("Trans: the passed in value is invalid - seems you passed in a variable like {number} - please pass in variables for interpolation as full objects like {{number}}.",o)}),t):""}("",t),w=g.hashTransKey,j=a||(w?w(S):S),x=j?b(j,c({},s,u,u?{}:{interpolation:{prefix:"#$?",suffix:"?$#"}},{defaultValue:S,count:o,ns:d})):S;if(g.exposeNamespace){var E="string"==typeof b.ns?b.ns:b.ns[0];if(a&&y.options&&y.options.nsSeparator&&a.indexOf(y.options.nsSeparator)>-1)E=a.split(y.options.nsSeparator)[0];b.ns&&(v["data-i18next-options"]=JSON.stringify({ns:E}))}return O?n.createElement(O,v,Te(p||t,x,y)):Te(p||t,x,y)}}]),t}(),Ae=Oe()(Me),De=function(e){function r(e){var t;return o(this,r),t=y(this,l(r).call(this,e)),O(e,!0),t}return u(r,t.Component),a(r,[{key:"componentWillReceiveProps",value:function(e){if(this.props.i18n!==e.i18n)throw new Error("[react-i18next][I18nextProvider]does not support changing the i18n object.")}},{key:"render",value:function(){var e=this.props,t=e.children,r=e.i18n,o=e.defaultNS,i=e.reportNS;return n.createElement(ge.Provider,{value:{i18n:r,defaultNS:o,reportNS:i,lng:r&&r.language,t:r&&r.t.bind(r)}},t)}}]),r}(),Ve=function(e){function r(e){var t;return o(this,r),t=y(this,l(r).call(this,e)),g('Interpolate is deprecated and will be removed in the next major version (v9.0.0). Usage can be replaced by the "Trans" component'),t}return u(r,t.Component),a(r,[{key:"render",value:function(){var e=this,t=this.props,r=t.i18n,o=t.t,i=t.i18nKey,a=t.options,s=t.className,u=t.style,l=this.props.parent||"span",p=this.props.regexp||r.services.interpolator.regexp,f=this.props.useDangerouslySetInnerHTML||!1,d=this.props.dangerouslySetInnerHTMLPartElement||"span",y=o(i,c({},{},a,{interpolation:{prefix:"#$?",suffix:"?$#"}}));if(!y||"string"!=typeof y)return n.createElement("noscript",null);var h=[];y.split(p).reduce(function(t,o,i){var a;if(i%2==0){if(0===o.length)return t;a=f?n.createElement(d,{dangerouslySetInnerHTML:{__html:o}}):o}else a=function(e,t){if(e.indexOf(r.options.interpolation.formatSeparator)<0)return void 0===t[e]&&r.services.logger.warn("interpolator: missed to pass in variable ".concat(e," for interpolating ").concat(y)),t[e];var n=e.split(r.options.interpolation.formatSeparator),o=n.shift().trim(),i=n.join(r.options.interpolation.formatSeparator).trim();return void 0===t[o]&&r.services.logger.warn("interpolator: missed to pass in variable ".concat(o," for interpolating ").concat(y)),r.options.interpolation.format(t[o],i,r.language)}(o,e.props);return t.push(a),t},h);var m={};if(r.options.react&&r.options.react.exposeNamespace){var v="string"==typeof o.ns?o.ns:o.ns[0];if(i&&r.options.nsSeparator&&i.indexOf(r.options.nsSeparator)>-1)v=i.split(r.options.nsSeparator)[0];o.ns&&(m["data-i18next-options"]=JSON.stringify({ns:v}))}return s&&(m.className=s),u&&(m.style=u),n.createElement.apply(this,[l,m].concat(h))}}]),r}(),Fe=Oe()(Ve);e.loadNamespaces=function(e){var t=e.components,n=e.i18n,r=w(t);return new Promise(function(e){n.loadNamespaces(r,e)})},e.withNamespaces=xe,e.translate=function(e,t){return g('translate was renamed to "withNamespaces" to make it more clear what the HOC does.'),xe(e,t)},e.NamespacesConsumer=je,e.I18n=function(e){return g('I18n was renamed to "NamespacesConsumer" to make it more clear what the render prop does.'),n.createElement(je,e)},e.Trans=Ae,e.I18nextProvider=De,e.withI18n=Oe,e.I18nContext=ge,e.reactI18nextModule=be,e.setDefaults=ye,e.getDefaults=he,e.setI18n=me,e.getI18n=ve,e.Interpolate=Fe,Object.defineProperty(e,"__esModule",{value:!0})}); +!function(e,t){"object"==typeof exports&&"undefined"!=typeof module?t(exports,require("react")):"function"==typeof define&&define.amd?define(["exports","react"],t):t(e["react-i18next"]={},e.React)}(this,function(e,t){"use strict";var n="default"in t?t.default:t;function r(e){return(r="function"==typeof Symbol&&"symbol"==typeof Symbol.iterator?function(e){return typeof e}:function(e){return e&&"function"==typeof Symbol&&e.constructor===Symbol&&e!==Symbol.prototype?"symbol":typeof e})(e)}function o(e,t){if(!(e instanceof t))throw new TypeError("Cannot call a class as a function")}function i(e,t){for(var n=0;n=0||(o[n]=e[n]);return o}(e,t);if(Object.getOwnPropertySymbols){var i=Object.getOwnPropertySymbols(e);for(r=0;r=0||Object.prototype.propertyIsEnumerable.call(e,n)&&(o[n]=e[n])}return o}function d(e){if(void 0===e)throw new ReferenceError("this hasn't been initialised - super() hasn't been called");return e}function y(e,t){return!t||"object"!=typeof t&&"function"!=typeof t?d(e):t}function h(e,t){return function(e){if(Array.isArray(e))return e}(e)||function(e,t){var n=[],r=!0,o=!1,i=void 0;try{for(var a,s=e[Symbol.iterator]();!(r=(a=s.next()).done)&&(n.push(a.value),!t||n.length!==t);r=!0);}catch(e){o=!0,i=e}finally{try{r||null==s.return||s.return()}finally{if(o)throw i}}return n}(e,t)||function(){throw new TypeError("Invalid attempt to destructure non-iterable instance")}()}function m(){if(console&&console.warn){for(var e=arguments.length,t=new Array(e),n=0;n2?n-2:0),o=2;o1?t-1:0),r=1;r1&&void 0!==arguments[1]?arguments[1]:{};return function(s){var p,d=function(p){function d(){return o(this,d),y(this,l(d).apply(this,arguments))}return u(d,t.Component),a(d,[{key:"shouldComponentUpdate",value:function(e){return!this.props.i18nOptions.usePureComponent&&!i.usePureComponent||!function(e,t){if(x(e,t))return!0;if("object"!==r(e)||null===e||"object"!==r(t)||null===t)return!1;var n=Object.keys(e),o=Object.keys(t);if(n.length!==o.length)return!1;for(var i=0;i0?a:null}),function(e,r){var o=r.ready,i=f(r,["ready"]);return n.createElement(s,c({tReady:o},t.props,u,i))})}}]),d}(),h=je()(d);return h.WrappedComponent=s,h.displayName="LoadNamespace(".concat((p=s).displayName||p.name||"Component",")"),h.namespaces=e,te(h,s)}}Pe.setDefaults=ve,Pe.setI18n=ge;var Ne={area:!0,base:!0,br:!0,col:!0,embed:!0,hr:!0,img:!0,input:!0,keygen:!0,link:!0,menuitem:!0,meta:!0,param:!0,source:!0,track:!0,wbr:!0},Ie=/([\w-]+)|=|(['"])([.\s\S]*?)\2/g,_e=/(?:|<(?:"[^"]*"['"]*|'[^']*'['"]*|[^'">])+>)/g,$e=Object.create?Object.create(null):{};function ke(e,t,n,r,o){var i=t.indexOf("<",r),a=t.slice(r,-1===i?void 0:i);/^\s*$/.test(a)&&(a=" "),(!o&&i>-1&&n+e.length>=0||" "!==a)&&e.push({type:"text",content:a})}function Me(e,t){switch(t.type){case"text":return e+t.content;case"tag":return e+="<"+t.name+(t.attrs?function(e){var t=[];for(var n in e)t.push(n+'="'+e[n]+'"');return t.length?" "+t.join(" "):""}(t.attrs):"")+(t.voidElement?"/>":">"),t.voidElement?e:e+t.children.reduce(Me,"")+""}}var Re={parse:function(e,t){t||(t={}),t.components||(t.components=$e);var n,r=[],o=-1,i=[],a={},s=!1;return e.replace(_e,function(c,u){if(s){if(c!=="")return;s=!1}var l,p="/"!==c.charAt(1),f=0===c.indexOf("\x3c!--"),d=u+c.length,y=e.charAt(d);p&&!f&&(o++,"tag"===(n=function(e){var t,n=0,r=!0,o={type:"tag",name:"",voidElement:!1,attrs:{},children:[]};return e.replace(Ie,function(i){if("="===i)return r=!0,void n++;r?0===n?((Ne[i]||"/"===e.charAt(e.length-2))&&(o.voidElement=!0),o.name=i):(o.attrs[t]=i.replace(/^['"]|['"]$/g,""),t=void 0):(t&&(o.attrs[t]=t),t=i),n++,r=!1}),o}(c)).type&&t.components[n.name]&&(n.type="component",s=!0),n.voidElement||s||!y||"<"===y||ke(n.children,e,o,d,t.ignoreWhitespace),a[n.tagName]=n,0===o&&r.push(n),(l=i[o-1])&&l.children.push(n),i[o]=n),(f||!p||n.voidElement)&&(f||o--,!s&&"<"!==y&&y&&ke(l=-1===o?r:i[o].children,e,o,d,t.ignoreWhitespace))}),!r.length&&e.length&&ke(r,e,0,0,t.ignoreWhitespace),r},stringify:function(e){return e.reduce(function(e,t){return e+Me("",t)},"")}};function Te(e){return e&&(e.children||e.props&&e.props.children)}function Ae(e){return e&&e.children?e.children:e.props&&e.props.children}function De(e,t,o){if(""===t)return[];if(!e)return[t];var i={};!function e(t){"[object Array]"!==Object.prototype.toString.call(t)&&(t=[t]),t.forEach(function(t){"string"!=typeof t&&(Te(t)?e(Ae(t)):"object"!==r(t)||n.isValidElement(t)||Object.assign(i,t))})}(e),t=o.services.interpolator.interpolate(t,i,o.language);var a=function e(t,o){return"[object Array]"!==Object.prototype.toString.call(t)&&(t=[t]),"[object Array]"!==Object.prototype.toString.call(o)&&(o=[o]),o.reduce(function(o,i,a){if("tag"===i.type){var s=t[parseInt(i.name,10)]||{},u=n.isValidElement(s);if("string"==typeof s)o.push(s);else if(Te(s)){var l=e(Ae(s),i.children);s.dummy&&(s.children=l),o.push(n.cloneElement(s,c({},s.props,{key:a}),l))}else if("object"!==r(s)||u)o.push(s);else{var p=i.children[0]?i.children[0].content:null;p&&o.push(p)}}else"text"===i.type&&o.push(i.content);return o},[])}([{dummy:!0,children:e}],Re.parse("<0>".concat(t,"")));return Ae(a[0])}var Ve=function(e){function t(){return o(this,t),y(this,l(t).apply(this,arguments))}return u(t,n.Component),a(t,[{key:"render",value:function(){var e=this.props,t=e.children,o=e.count,i=e.parent,a=e.i18nKey,s=e.tOptions,u=e.values,l=e.defaults,p=e.components,d=e.ns,y=e.i18n,h=e.t,v=(e.defaultNS,e.reportNS,e.lng,e.i18nOptions,f(e,["children","count","parent","i18nKey","tOptions","values","defaults","components","ns","i18n","t","defaultNS","reportNS","lng","i18nOptions"])),b=h||y.t.bind(y),g=y.options&&y.options.react||{},O=void 0!==i?i:g.defaultTransParent,S=l||function e(t,o,i){return o?("[object Array]"!==Object.prototype.toString.call(o)&&(o=[o]),o.forEach(function(o,i){var a="".concat(i);if("string"==typeof o)t="".concat(t).concat(o);else if(Te(o))t="".concat(t,"<").concat(a,">").concat(e("",Ae(o),i+1),"");else if(n.isValidElement(o))t="".concat(t,"<").concat(a,">");else if("object"===r(o)){var s=c({},o),u=s.format;delete s.format;var l=Object.keys(s);u&&1===l.length?t="".concat(t,"{{").concat(l[0],", ").concat(u,"}}"):1===l.length?t="".concat(t,"{{").concat(l[0],"}}"):m("react-i18next: the passed in object contained more than one variable - the object should look like {{ value, format }} where format is optional.",o)}else m("Trans: the passed in value is invalid - seems you passed in a variable like {number} - please pass in variables for interpolation as full objects like {{number}}.",o)}),t):""}("",t),w=g.hashTransKey,j=a||(w?w(S):S),x=j?b(j,c({},s,u,u?{}:{interpolation:{prefix:"#$?",suffix:"?$#"}},{defaultValue:S,count:o,ns:d})):S;if(g.exposeNamespace){var E="string"==typeof b.ns?b.ns:b.ns[0];if(a&&y.options&&y.options.nsSeparator&&a.indexOf(y.options.nsSeparator)>-1)E=a.split(y.options.nsSeparator)[0];b.ns&&(v["data-i18next-options"]=JSON.stringify({ns:E}))}return O?n.createElement(O,v,De(p||t,x,y)):De(p||t,x,y)}}]),t}(),We=je()(Ve),Fe=function(e){function r(e){var t;return o(this,r),t=y(this,l(r).call(this,e)),O(e,!0),t}return u(r,t.Component),a(r,[{key:"componentWillReceiveProps",value:function(e){if(this.props.i18n!==e.i18n)throw new Error("[react-i18next][I18nextProvider]does not support changing the i18n object.")}},{key:"render",value:function(){var e=this.props,t=e.children,r=e.i18n,o=e.defaultNS,i=e.reportNS;return n.createElement(we.Provider,{value:{i18n:r,defaultNS:o,reportNS:i,lng:r&&r.language,t:r&&r.t.bind(r)}},t)}}]),r}(),Le=function(e){function r(e){var t;return o(this,r),t=y(this,l(r).call(this,e)),g('Interpolate is deprecated and will be removed in the next major version (v9.0.0). Usage can be replaced by the "Trans" component'),t}return u(r,t.Component),a(r,[{key:"render",value:function(){var e=this,t=this.props,r=t.i18n,o=t.t,i=t.i18nKey,a=t.options,s=t.className,u=t.style,l=this.props.parent||"span",p=this.props.regexp||r.services.interpolator.regexp,f=this.props.useDangerouslySetInnerHTML||!1,d=this.props.dangerouslySetInnerHTMLPartElement||"span",y=o(i,c({},{},a,{interpolation:{prefix:"#$?",suffix:"?$#"}}));if(!y||"string"!=typeof y)return n.createElement("noscript",null);var h=[];y.split(p).reduce(function(t,o,i){var a;if(i%2==0){if(0===o.length)return t;a=f?n.createElement(d,{dangerouslySetInnerHTML:{__html:o}}):o}else a=function(e,t){if(e.indexOf(r.options.interpolation.formatSeparator)<0)return void 0===t[e]&&r.services.logger.warn("interpolator: missed to pass in variable ".concat(e," for interpolating ").concat(y)),t[e];var n=e.split(r.options.interpolation.formatSeparator),o=n.shift().trim(),i=n.join(r.options.interpolation.formatSeparator).trim();return void 0===t[o]&&r.services.logger.warn("interpolator: missed to pass in variable ".concat(o," for interpolating ").concat(y)),r.options.interpolation.format(t[o],i,r.language)}(o,e.props);return t.push(a),t},h);var m={};if(r.options.react&&r.options.react.exposeNamespace){var v="string"==typeof o.ns?o.ns:o.ns[0];if(i&&r.options.nsSeparator&&i.indexOf(r.options.nsSeparator)>-1)v=i.split(r.options.nsSeparator)[0];o.ns&&(m["data-i18next-options"]=JSON.stringify({ns:v}))}return s&&(m.className=s),u&&(m.style=u),n.createElement.apply(this,[l,m].concat(h))}}]),r}(),Be=je()(Le);e.loadNamespaces=function(e){var t=e.components,n=e.i18n,r=w(t);return new Promise(function(e){n.loadNamespaces(r,e)})},e.withNamespaces=Pe,e.translate=function(e,t){return g('translate was renamed to "withNamespaces" to make it more clear what the HOC does.'),Pe(e,t)},e.NamespacesConsumer=Ce,e.I18n=function(e){return g('I18n was renamed to "NamespacesConsumer" to make it more clear what the render prop does.'),n.createElement(Ce,e)},e.Trans=We,e.I18nextProvider=Fe,e.withI18n=je,e.I18nContext=we,e.reactI18nextModule=Se,e.setDefaults=ve,e.getDefaults=be,e.setI18n=ge,e.getI18n=Oe,e.Interpolate=Be,Object.defineProperty(e,"__esModule",{value:!0})}); diff --git a/src/context.js b/src/context.js index ea060642f..c3b44ca06 100644 --- a/src/context.js +++ b/src/context.js @@ -1,5 +1,6 @@ import React, { Component } from 'react'; import createReactContext from 'create-react-context'; +import hoistStatics from 'hoist-non-react-statics'; let defaultOptions = { wait: false, @@ -62,6 +63,10 @@ export function withContext() { }; } +function getDisplayName(component) { + return component.displayName || component.name || 'Component'; +} + /* eslint-disable react/no-multi-comp */ export function withI18n() { return function Wrapper(WrappedComponent) { @@ -106,6 +111,13 @@ export function withI18n() { } } - return withContext()(WithMergedOptions); + const WithMergedOptionsWithContext = withContext()(WithMergedOptions); + + WithMergedOptionsWithContext.WrappedComponent = WrappedComponent; + WithMergedOptionsWithContext.displayName = `WithMergedOptions(${getDisplayName( + WrappedComponent + )})`; + + return hoistStatics(WithMergedOptionsWithContext, WrappedComponent); }; }