Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

added test case

  • Loading branch information...
commit 1644ea0d28ca58539ed7598512ab92a34b27470a 1 parent ca905a6
@jamuhl jamuhl authored
Showing with 5 additions and 3 deletions.
  1. +1 −1  i18next-1.3.1.js
  2. +1 −1  i18next-1.3.1.min.js
  3. +3 −1 test/test.js
View
2  i18next-1.3.1.js
@@ -252,7 +252,7 @@
if (typeof value === 'object') {
str = applyReplacement(str, value, key);
} else {
- str = str.replace([o.interpolationPrefix, nestedKey ? nestedKey + '.' + key : key, o.interpolationSuffix].join(''), value);
+ str = str.replace(new RegExp([o.interpolationPrefix, nestedKey ? nestedKey + '.' + key : key, o.interpolationSuffix].join(''), 'g'), value);
}
});
return str;
View
2  i18next-1.3.1.min.js
@@ -1,2 +1,2 @@
-(function(){function j(a,h){typeof a=="function"&&(h=a,a={}),a=a||{},e.extend(d,a),typeof d.ns=="string"&&(d.ns={namespaces:[d.ns],defaultNs:d.ns}),d.lng?e.cookie.create("i18next",d.lng):d.lng=e.detectLanguage(),i=[];if(d.lng.indexOf("-")===2&&d.lng.length===5){var j=d.lng.split("-");d.lng=d.lowerCaseLng?j[0].toLowerCase()+"-"+j[1].toLowerCase():j[0].toLowerCase()+"-"+j[1].toUpperCase(),i.push(d.lng),i.push(d.lng.substr(0,2))}else i.push(d.lng);i.indexOf(d.fallbackLng)===-1&&i.push(d.fallbackLng),g=d.lng,e.log("currentLng set to: "+g),b&&d.setJqueryExt&&l();if(d.resStore){f=d.resStore,h&&h(q);return}c.sync.load(i,d,function(a,b){f=b,h&&h(q)})}function k(a,b){j({lng:a},b)}function l(){function a(a,c,d){if(c.length===0)return;var e="text";if(c.indexOf("[")===0){var f=c.split("]");c=f[1],e=f[0].substr(1,f[0].length-1)}c.indexOf(";")===c.length-1&&(c=c.substr(0,c.length-2));var g;e==="text"?(g=b.extend({defaultValue:a.text()},d),a.text(b.t(c,g))):(g=b.extend({defaultValue:a.attr(e)},d),a.attr(e,b.t(c,g)))}function c(c,d){var e=c.attr("data-i18n");if(!e)return;if(e.indexOf(";")<=e.length-1){var f=e.split(";");b.each(f,function(b,e){a(c,e,d)})}else a(c,e,d)}b.t=b.t||q,b.fn.i18n=function(a){return this.each(function(){c(b(this),a);var d=b(this).find("[data-i18n]");d.each(function(){c(b(this),a)})})}}function m(a,b,c){return e.each(b,function(b,e){typeof e=="object"?a=m(a,e,b):a=a.replace([d.interpolationPrefix,c?c+"."+b:b,d.interpolationSuffix].join(""),e)}),a}function n(a,b){while(a.indexOf(d.reusePrefix)!=-1){h++;if(h>d.maxRecursion)break;var c=a.indexOf(d.reusePrefix),e=a.indexOf(d.reuseSuffix,c)+d.reuseSuffix.length,f=a.substring(c,e),g=f.replace(d.reusePrefix,"").replace(d.reuseSuffix,""),i=r(g,b);a=a.replace(f,i)}return a}function o(a){return a.context&&typeof a.context=="string"}function p(a){return a.count!==undefined&&typeof a.count!="string"&&a.count!==1}function q(a,b){return h=0,r(a,b)}function r(a,b){b=b||{};var c,h,j=b.defaultValue||a;if(!f)return j;var k=d.ns.defaultNs;if(a.indexOf(d.nsseparator)>-1){var l=a.split(d.nsseparator);k=l[0],a=l[1]}if(o(b)){c=e.extend({},b),delete c.context,c.defaultValue=d.contextNotFound;var r=a+"_"+b.context;h=q(r,c);if(h!=d.contextNotFound)return m(h,{context:b.context})}if(p(b)){c=e.extend({},b),delete c.count,c.defaultValue=d.pluralNotFound;var s=a+d.pluralSuffix,t=v.get(g,b.count);t!=="other"&&(s=s+"_"+t),h=q(s,c);if(h!=d.pluralNotFound)return m(h,{count:b.count})}var w,x=a.split(d.keyseparator);for(var y=0,z=i.length;y<z;y++){if(w)break;var A=i[y],B=0,C=f[A][k];while(x[B])C=C&&C[x[B]],B++;C&&(typeof C!="string"&&(C="key '"+k+":"+a+" ("+A+")' "+"returned a object instead of string.",e.log(C)),C=m(C,b),C=n(C,b),w=C)}return!w&&d.sendMissing&&u.postMissing(k,a,j),w?w:j}function s(){var a,b=[];(function(){var a=window.location.search.substring(1),c=a.split("&");for(var d=0;d<c.length;d++){var e=c[d].indexOf("=");if(e>0){var f=c[d].substring(0,e),g=c[d].substring(e+1);b[f]=g}}})(),b.setLng&&(a=b.setLng,e.cookie.create("i18next",a));if(!a){var c=e.cookie.read("i18next");c&&(a=c)}return!a&&navigator&&(a=navigator.language?navigator.language:navigator.userLanguage),a||(a=d.fallbackLng),a}function t(){return g}Array.prototype.indexOf||(Array.prototype.indexOf=function(a){"use strict";if(this==null)throw new TypeError;var b=Object(this),c=b.length>>>0;if(c===0)return-1;var d=0;arguments.length>0&&(d=Number(arguments[1]),d!=d?d=0:d!=0&&d!=Infinity&&d!=-Infinity&&(d=(d>0||-1)*Math.floor(Math.abs(d))));if(d>=c)return-1;var e=d>=0?d:Math.max(c-Math.abs(d),0);for(;e<c;e++)if(e in b&&b[e]===a)return e;return-1});var a=this,b=a.jQuery,c={};typeof module!="undefined"&&module.exports?module.exports=c:b?b.i18n=b.i18n||c:a.i18n=a.i18n||c;var d={lng:undefined,lowerCaseLng:!1,fallbackLng:"dev",ns:"translation",nsseparator:":",keyseparator:".",debug:!1,resGetPath:"locales/__lng__/__ns__.json",resPostPath:"locales/add/__lng__/__ns__",resStore:undefined,useLocalStorage:!0,dynamicLoad:!1,sendMissing:!1,interpolationPrefix:"__",interpolationSuffix:"__",reusePrefix:"$t(",reuseSuffix:")",pluralSuffix:"_plural",pluralNotFound:["plural_not_found",Math.random()].join(""),contextNotFound:["context_not_found",Math.random()].join(""),setJqueryExt:!0},e={extend:b?b.extend:undefined,each:b?b.each:undefined,ajax:b?b.ajax:undefined,detectLanguage:s,log:function(a){d.debug&&console.log(a)},cookie:{create:function(a,b,c){var d;if(c){var e=new Date;e.setTime(e.getTime()+c*60*1e3),d="; expires="+e.toGMTString()}else d="";document.cookie=a+"="+b+d+"; path=/"},read:function(a){var b=a+"=",c=document.cookie.split(";");for(var d=0;d<c.length;d++){var e=c[d];while(e.charAt(0)==" ")e=e.substring(1,e.length);if(e.indexOf(b)===0)return e.substring(b.length,e.length)}return null},remove:function(a){this.create(a,"",-1)}}},f,g,h=0,i=[],u={load:function(a,b,c){b.useLocalStorage?u._loadLocal(a,function(d,f){var g=[];for(var h=0,i=a.length;h<i;h++)f[a[h]]||g.push(a[h]);g.length>0?u._fetch(g,b,function(a,b){e.extend(f,b),u._storeLocal(b),c(null,f)}):c(null,f)}):u._fetch(a,b,function(a,b){c(null,b)})},_loadLocal:function(a,b){var c={};if(window.localStorage){var d=a.length;e.each(a,function(a,e){var f=window.localStorage.getItem("res_"+e);f&&(c[e]=JSON.parse(f)),d--,d===0&&b(null,c)})}},_storeLocal:function(a){if(window.localStorage)for(var b in a)window.localStorage.setItem("res_"+b,JSON.stringify(a[b]));return},_fetch:function(a,b,c){var f=b.ns,g={};if(!b.dynamicLoad){var h=f.namespaces.length*a.length,i;e.each(f.namespaces,function(b,d){e.each(a,function(a,b){u._fetchOne(b,d,function(a,e){a&&(i=i||[],i.push(a)),g[b]=g[b]||{},g[b][d]=e,h--,h===0&&c(i,g)})})})}else{var j=m(d.resGetPath,{lng:a.join("+"),ns:f.namespaces.join("+")});e.ajax({url:j,success:function(a,b,d){e.log("loaded: "+j),c(null,a)},error:function(a,b,d){e.log("failed loading: "+j),c("failed loading resource.json error: "+d)},dataType:"json"})}},_fetchOne:function(a,b,c){var f=m(d.resGetPath,{lng:a,ns:b});e.ajax({url:f,success:function(a,b,d){e.log("loaded: "+f),c(null,a)},error:function(a,b,d){e.log("failed loading: "+f),c(d,{})},dataType:"json"})},postMissing:function(a,b,c){var g={};g[b]=c;var h=m(d.resPostPath,{lng:d.fallbackLng,ns:a});e.ajax({url:h,type:"POST",data:g,success:function(g,i,j){e.log("posted missing key '"+b+"' to: "+h),f[d.fallbackLng][a][b]=c},error:function(a,c,d){e.log("failed posting missing key '"+b+"' to: "+h)},dataType:"json"})}},v={rules:{sl:function(a){return a%100===1?"one":a%100===2?"two":a%100===3||a%100===4?"few":"other"},ar:function(a){return a===0?"zero":a===1?"one":a===2?"two":a%100>=3&&a%100<=10?"few":a%100>=11&&a%100<=99?"many":"other"}},addRule:function(a,b){v.rules[a]=b},get:function(a,b){function d(a,b){return v.rules[a]?v.rules[a](b):b===1?"one":"other"}var c=a.split("-");return c.length===2?d(c[0],b):d(a,b)}};c.init=j,c.setLng=k,c.t=q,c.translate=q,c.detectLanguage=e.detectLanguage,c.pluralExtensions=v,c.sync=u,c.functions=e,c.lng=t,c.options=d})()
+(function(){function init(options,cb){typeof options=="function"&&(cb=options,options={}),options=options||{},f.extend(o,options),typeof o.ns=="string"&&(o.ns={namespaces:[o.ns],defaultNs:o.ns}),o.lng?f.cookie.create("i18next",o.lng):o.lng=f.detectLanguage(),languages=[];if(o.lng.indexOf("-")===2&&o.lng.length===5){var parts=o.lng.split("-");o.lng=o.lowerCaseLng?parts[0].toLowerCase()+"-"+parts[1].toLowerCase():parts[0].toLowerCase()+"-"+parts[1].toUpperCase(),languages.push(o.lng),languages.push(o.lng.substr(0,2))}else languages.push(o.lng);languages.indexOf(o.fallbackLng)===-1&&languages.push(o.fallbackLng),currentLng=o.lng,f.log("currentLng set to: "+currentLng),$&&o.setJqueryExt&&addJqueryFunct();if(o.resStore){resStore=o.resStore,cb&&cb(translate);return}i18n.sync.load(languages,o,function(err,store){resStore=store,cb&&cb(translate)})}function setLng(lng,cb){init({lng:lng},cb)}function addJqueryFunct(){function parse(ele,key,options){if(key.length===0)return;var attr="text";if(key.indexOf("[")===0){var parts=key.split("]");key=parts[1],attr=parts[0].substr(1,parts[0].length-1)}key.indexOf(";")===key.length-1&&(key=key.substr(0,key.length-2));var optionsToUse;attr==="text"?(optionsToUse=$.extend({defaultValue:ele.text()},options),ele.text($.t(key,optionsToUse))):(optionsToUse=$.extend({defaultValue:ele.attr(attr)},options),ele.attr(attr,$.t(key,optionsToUse)))}function localize(ele,options){var key=ele.attr("data-i18n");if(!key)return;if(key.indexOf(";")<=key.length-1){var keys=key.split(";");$.each(keys,function(m,k){parse(ele,k,options)})}else parse(ele,key,options)}$.t=$.t||translate,$.fn.i18n=function(options){return this.each(function(){localize($(this),options);var elements=$(this).find("[data-i18n]");elements.each(function(){localize($(this),options)})})}}function applyReplacement(str,replacementHash,nestedKey){return f.each(replacementHash,function(key,value){typeof value=="object"?str=applyReplacement(str,value,key):str=str.replace(new RegExp([o.interpolationPrefix,nestedKey?nestedKey+"."+key:key,o.interpolationSuffix].join(""),"g"),value)}),str}function applyReuse(translated,options){while(translated.indexOf(o.reusePrefix)!=-1){replacementCounter++;if(replacementCounter>o.maxRecursion)break;var index_of_opening=translated.indexOf(o.reusePrefix),index_of_end_of_closing=translated.indexOf(o.reuseSuffix,index_of_opening)+o.reuseSuffix.length,token=translated.substring(index_of_opening,index_of_end_of_closing),token_sans_symbols=token.replace(o.reusePrefix,"").replace(o.reuseSuffix,""),translated_token=_translate(token_sans_symbols,options);translated=translated.replace(token,translated_token)}return translated}function hasContext(options){return options.context&&typeof options.context=="string"}function needsPlural(options){return options.count!==undefined&&typeof options.count!="string"&&options.count!==1}function translate(key,options){return replacementCounter=0,_translate(key,options)}function _translate(key,options){options=options||{};var optionsSansCount,translated,notfound=options.defaultValue||key;if(!resStore)return notfound;var ns=o.ns.defaultNs;if(key.indexOf(o.nsseparator)>-1){var parts=key.split(o.nsseparator);ns=parts[0],key=parts[1]}if(hasContext(options)){optionsSansCount=f.extend({},options),delete optionsSansCount.context,optionsSansCount.defaultValue=o.contextNotFound;var contextKey=key+"_"+options.context;translated=translate(contextKey,optionsSansCount);if(translated!=o.contextNotFound)return applyReplacement(translated,{context:options.context})}if(needsPlural(options)){optionsSansCount=f.extend({},options),delete optionsSansCount.count,optionsSansCount.defaultValue=o.pluralNotFound;var pluralKey=key+o.pluralSuffix,pluralExtension=pluralExtensions.get(currentLng,options.count);pluralExtension!=="other"&&(pluralKey=pluralKey+"_"+pluralExtension),translated=translate(pluralKey,optionsSansCount);if(translated!=o.pluralNotFound)return applyReplacement(translated,{count:options.count})}var found,keys=key.split(o.keyseparator);for(var i=0,len=languages.length;i<len;i++){if(found)break;var l=languages[i],x=0,value=resStore[l][ns];while(keys[x])value=value&&value[keys[x]],x++;value&&(typeof value!="string"&&(value="key '"+ns+":"+key+" ("+l+")' "+"returned a object instead of string.",f.log(value)),value=applyReplacement(value,options),value=applyReuse(value,options),found=value)}return!found&&o.sendMissing&&sync.postMissing(ns,key,notfound),found?found:notfound}function detectLanguage(){var detectedLng,qsParm=[];(function(){var query=window.location.search.substring(1),parms=query.split("&");for(var i=0;i<parms.length;i++){var pos=parms[i].indexOf("=");if(pos>0){var key=parms[i].substring(0,pos),val=parms[i].substring(pos+1);qsParm[key]=val}}})(),qsParm.setLng&&(detectedLng=qsParm.setLng,f.cookie.create("i18next",detectedLng));if(!detectedLng){var c=f.cookie.read("i18next");c&&(detectedLng=c)}return!detectedLng&&navigator&&(detectedLng=navigator.language?navigator.language:navigator.userLanguage),detectedLng||(detectedLng=o.fallbackLng),detectedLng}function lng(){return currentLng}Array.prototype.indexOf||(Array.prototype.indexOf=function(searchElement){"use strict";if(this==null)throw new TypeError;var t=Object(this),len=t.length>>>0;if(len===0)return-1;var n=0;arguments.length>0&&(n=Number(arguments[1]),n!=n?n=0:n!=0&&n!=Infinity&&n!=-Infinity&&(n=(n>0||-1)*Math.floor(Math.abs(n))));if(n>=len)return-1;var k=n>=0?n:Math.max(len-Math.abs(n),0);for(;k<len;k++)if(k in t&&t[k]===searchElement)return k;return-1});var root=this,$=root.jQuery,i18n={};typeof module!="undefined"&&module.exports?module.exports=i18n:$?$.i18n=$.i18n||i18n:root.i18n=root.i18n||i18n;var o={lng:undefined,lowerCaseLng:!1,fallbackLng:"dev",ns:"translation",nsseparator:":",keyseparator:".",debug:!1,resGetPath:"locales/__lng__/__ns__.json",resPostPath:"locales/add/__lng__/__ns__",resStore:undefined,useLocalStorage:!0,dynamicLoad:!1,sendMissing:!1,interpolationPrefix:"__",interpolationSuffix:"__",reusePrefix:"$t(",reuseSuffix:")",pluralSuffix:"_plural",pluralNotFound:["plural_not_found",Math.random()].join(""),contextNotFound:["context_not_found",Math.random()].join(""),setJqueryExt:!0},f={extend:$?$.extend:undefined,each:$?$.each:undefined,ajax:$?$.ajax:undefined,detectLanguage:detectLanguage,log:function(str){o.debug&&console.log(str)},cookie:{create:function(name,value,minutes){var expires;if(minutes){var date=new Date;date.setTime(date.getTime()+minutes*60*1e3),expires="; expires="+date.toGMTString()}else expires="";document.cookie=name+"="+value+expires+"; path=/"},read:function(name){var nameEQ=name+"=",ca=document.cookie.split(";");for(var i=0;i<ca.length;i++){var c=ca[i];while(c.charAt(0)==" ")c=c.substring(1,c.length);if(c.indexOf(nameEQ)===0)return c.substring(nameEQ.length,c.length)}return null},remove:function(name){this.create(name,"",-1)}}},resStore,currentLng,replacementCounter=0,languages=[],sync={load:function(lngs,options,cb){options.useLocalStorage?sync._loadLocal(lngs,function(err,store){var missingLngs=[];for(var i=0,len=lngs.length;i<len;i++)store[lngs[i]]||missingLngs.push(lngs[i]);missingLngs.length>0?sync._fetch(missingLngs,options,function(err,fetched){f.extend(store,fetched),sync._storeLocal(fetched),cb(null,store)}):cb(null,store)}):sync._fetch(lngs,options,function(err,store){cb(null,store)})},_loadLocal:function(lngs,cb){var store={};if(window.localStorage){var todo=lngs.length;f.each(lngs,function(key,lng){var local=window.localStorage.getItem("res_"+lng);local&&(store[lng]=JSON.parse(local)),todo--,todo===0&&cb(null,store)})}},_storeLocal:function(store){if(window.localStorage)for(var m in store)window.localStorage.setItem("res_"+m,JSON.stringify(store[m]));return},_fetch:function(lngs,options,cb){var ns=options.ns,store={};if(!options.dynamicLoad){var todo=ns.namespaces.length*lngs.length,errors;f.each(ns.namespaces,function(nsIndex,nsValue){f.each(lngs,function(lngIndex,lngValue){sync._fetchOne(lngValue,nsValue,function(err,data){err&&(errors=errors||[],errors.push(err)),store[lngValue]=store[lngValue]||{},store[lngValue][nsValue]=data,todo--,todo===0&&cb(errors,store)})})})}else{var url=applyReplacement(o.resGetPath,{lng:lngs.join("+"),ns:ns.namespaces.join("+")});f.ajax({url:url,success:function(data,status,xhr){f.log("loaded: "+url),cb(null,data)},error:function(xhr,status,error){f.log("failed loading: "+url),cb("failed loading resource.json error: "+error)},dataType:"json"})}},_fetchOne:function(lng,ns,done){var url=applyReplacement(o.resGetPath,{lng:lng,ns:ns});f.ajax({url:url,success:function(data,status,xhr){f.log("loaded: "+url),done(null,data)},error:function(xhr,status,error){f.log("failed loading: "+url),done(error,{})},dataType:"json"})},postMissing:function(ns,key,defaultValue){var payload={};payload[key]=defaultValue;var url=applyReplacement(o.resPostPath,{lng:o.fallbackLng,ns:ns});f.ajax({url:url,type:"POST",data:payload,success:function(data,status,xhr){f.log("posted missing key '"+key+"' to: "+url),resStore[o.fallbackLng][ns][key]=defaultValue},error:function(xhr,status,error){f.log("failed posting missing key '"+key+"' to: "+url)},dataType:"json"})}},pluralExtensions={rules:{sl:function(n){return n%100===1?"one":n%100===2?"two":n%100===3||n%100===4?"few":"other"},ar:function(n){return n===0?"zero":n===1?"one":n===2?"two":n%100>=3&&n%100<=10?"few":n%100>=11&&n%100<=99?"many":"other"}},addRule:function(lng,fc){pluralExtensions.rules[lng]=fc},get:function(lng,count){function getResult(l,c){return pluralExtensions.rules[l]?pluralExtensions.rules[l](c):c===1?"one":"other"}var parts=lng.split("-");return parts.length===2?getResult(parts[0],count):getResult(lng,count)}};i18n.init=init,i18n.setLng=setLng,i18n.t=translate,i18n.translate=translate,i18n.detectLanguage=f.detectLanguage,i18n.pluralExtensions=pluralExtensions,i18n.sync=sync,i18n.functions=f,i18n.lng=lng,i18n.options=o})()
View
4 test/test.js
@@ -173,7 +173,8 @@ asyncTest("extended functions", function() {
nesting3: '3',
pluralTest: 'no plural',
pluralTest_plural: 'plural',
- interpolationTest: 'added __toAdd__'
+ interpolationTest: 'added __toAdd__',
+ interpolationTest2: 'added __toAdd__ __toAdd__ twice'
}
}
}
@@ -183,6 +184,7 @@ asyncTest("extended functions", function() {
equals(t('pluralTest', {count: 1}), 'no plural', 'call plural with count = 1');
equals(t('pluralTest', {count: 2}), 'plural', 'call plural with count = 2');
equals(t('interpolationTest', {toAdd: 'something'}), 'added something', 'insert variable into resource');
+ equals(t('interpolationTest2', {toAdd: 'something'}), 'added something something twice', 'insert variable into resource');
start();
});
Please sign in to comment.
Something went wrong with that request. Please try again.