This commit is contained in:
zhenjiang.dou 2016-12-23 16:35:16 +08:00
parent 997076faf7
commit 7cdd671d3d
6 changed files with 143 additions and 16 deletions

View file

@ -1,20 +1,25 @@
loadProperties();
function loadProperties() {
$.i18n.properties({
name: 'hupu-lang',
path: 'i18n/',
mode: 'map',
language: $.i18n.browserLang(),
callback: function () {
$("[data-locale]").each(function () {
$(this).html($.i18n.prop($(this).data("locale")));
});
}
});
}
jQuery.i18n.properties({
name: 'lang',
path: './locales/',
mode: 'both',
// language:'zh_CN',
checkAvailableLanguages: true,
async: true,
callback: function () {
$("[data-i18n]").each(function () {
var elem = $(this),
localizedValue = jQuery.i18n.map[elem.data("i18n")];
if (elem.is("input[type=text]") || elem.is("input[type=password]") || elem.is("input[type=email]")) {
elem.attr("placeholder", localizedValue);
} else if (elem.is("input[type=button]") || elem.is("input[type=submit]")) {
elem.attr("value", localizedValue);
} else {
elem.text(localizedValue);
}
});
}
});
function turnPage(url) {
$.ajax({
url: url,

61
js/jquery.i18n.properties.min.js vendored Normal file
View file

@ -0,0 +1,61 @@
/******************************************************************************
* jquery.i18n.properties
*
* Dual licensed under the GPL (http://dev.jquery.com/browser/trunk/jquery/GPL-LICENSE.txt) and
* MIT (http://dev.jquery.com/browser/trunk/jquery/MIT-LICENSE.txt) licenses.
*
* @version 1.2.2
* @url https://github.com/jquery-i18n-properties/jquery-i18n-properties
* @inspiration Localisation assistance for jQuery (http://keith-wood.name/localisation.html)
* by Keith Wood (kbwood{at}iinet.com.au) June 2007
*
*****************************************************************************/
!function($){function callbackIfComplete(e){e.async&&(e.filesLoaded+=1,e.filesLoaded===e.totalFiles&&e.callback&&e.callback())}function loadAndParseFile(e,a){$.ajax({url:e,async:a.async,cache:a.cache,dataType:"text",success:function(e,r){parseData(e,a.mode),callbackIfComplete(a)},error:function(r,t,n){console.log("Failed to download or parse "+e),callbackIfComplete(a)}})}function parseData(data,mode){for(var parsed="",parameters=data.split(/\n/),regPlaceHolder=/(\{\d+})/g,regRepPlaceHolder=/\{(\d+)}/g,unicodeRE=/(\\u.{4})/gi,i=0;i<parameters.length;i++)if(parameters[i]=parameters[i].replace(/^\s\s*/,"").replace(/\s\s*$/,""),parameters[i].length>0&&"#"!=parameters[i].match("^#")){var pair=parameters[i].split("=")
if(pair.length>0){for(var name=decodeURI(pair[0]).replace(/^\s\s*/,"").replace(/\s\s*$/,""),value=1==pair.length?"":pair[1];"\\"==value.match(/\\$/);)value=value.substring(0,value.length-1),value+=parameters[++i].replace(/\s\s*$/,"")
for(var s=2;s<pair.length;s++)value+="="+pair[s]
if(value=value.replace(/^\s\s*/,"").replace(/\s\s*$/,""),"map"==mode||"both"==mode){var unicodeMatches=value.match(unicodeRE)
if(unicodeMatches)for(var u=0;u<unicodeMatches.length;u++)value=value.replace(unicodeMatches[u],unescapeUnicode(unicodeMatches[u]))
$.i18n.map[name]=value}if("vars"==mode||"both"==mode)if(value=value.replace(/"/g,'\\"'),checkKeyNamespace(name),regPlaceHolder.test(value)){for(var parts=value.split(regPlaceHolder),first=!0,fnArgs="",usedArgs=[],p=0;p<parts.length;p++)!regPlaceHolder.test(parts[p])||0!=usedArgs.length&&-1!=usedArgs.indexOf(parts[p])||(first||(fnArgs+=","),fnArgs+=parts[p].replace(regRepPlaceHolder,"v$1"),usedArgs.push(parts[p]),first=!1)
parsed+=name+"=function("+fnArgs+"){"
var fnExpr='"'+value.replace(regRepPlaceHolder,'"+v$1+"')+'"'
parsed+="return "+fnExpr+";};"}else parsed+=name+'="'+value+'";'}}eval(parsed)}function checkKeyNamespace(key){var regDot=/\./
if(regDot.test(key))for(var fullname="",names=key.split(/\./),i=0;i<names.length;i++)i>0&&(fullname+="."),fullname+=names[i],eval("typeof "+fullname+' == "undefined"')&&eval(fullname+"={};")}function getFiles(e){return e&&e.constructor==Array?e:[e]}function unescapeUnicode(e){var a=[],r=parseInt(e.substr(2),16)
r>=0&&r<Math.pow(2,16)&&a.push(r)
for(var t="",n=0;n<a.length;++n)t+=String.fromCharCode(a[n])
return t}$.i18n={},$.i18n.map={},$.i18n.properties=function(e){var a={name:"Messages",language:"",path:"",mode:"vars",cache:!1,encoding:"UTF-8",async:!1,checkAvailableLanguages:!1,callback:null}
e=$.extend(a,e),e.language=this.normaliseLanguageCode(e.language)
var r=function(a){e.totalFiles=0,e.filesLoaded=0
var r=getFiles(e.name)
if(e.async)for(var t=0,n=r.length;n>t;t++){e.totalFiles+=1
var s=e.language.substring(0,2)
if(0!=a.length&&-1==$.inArray(s,a)||(e.totalFiles+=1),e.language.length>=5){var l=e.language.substring(0,5)
0!=a.length&&-1==$.inArray(l,a)||(e.totalFiles+=1)}}for(var i=0,g=r.length;g>i;i++){loadAndParseFile(e.path+r[i]+".properties",e)
var s=e.language.substring(0,2)
if(0!=a.length&&-1==$.inArray(s,a)||loadAndParseFile(e.path+r[i]+"_"+s+".properties",e),e.language.length>=5){var l=e.language.substring(0,5)
0!=a.length&&-1==$.inArray(l,a)||loadAndParseFile(e.path+r[i]+"_"+l+".properties",e)}}e.callback&&!e.async&&e.callback()}
e.checkAvailableLanguages?$.ajax({url:e.path+"languages.json",async:e.async,cache:!1,success:function(e,a,t){r(e.languages||[])}}):r([])},$.i18n.prop=function(e){var a=$.i18n.map[e]
if(null==a)return"["+e+"]"
var r
2==arguments.length&&$.isArray(arguments[1])&&(r=arguments[1])
var t
if("string"==typeof a){for(t=0;-1!=(t=a.indexOf("\\",t));)a="t"==a.charAt(t+1)?a.substring(0,t)+" "+a.substring(t++ +2):"r"==a.charAt(t+1)?a.substring(0,t)+"\r"+a.substring(t++ +2):"n"==a.charAt(t+1)?a.substring(0,t)+"\n"+a.substring(t++ +2):"f"==a.charAt(t+1)?a.substring(0,t)+"\f"+a.substring(t++ +2):"\\"==a.charAt(t+1)?a.substring(0,t)+"\\"+a.substring(t++ +2):a.substring(0,t)+a.substring(t+1)
var n,s,l=[]
for(t=0;t<a.length;)if("'"==a.charAt(t))if(t==a.length-1)a=a.substring(0,t)
else if("'"==a.charAt(t+1))a=a.substring(0,t)+a.substring(++t)
else{for(n=t+2;-1!=(n=a.indexOf("'",n));){if(n==a.length-1||"'"!=a.charAt(n+1)){a=a.substring(0,t)+a.substring(t+1,n)+a.substring(n+1),t=n-1
break}a=a.substring(0,n)+a.substring(++n)}-1==n&&(a=a.substring(0,t)+a.substring(t+1))}else if("{"==a.charAt(t))if(n=a.indexOf("}",t+1),-1==n)t++
else if(s=parseInt(a.substring(t+1,n)),!isNaN(s)&&s>=0){var i=a.substring(0,t)
""!=i&&l.push(i),l.push(s),t=0,a=a.substring(n+1)}else t=n+1
else t++
""!=a&&l.push(a),a=l,$.i18n.map[e]=l}if(0==a.length)return""
if(1==a.length&&"string"==typeof a[0])return a[0]
var g=""
for(t=0;t<a.length;t++)g+="string"==typeof a[t]?a[t]:r&&a[t]<r.length?r[a[t]]:!r&&a[t]+1<arguments.length?arguments[a[t]+1]:"{"+a[t]+"}"
return g},$.i18n.normaliseLanguageCode=function(e){return(!e||e.length<2)&&(e=navigator.languages?navigator.languages[0]:navigator.language||navigator.userLanguage||"en"),e=e.toLowerCase(),e=e.replace(/-/,"_"),e.length>3&&(e=e.substring(0,3)+e.substring(3).toUpperCase()),e}
var cbSplit
cbSplit||(cbSplit=function(e,a,r){if("[object RegExp]"!==Object.prototype.toString.call(a))return"undefined"==typeof cbSplit._nativeSplit?e.split(a,r):cbSplit._nativeSplit.call(e,a,r)
var t,n,s,l,i=[],g=0,c=(a.ignoreCase?"i":"")+(a.multiline?"m":"")+(a.sticky?"y":""),a=new RegExp(a.source,c+"g")
if(e+="",cbSplit._compliantExecNpcg||(t=new RegExp("^"+a.source+"$(?!\\s)",c)),void 0===r||0>+r)r=1/0
else if(r=Math.floor(+r),!r)return[]
for(;(n=a.exec(e))&&(s=n.index+n[0].length,!(s>g&&(i.push(e.slice(g,n.index)),!cbSplit._compliantExecNpcg&&n.length>1&&n[0].replace(t,function(){for(var e=1;e<arguments.length-2;e++)void 0===arguments[e]&&(n[e]=void 0)}),n.length>1&&n.index<e.length&&Array.prototype.push.apply(i,n.slice(1)),l=n[0].length,g=s,i.length>=r)));)a.lastIndex===n.index&&a.lastIndex++
return g===e.length?!l&&a.test("")||i.push(""):i.push(e.slice(g)),i.length>r?i.slice(0,r):i},cbSplit._compliantExecNpcg=void 0===/()??/.exec("")[1],cbSplit._nativeSplit=String.prototype.split),String.prototype.split=function(e,a){return cbSplit(this,e,a)}}(jQuery)

26
locales/lang.properties Normal file
View file

@ -0,0 +1,26 @@
# This line is ignored by the plugin
Home = Home
tos = Terms of service
home = Home
production = Product
contact = Contact
contact_us = Contact us
login = Login
signup = Signup
legal_information = Legal Information
privacy_policy = Privacy Policy
tos = Terms of service
cancellation_refund = Cancellation & Refund
payment_methods = Payment Methods & Information
msg_hello = Hello
msg_world = World
msg_complex = Good morning {0}! \
Are you OK? \
This is a multi-line property :)
msg_url = http://localhost/webapp/servletName?solution={0}
msg_multi_placeholder = It\'s a {0} day... let\'s go {1}?
msg_multi_placeholder_corrected = It''s a {0} day... let''s go {1}?

View file

@ -0,0 +1,13 @@
Home = Home
tos = Terms of service
home = Home
production = Product
contact = Contact
contact_us = Contact us
login = Login
signup = Signup
legal_information = Legal Information
privacy_policy = Privacy Policy
tos = Terms of service
cancellation_refund = Cancellation & Refund
payment_methods = Payment Methods & Information

View file

@ -0,0 +1,11 @@
home = 主页
production = 产品
contact = 联系
contact_us = 联系我们
login = 登录
signup = 注册
legal_information = 法律信息
privacy_policy = 隐私政策
tos = 服务条款
cancellation_refund = 取消与退款
payment_methods = 支付方式

11
locales/languages.json Normal file
View file

@ -0,0 +1,11 @@
{
"languages": [
"en",
"zh_CN",
"zh_TW",
"en_GB",
"es_ES",
"pt_BR",
"sv_SE"
]
}