reactlang.js
Version:
React.js localization component highly inspired by Laravel's Lang
3 lines (2 loc) • 10.2 kB
JavaScript
!function(e,t){"object"==typeof exports&&"undefined"!=typeof module?module.exports=t(require("preact")):"function"==typeof define&&define.amd?define(["preact"],t):e.ReactlangJs=t(e.preact)}(this,function(e){"use strict";e=e&&e.hasOwnProperty("default")?e.default:e;"undefined"!=typeof window?window:"undefined"!=typeof global?global:"undefined"!=typeof self&&self;var t,r=(function(e,t){var r;r=function(){function e(e){return"-Inf"===e?-1/0:"+Inf"===e||"Inf"===e||"*"===e?1/0:parseInt(e,10)}var t=/^({\s*(\-?\d+(\.\d+)?[\s*,\s*\-?\d+(\.\d+)?]*)\s*})|([\[\]])\s*(-Inf|\*|\-?\d+(\.\d+)?)\s*,\s*(\+?Inf|\*|\-?\d+(\.\d+)?)\s*([\[\]])$/,r=/({\s*(\-?\d+(\.\d+)?[\s*,\s*\-?\d+(\.\d+)?]*)\s*})|([\[\]])\s*(-Inf|\*|\-?\d+(\.\d+)?)\s*,\s*(\+?Inf|\*|\-?\d+(\.\d+)?)\s*([\[\]])/,n="en",s=function(e){e=e||{},this.locale=e.locale||function(){if("undefined"!=typeof document&&document.documentElement)return document.documentElement.lang}()||n,this.fallback=e.fallback,this.messages=e.messages};return s.prototype.setMessages=function(e){this.messages=e},s.prototype.getLocale=function(){return this.locale||this.fallback},s.prototype.setLocale=function(e){this.locale=e},s.prototype.getFallback=function(){return this.fallback},s.prototype.setFallback=function(e){this.fallback=e},s.prototype.has=function(e,t){return!("string"!=typeof e||!this.messages)&&null!==this._getMessage(e,t)},s.prototype.get=function(e,t,r){if(!this.has(e,r))return e;var n=this._getMessage(e,r);return null===n?e:(t&&(n=this._applyReplacements(n,t)),n)},s.prototype.trans=function(e,t){return this.get(e,t)},s.prototype.choice=function(e,t,n,s){(n=void 0!==n?n:{}).count=t;var a=this.get(e,n,s);if(null===a||void 0===a)return a;for(var o=a.split("|"),i=[],c=0;c<o.length;c++)if(o[c]=o[c].trim(),r.test(o[c])){var l=o[c].split(/\s/);i.push(l.shift()),o[c]=l.join(" ")}if(1===o.length)return a;for(var u=0;u<i.length;u++)if(this._testInterval(t,i[u]))return o[u];return s=s||this._getLocale(e),o[this._getPluralForm(t,s)]},s.prototype.transChoice=function(e,t,r){return this.choice(e,t,r)},s.prototype._parseKey=function(e,t){if("string"!=typeof e||"string"!=typeof t)return null;var r=e.split("."),n=r[0].replace(/\//g,".");return{source:t+"."+n,sourceFallback:this.getFallback()+"."+n,entries:r.slice(1)}},s.prototype._getMessage=function(e,t){if(t=t||this.getLocale(),e=this._parseKey(e,t),void 0===this.messages[e.source]&&void 0===this.messages[e.sourceFallback])return null;for(var r=this.messages[e.source],n=e.entries.slice(),s="";n.length&&void 0!==r;){void 0!==r[s=s?s.concat(".",n.shift()):n.shift()]&&(r=r[s],s="")}if("string"!=typeof r&&this.messages[e.sourceFallback])for(r=this.messages[e.sourceFallback],n=e.entries.slice(),s="";n.length&&void 0!==r;){r[s=s?s.concat(".",n.shift()):n.shift()]&&(r=r[s],s="")}return"string"!=typeof r?null:r},s.prototype._getLocale=function(e){return e=this._parseKey(e,this.locale),this.messages[e.source]?this.locale:this.messages[e.sourceFallback]?this.fallback:null},s.prototype._findMessageInTree=function(e,t){for(;e.length&&void 0!==t;){var r=e.join(".");if(t[r]){t=t[r];break}t=t[e.shift()]}return t},s.prototype._sortReplacementKeys=function(e,t){return t.length-e.length},s.prototype._applyReplacements=function(e,t){return Object.keys(t).sort(this._sortReplacementKeys).forEach(function(r){e=e.replace(new RegExp(":"+r,"gi"),function(e){var n=t[r];return e===e.toUpperCase()?n.toUpperCase():e===e.replace(/\w/i,function(e){return e.toUpperCase()})?n.charAt(0).toUpperCase()+n.slice(1):n})}),e},s.prototype._testInterval=function(r,n){if("string"!=typeof n)throw"Invalid interval: should be a string.";var s=(n=n.trim()).match(t);if(!s)throw"Invalid interval: "+n;if(!s[2]){var a=(s=s.filter(function(e){return!!e}))[1],o=e(s[2]);o===1/0&&(o=-1/0);var i=e(s[3]),c=s[4];return("["===a?r>=o:r>o)&&("]"===c?r<=i:r<i)}for(var l=s[2].split(","),u=0;u<l.length;u++)if(parseInt(l[u],10)===r)return!0;return!1},s.prototype._getPluralForm=function(e,t){switch(t){case"az":case"bo":case"dz":case"id":case"ja":case"jv":case"ka":case"km":case"kn":case"ko":case"ms":case"th":case"tr":case"vi":case"zh":return 0;case"af":case"bn":case"bg":case"ca":case"da":case"de":case"el":case"en":case"eo":case"es":case"et":case"eu":case"fa":case"fi":case"fo":case"fur":case"fy":case"gl":case"gu":case"ha":case"he":case"hu":case"is":case"it":case"ku":case"lb":case"ml":case"mn":case"mr":case"nah":case"nb":case"ne":case"nl":case"nn":case"no":case"om":case"or":case"pa":case"pap":case"ps":case"pt":case"so":case"sq":case"sv":case"sw":case"ta":case"te":case"tk":case"ur":case"zu":return 1==e?0:1;case"am":case"bh":case"fil":case"fr":case"gun":case"hi":case"hy":case"ln":case"mg":case"nso":case"xbr":case"ti":case"wa":return 0===e||1===e?0:1;case"be":case"bs":case"hr":case"ru":case"sr":case"uk":return e%10==1&&e%100!=11?0:e%10>=2&&e%10<=4&&(e%100<10||e%100>=20)?1:2;case"cs":case"sk":return 1==e?0:e>=2&&e<=4?1:2;case"ga":return 1==e?0:2==e?1:2;case"lt":return e%10==1&&e%100!=11?0:e%10>=2&&(e%100<10||e%100>=20)?1:2;case"sl":return e%100==1?0:e%100==2?1:e%100==3||e%100==4?2:3;case"mk":return e%10==1?0:1;case"mt":return 1==e?0:0===e||e%100>1&&e%100<11?1:e%100>10&&e%100<20?2:3;case"lv":return 0===e?0:e%10==1&&e%100!=11?1:2;case"pl":return 1==e?0:e%10>=2&&e%10<=4&&(e%100<12||e%100>14)?1:2;case"cy":return 1==e?0:2==e?1:8==e||11==e?2:3;case"ro":return 1==e?0:0===e||e%100>0&&e%100<20?1:2;case"ar":return 0===e?0:1==e?1:2==e?2:e%100>=3&&e%100<=10?3:e%100>=11&&e%100<=99?4:5;default:return 0}},s},e.exports=r()}(t={exports:{}},t.exports),t.exports),n="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},s=function(e,t){if(!(e instanceof t))throw new TypeError("Cannot call a class as a function")},a=function(){function e(e,t){for(var r=0;r<t.length;r++){var n=t[r];n.enumerable=n.enumerable||!1,n.configurable=!0,"value"in n&&(n.writable=!0),Object.defineProperty(e,n.key,n)}}return function(t,r,n){return r&&e(t.prototype,r),n&&e(t,n),t}}(),o=Object.assign||function(e){for(var t=1;t<arguments.length;t++){var r=arguments[t];for(var n in r)Object.prototype.hasOwnProperty.call(r,n)&&(e[n]=r[n])}return e},i=function(e,t){if("function"!=typeof t&&null!==t)throw new TypeError("Super expression must either be null or a function, not "+typeof t);e.prototype=Object.create(t&&t.prototype,{constructor:{value:e,enumerable:!1,writable:!0,configurable:!0}}),t&&(Object.setPrototypeOf?Object.setPrototypeOf(e,t):e.__proto__=t)},c=function(e,t){if(!e)throw new ReferenceError("this hasn't been initialised - super() hasn't been called");return!t||"object"!=typeof t&&"function"!=typeof t?e:t},l=function(e){function t(e){s(this,t);var r=c(this,(t.__proto__||Object.getPrototypeOf(t)).call(this,e));return r.messages=e.messages,r}return i(t,r),a(t,[{key:"_parseKey",value:function(e){if("string"!=typeof e)return null;var t=e.split("."),r=t[0].replace(/\//g,".");return{source:r,sourceFallback:r,entries:t.slice(1)}}},{key:"getAndReplace",value:function(e,t){return"string"!=typeof e?null:"object"!==(void 0===t?"undefined":n(t))?null:this.choice(e,1,t)}}]),t}();return function(t){function r(e){s(this,r);var t=c(this,(r.__proto__||Object.getPrototypeOf(r)).call(this,e));return t.langHelper=new l({messages:e.lang}),t.applyPropsAndContentToChildren=t.applyPropsAndContentToChildren.bind(t),t.handleAttributes=t.handleAttributes.bind(t),t.handleNestedAttributes=t.handleNestedAttributes.bind(t),t.mungeString=t.mungeString.bind(t),t.removeUnwantedProps=t.removeUnwantedProps.bind(t),t}return i(r,t),a(r,[{key:"applyPropsAndContentToChildren",value:function(t){var r=arguments.length>1&&void 0!==arguments[1]?arguments[1]:null;return e.Children.map(this.props.children,function(n){var s=o({},n.props,t);for(var a in n.props)t[a]&&(t[a]instanceof Object?s[a]=o({},n.props[a],t[a]):"string"==typeof t[a]&&(s[a]=n.props[a]+" "+t[a]));if(t.nestedAttributes)for(var i in n.props)if(t.nestedAttributes[i]){var c=n.props[i];c&&c instanceof Object&&s[i]&&(s[i]=o({},c,t[i]))}var l=e.cloneElement(n,s,r);return n.props.children&&(l=e.cloneElement(n,s,n.props.children)),l})}},{key:"handleAttributes",value:function(e){var t=o({},e);for(var r in t){var n=t[r];if(n&&void 0===n.transKey)t[r]=this.handleAttributes(n);else if(n){var s=this.mungeString(n);t[r]=s}}return t}},{key:"handleNestedAttributes",value:function(e){var t=o({},e);for(var r in t.nestedAttributes){var n=t.nestedAttributes[r];if(n)for(var s in n){var a=n[s];if(a){var i=this.mungeString(a);t[r]={},t[r][s]=i}}}return t}},{key:"mungeString",value:function(e){var t=e.transKey,r=e.count,n=e.replacements,s=void 0;if(void 0!==r)s=this.langHelper.choice(t,r,n);else if(void 0===r&&void 0!==n){if(!this.props.handleError)throw new Error("Replacements were given without the required count property");this.props.handleError({message:"Replacements were given without the required count property",name:"No count supplied"})}else s=this.langHelper.get(t);return this.props.reference&&this.props.reference(s),s}},{key:"removeUnwantedProps",value:function(){var e=o({},this.props);return delete e.store,delete e.storeSubscription,delete e.dispatch,delete e.lang,e}},{key:"render",value:function(){if(this.props.children&&e.Children.count(this.props.children)>1){if(!this.props.handleError)throw new Error("The Translatable component only allows a single child");this.props.handleError({message:"The Translatable component only allows a single child",name:"Too many children"})}var t=this.removeUnwantedProps(),r=t.content;if(delete t.content,delete t.handleError,t.className?t.className="translatable "+t.className:t.className="translatable",t.attributes){var n=this.handleAttributes(t.attributes);delete(t=o({},t,n)).attributes}if(t.nestedAttributes&&(t=this.handleNestedAttributes(t)),this.props.reference&&delete t.reference,this.props.children){delete t.children;var s=void 0;if(r){var a=this.mungeString(r);s=this.applyPropsAndContentToChildren(t,a)}else s=this.applyPropsAndContentToChildren(t);var i=s[0];return e.createElement(i.type,i.props)}return h("span",t,this.mungeString(r))}}]),r}(e.Component)});
//# sourceMappingURL=reactlang.js.umd.min.js.map