diff --git a/bower.json b/bower.json new file mode 100644 index 0000000..eeb27d0 --- /dev/null +++ b/bower.json @@ -0,0 +1,36 @@ +{ + "name": "jquery-localize", + "version": "0.1.2", + "license": "MIT", + "description": "A jQuery plugin that makes it easy to i18n your static web site.", + "keywords": [ + "jquery", + "localize", + "i18n" + ], + "homepage": "https://github.com/Haixing-Hu/jquery-localize", + "authors": [ + { + "name": "coderifous", + "email": "jim@thegarvin.com" + } + ], + "repository": { + "type": "git", + "url": "git@github.com:Haixing-Hu/jquery-localize.git" + }, + "main": [ + "dist/jquery.localize.js" + ], + "dependencies": { + "jquery": ">=1.9.0" + }, + "devDependencies": { + "qunit":">=1.11.0" + }, + "ignore": [ + "test", + "libs" + ] +} + diff --git a/dist/jquery.localize.js b/dist/jquery.localize.js index e33ce7b..e65e89d 100644 --- a/dist/jquery.localize.js +++ b/dist/jquery.localize.js @@ -64,6 +64,9 @@ http://keith-wood.name/localisation.html return loadLanguage(pkg, lang, level + 1); }; errorFunc = function() { + if (level < 3) { + loadLanguage(pkg, lang, level + 1); + } if (options.fallback && options.fallback !== lang) { return loadLanguage(pkg, options.fallback); } diff --git a/dist/jquery.localize.min.js b/dist/jquery.localize.min.js index 73f1334..ca7476f 100644 --- a/dist/jquery.localize.min.js +++ b/dist/jquery.localize.min.js @@ -1,4 +1,4 @@ -/*! Localize - v0.1.0 - 2014-03-04 +/*! Localize - v0.1.1 - 2015-03-08 * https://github.com/coderifous/jquery-localize - * Copyright (c) 2014 coderifous; Licensed MIT */ -!function(a){var b;return b=function(a){return a=a.replace(/_/,"-").toLowerCase(),a.length>3&&(a=a.substring(0,3)+a.substring(3).toUpperCase()),a},a.defaultLanguage=b(navigator.language||navigator.userLanguage),a.localize=function(c,d){var e,f,g,h,i,j,k,l,m,n,o,p,q,r,s,t,u;return null==d&&(d={}),u=this,g={},f=d.fileExtension||"json",j=function(a,b,c){var e;switch(null==c&&(c=1),c){case 1:return g={},d.loadBase?(e=a+("."+f),h(e,a,b,c)):j(a,b,2);case 2:if(b.length>=2)return e=""+a+"-"+b.substring(0,2)+"."+f,h(e,a,b,c);break;case 3:if(b.length>=5)return e=""+a+"-"+b.substring(0,5)+"."+f,h(e,a,b,c)}},h=function(b,c,e,f){var h,i,k;return null!=d.pathPrefix&&(b=""+d.pathPrefix+"/"+b),k=function(b){return a.extend(g,b),p(g),j(c,e,f+1)},i=function(){return d.fallback&&d.fallback!==e?j(c,d.fallback):void 0},h={url:b,dataType:"json",async:!1,timeout:null!=d.timeout?d.timeout:500,success:k,error:i},"file:"===window.location.protocol&&(h.error=function(b){return k(a.parseJSON(b.responseText))}),a.ajax(h)},p=function(a){return null!=d.callback?d.callback(a,e):e(a)},e=function(b){return a.localize.data[c]=b,u.each(function(){var c,d,e;return c=a(this),d=c.data("localize"),d||(d=c.attr("rel").match(/localize\[(.*?)\]/)[1]),e=t(d,b),null!=e?k(c,d,e):void 0})},k=function(b,c,d){return b.is("input")?n(b,c,d):b.is("img")?m(b,c,d):b.is("optgroup")?o(b,c,d):a.isPlainObject(d)||b.html(d),a.isPlainObject(d)?l(b,d):void 0},n=function(b,c,d){var e;return e=a.isPlainObject(d)?d.value:d,b.is("[placeholder]")?b.attr("placeholder",e):b.val(e)},l=function(a,b){return r(a,"title",b),s(a,"text",b)},o=function(a,b,c){return a.attr("label",c)},m=function(a,b,c){return r(a,"alt",c),r(a,"src",c)},t=function(a,b){var c,d,e,f;for(c=a.split(/\./),d=b,e=0,f=c.length;f>e;e++)a=c[e],d=null!=d?d[a]:null;return d},r=function(a,b,c){return c=t(b,c),null!=c?a.attr(b,c):void 0},s=function(a,b,c){return c=t(b,c),null!=c?a.text(c):void 0},q=function(a){var b;return"string"==typeof a?"^"+a+"$":null!=a.length?function(){var c,d,e;for(e=[],c=0,d=a.length;d>c;c++)b=a[c],e.push(q(b));return e}().join("|"):a},i=b(d.language?d.language:a.defaultLanguage),d.skipLanguage&&i.match(q(d.skipLanguage))||j(c,i,1),u},a.fn.localize=a.localize,a.localize.data={}}(jQuery); \ No newline at end of file + * Copyright (c) 2015 coderifous; Licensed MIT */ +!function(a){var b;return b=function(a){return a=a.replace(/_/,"-").toLowerCase(),a.length>3&&(a=a.substring(0,3)+a.substring(3).toUpperCase()),a},a.defaultLanguage=b(navigator.language||navigator.userLanguage),a.localize=function(c,d){var e,f,g,h,i,j,k,l,m,n,o,p,q,r,s,t,u;return null==d&&(d={}),u=this,g={},f=d.fileExtension||"json",j=function(a,b,c){var e;switch(null==c&&(c=1),c){case 1:return g={},d.loadBase?(e=a+("."+f),h(e,a,b,c)):j(a,b,2);case 2:if(b.length>=2)return e=""+a+"-"+b.substring(0,2)+"."+f,h(e,a,b,c);break;case 3:if(b.length>=5)return e=""+a+"-"+b.substring(0,5)+"."+f,h(e,a,b,c)}},h=function(b,c,e,f){var h,i,k;return null!=d.pathPrefix&&(b=""+d.pathPrefix+"/"+b),k=function(b){return a.extend(g,b),p(g),j(c,e,f+1)},i=function(){return 3>f&&j(c,e,f+1),d.fallback&&d.fallback!==e?j(c,d.fallback):void 0},h={url:b,dataType:"json",async:!1,timeout:null!=d.timeout?d.timeout:500,success:k,error:i},"file:"===window.location.protocol&&(h.error=function(b){return k(a.parseJSON(b.responseText))}),a.ajax(h)},p=function(a){return null!=d.callback?d.callback(a,e):e(a)},e=function(b){return a.localize.data[c]=b,u.each(function(){var c,d,e;return c=a(this),d=c.data("localize"),d||(d=c.attr("rel").match(/localize\[(.*?)\]/)[1]),e=t(d,b),null!=e?k(c,d,e):void 0})},k=function(b,c,d){return b.is("input")?n(b,c,d):b.is("img")?m(b,c,d):b.is("optgroup")?o(b,c,d):a.isPlainObject(d)||b.html(d),a.isPlainObject(d)?l(b,d):void 0},n=function(b,c,d){var e;return e=a.isPlainObject(d)?d.value:d,b.is("[placeholder]")?b.attr("placeholder",e):b.val(e)},l=function(a,b){return r(a,"title",b),s(a,"text",b)},o=function(a,b,c){return a.attr("label",c)},m=function(a,b,c){return r(a,"alt",c),r(a,"src",c)},t=function(a,b){var c,d,e,f;for(c=a.split(/\./),d=b,e=0,f=c.length;f>e;e++)a=c[e],d=null!=d?d[a]:null;return d},r=function(a,b,c){return c=t(b,c),null!=c?a.attr(b,c):void 0},s=function(a,b,c){return c=t(b,c),null!=c?a.text(c):void 0},q=function(a){var b;return"string"==typeof a?"^"+a+"$":null!=a.length?function(){var c,d,e;for(e=[],c=0,d=a.length;d>c;c++)b=a[c],e.push(q(b));return e}().join("|"):a},i=b(d.language?d.language:a.defaultLanguage),d.skipLanguage&&i.match(q(d.skipLanguage))||j(c,i,1),u},a.fn.localize=a.localize,a.localize.data={}}(jQuery); \ No newline at end of file diff --git a/examples/language_country_code_directly.html b/examples/language_country_code_directly.html new file mode 100644 index 0000000..459549a --- /dev/null +++ b/examples/language_country_code_directly.html @@ -0,0 +1,24 @@ + + + + +
+ +It failed :(
+ + + + diff --git a/localize.jquery.json b/localize.jquery.json index 3302442..b2b9e39 100644 --- a/localize.jquery.json +++ b/localize.jquery.json @@ -2,7 +2,7 @@ "name": "jquery.localize", "title": "Localize", "description": "A jQuery plugin that makes it easy to i18n your static web site.", - "version": "0.1.0", + "version": "0.1.1", "homepage": "https://github.com/coderifous/jquery-localize", "author": { "name": "coderifous", diff --git a/package.json b/package.json index ccbe2a6..1955d0c 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "jquery-localize", - "version": "0.0.0-ignored", + "version": "0.1.1", "engines": { "node": ">= 0.8.0" }, diff --git a/src/jquery.localize.coffee b/src/jquery.localize.coffee index 723e9a3..106f7b6 100644 --- a/src/jquery.localize.coffee +++ b/src/jquery.localize.coffee @@ -49,6 +49,9 @@ do ($ = jQuery) -> notifyDelegateLanguageLoaded(intermediateLangData) loadLanguage(pkg, lang, level + 1) errorFunc = -> + # by Haixing Hu: load the next level even if the higher level failed. + if level < 3 + loadLanguage(pkg, lang, level + 1) if options.fallback && options.fallback != lang loadLanguage(pkg, options.fallback) ajaxOptions = diff --git a/test/lang/test-zh-CN.json b/test/lang/test-zh-CN.json new file mode 100644 index 0000000..91b826e --- /dev/null +++ b/test/lang/test-zh-CN.json @@ -0,0 +1,3 @@ +{ + "message": "language-country code success" +} \ No newline at end of file diff --git a/test/localize_test.coffee b/test/localize_test.coffee index 62cfb1d..435df5f 100644 --- a/test/localize_test.coffee +++ b/test/localize_test.coffee @@ -138,6 +138,12 @@ do ($ = jQuery) -> t.localize("test", opts) equal t.text(), "country code success" + test "load language-country code json file directly", -> + opts = language: "zh-CN", pathPrefix: "lang" + t = localizableTagWithRel("p", "message", text: "language-country code fail") + t.localize("test", opts) + equal t.text(), "language-country code success" + module "Language optimization" test "skipping language using string match", -> diff --git a/test/localize_test.js b/test/localize_test.js index 476f505..02ccfd5 100644 --- a/test/localize_test.js +++ b/test/localize_test.js @@ -215,6 +215,18 @@ t.localize("test", opts); return equal(t.text(), "country code success"); }); + test("load language-country code json file directly", function() { + var opts, t; + opts = { + language: "zh-CN", + pathPrefix: "lang" + }; + t = localizableTagWithRel("p", "message", { + text: "language-country code fail" + }); + t.localize("test", opts); + return equal(t.text(), "language-country code success"); + }); module("Language optimization"); test("skipping language using string match", function() { var opts, t;