From 2ad59839a56c36383066959e787a01069a95859d Mon Sep 17 00:00:00 2001 From: printempw Date: Thu, 15 Sep 2016 11:04:42 +0800 Subject: [PATCH] add JavaScript i18n files --- resources/lang/zh-CN/locale.js | 40 +++++++++++++++ resources/src/js/auth.js | 67 ++++++++++++------------ resources/src/js/general.js | 44 ++++++++++++++++ resources/src/js/utils.js | 94 +++++++++++++++++++++------------- 4 files changed, 176 insertions(+), 69 deletions(-) create mode 100644 resources/lang/zh-CN/locale.js create mode 100644 resources/src/js/general.js diff --git a/resources/lang/zh-CN/locale.js b/resources/lang/zh-CN/locale.js new file mode 100644 index 00000000..f6c16115 --- /dev/null +++ b/resources/lang/zh-CN/locale.js @@ -0,0 +1,40 @@ +/*! + * Blessing Skin Chinese Translations + * + * @see https://github.com/printempw/blessing-skin-server + * @author printempw + * + * NOTE: this file must be saved in UTF-8 encoding. + */ + +(function ($) { + "use strict"; + + $.locales['zh-CN'] = { + auth: { + emptyIdentification: '你还没有填写邮箱/角色名哦', + emptyPassword: '密码要好好填哦', + emptyCaptcha: '你还没有填写验证码哦', + login: '登录', + loggingIn: '登录中', + tooManyFails: '你尝试的次数太多啦,请输入验证码', + emptyEmail: '你还没有填写邮箱哦', + invalidEmail: '邮箱格式不正确!', + invalidPassword: '无效的密码。密码长度应该大于 8 并小于 16。', + emptyConfirmPwd: '确认密码不能为空', + invalidConfirmPwd: '密码和确认的密码不一样诶?', + emptyNickname: '你还没有填写昵称哦', + register: '注册', + registering: '注册中', + send: '发送', + sending: '发送中', + reset: '重置', + resetting: '重置中' + }, + general: { + confirmLogout: '确定要登出吗?', + confirm: '确定', + cancel: '取消' + } + }; +})(window.jQuery); diff --git a/resources/src/js/auth.js b/resources/src/js/auth.js index 4f3c935e..d9f492ed 100644 --- a/resources/src/js/auth.js +++ b/resources/src/js/auth.js @@ -2,7 +2,7 @@ * @Author: printempw * @Date: 2016-07-17 10:54:22 * @Last Modified by: printempw - * @Last Modified time: 2016-09-11 15:26:09 + * @Last Modified time: 2016-09-15 10:09:52 */ 'use strict'; @@ -34,18 +34,18 @@ $('#login-button').click(function() { data.keep = $('#keep').prop('checked') ? true : false; if (email_or_uname == "") { - showMsg('你还没有填写邮箱/角色名哦'); + showMsg(trans('auth.emptyIdentification')); $('#email_or_username').focus(); // check valid email address } else if (data.password == "") { - showMsg('密码要好好填哦'); + showMsg(trans('auth.emptyPassword')); $('#password').focus(); } else { // if captcha form is shown if ($('#captcha-form').css('display') == "block") { data.captcha = $("#captcha").val(); if (data.captcha == "") { - showMsg('你还没有填写验证码哦'); + showMsg(trans('auth.emptyCaptcha')); $('#captcha').focus(); return false; } @@ -57,7 +57,7 @@ $('#login-button').click(function() { dataType: "json", data: data, beforeSend: function() { - $('#login-button').html(' 登录中').prop('disabled', 'disabled'); + $('#login-button').html(' '+trans('auth.loggingIn')).prop('disabled', 'disabled'); }, success: function(json) { if (json.errno == 0) { @@ -72,7 +72,7 @@ $('#login-button').click(function() { if ($('#captcha-form').css('display') == "none") { swal({ type: 'error', - html: '你尝试的次数太多啦,请输入验证码' + html: trans('auth.tooManyFails') }); } @@ -82,12 +82,12 @@ $('#login-button').click(function() { freshCaptcha(); showMsg(json.msg, 'warning'); - $('#login-button').html('登录').prop('disabled', ''); + $('#login-button').html(trans('auth.login')).prop('disabled', ''); } }, error: function(json) { showAjaxError(json); - $('#login-button').html('登录').prop('disabled', ''); + $('#login-button').html(trans('auth.login')).prop('disabled', ''); } }); } @@ -105,27 +105,27 @@ $('#register-button').click(function() { // check valid email address if (email == "") { - showMsg('你还没有填写邮箱哦'); + showMsg(trans('auth.emptyEmail')); $('#email').focus(); } else if (!/\S+@\S+\.\S+/.test(email)) { - showMsg('邮箱格式不正确!', 'warning'); + showMsg(trans('auth.invalidEmail'), 'warning'); } else if (password == "") { - showMsg('密码要好好填哦'); + showMsg(trans('auth.emptyPassword')); $('#password').focus(); } else if (password.length < 8 || password.length > 16) { - showMsg('无效的密码。密码长度应该大于 8 并小于 16。', 'warning'); + showMsg(trans('auth.invalidPassword'), 'warning'); $('#password').focus(); } else if ($('#confirm-pwd').val() == "") { - showMsg('确认密码不能为空'); + showMsg(trans('auth.emptyConfirmPwd')); $('#confirm-pwd').focus(); } else if (password != $('#confirm-pwd').val()) { - showMsg('密码和确认的密码不一样诶?', 'warning'); + showMsg(trans('auth.invalidConfirmPwd'), 'warning'); $('#confirm-pwd').focus(); } else if (nickname == "") { - showMsg('你还没有填写昵称哦'); + showMsg(trans('auth.emptyNickname')); $('#nickname').focus(); } else if (captcha == "") { - showMsg('你还没有填写验证码哦'); + showMsg(trans('auth.emptyCaptcha')); $('#captcha').focus(); } else { @@ -135,7 +135,7 @@ $('#register-button').click(function() { dataType: "json", data: { 'email': email, 'password': password, 'nickname': nickname, 'captcha': captcha }, beforeSend: function() { - $('#register-button').html(' 注册中').prop('disabled', 'disabled'); + $('#register-button').html(' '+trans('auth.registering')).prop('disabled', 'disabled'); }, success: function(json) { if (json.errno == 0) { @@ -147,12 +147,12 @@ $('#register-button').click(function() { } else { showMsg(json.msg, 'warning'); freshCaptcha(); - $('#register-button').html('注册').prop('disabled', ''); + $('#register-button').html(trans('auth.register')).prop('disabled', ''); } }, error: function(json) { showAjaxError(json); - $('#register-button').html('注册').prop('disabled', ''); + $('#register-button').html(trans('auth.register')).prop('disabled', ''); } }); } @@ -167,12 +167,12 @@ $('#forgot-button').click(function() { // check valid email address if (email == "") { - showMsg('你还没有填写邮箱哦'); + showMsg(trans('auth.emptyEmail')); $('#email').focus(); } else if (!/\S+@\S+\.\S+/.test(email)) { - showMsg('邮箱格式不正确!', 'warning'); + showMsg(trans('auth.invalidEmail'), 'warning'); } else if (captcha == "") { - showMsg('你还没有填写验证码哦'); + showMsg(trans('auth.emptyCaptcha')); $('#captcha').focus(); } else { @@ -182,21 +182,21 @@ $('#forgot-button').click(function() { dataType: "json", data: { 'email': email, 'captcha': captcha }, beforeSend: function() { - $('#forgot-button').html(' 发送中').prop('disabled', 'disabled'); + $('#forgot-button').html(' '+trans('auth.sending')).prop('disabled', 'disabled'); }, success: function(json) { if (json.errno == 0) { showMsg(json.msg, 'success'); - $('#forgot-button').html('发送').prop('disabled', 'disabled'); + $('#forgot-button').html(trans('auth.send')).prop('disabled', 'disabled'); } else { showMsg(json.msg, 'warning'); freshCaptcha(); - $('#forgot-button').html('发送').prop('disabled', ''); + $('#forgot-button').html(trans('auth.send')).prop('disabled', ''); } }, error: function(json) { showAjaxError(json); - $('#forgot-button').html('发送').prop('disabled', ''); + $('#forgot-button').html(trans('auth.send')).prop('disabled', ''); } }); } @@ -209,13 +209,16 @@ $('#reset-button').click(function() { var password = $('#password').val(); if (password == "") { - showMsg('密码要好好填哦'); + showMsg(trans('auth.emptyPassword')); $('#password').focus(); } else if (password.length < 8 || password.length > 16) { - showMsg('无效的密码。密码长度应该大于 8 并小于 16。', 'warning'); + showMsg(trans('auth.invalidPassword'), 'warning'); $('#password').focus(); } else if ($('#confirm-pwd').val() == "") { - showMsg('确认密码不能为空'); + showMsg(trans('auth.emptyConfirmPwd')); + $('#confirm-pwd').focus(); + } else if (password != $('#confirm-pwd').val()) { + showMsg(trans('auth.invalidConfirmPwd'), 'warning'); $('#confirm-pwd').focus(); } else { @@ -225,7 +228,7 @@ $('#reset-button').click(function() { dataType: "json", data: { 'uid': uid, 'password': password }, beforeSend: function() { - $('#reset-button').html(' 重置中').prop('disabled', 'disabled'); + $('#reset-button').html(' '+trans('auth.resetting')).prop('disabled', 'disabled'); }, success: function(json) { if (json.errno == 0) { @@ -237,12 +240,12 @@ $('#reset-button').click(function() { }); } else { showMsg(json.msg, 'warning'); - $('#reset-button').html('重置').prop('disabled', ''); + $('#reset-button').html(trans('auth.reset')).prop('disabled', ''); } }, error: function(json) { showAjaxError(json); - $('#reset-button').html('重置').prop('disabled', ''); + $('#reset-button').html(trans('auth.reset')).prop('disabled', ''); } }); } diff --git a/resources/src/js/general.js b/resources/src/js/general.js new file mode 100644 index 00000000..71b7bf49 --- /dev/null +++ b/resources/src/js/general.js @@ -0,0 +1,44 @@ +/* +* @Author: printempw +* @Date: 2016-09-15 10:39:41 +* @Last Modified by: printempw +* @Last Modified time: 2016-09-15 10:43:32 +*/ + +'use strict'; + +function logout(with_out_confirm, callback) { + if (!with_out_confirm) { + swal({ + text: trans('general.confirmLogout'), + type: 'warning', + showCancelButton: true, + confirmButtonText: trans('general.confirm'), + cancelButtonText: trans('general.cancel') + }).then(function() { + do_logout(function(json) { + swal({ + type: 'success', + html: json.msg + }); + window.setTimeout('window.location = "../"', 1000); + }); + }); + } else { + do_logout(function(json) { + if (callback) callback(json); + }); + } +} + +function do_logout(callback) { + $.ajax({ + type: "POST", + url: "../auth/logout", + dataType: "json", + success: function(json) { + if (callback) callback(json); + }, + error: showAjaxError + }); +} diff --git a/resources/src/js/utils.js b/resources/src/js/utils.js index ca18faa2..6a583c6b 100644 --- a/resources/src/js/utils.js +++ b/resources/src/js/utils.js @@ -2,9 +2,65 @@ * @Author: printempw * @Date: 2016-07-16 09:02:32 * @Last Modified by: printempw - * @Last Modified time: 2016-09-10 17:10:08 + * @Last Modified time: 2016-09-15 10:39:49 */ +$.locales = {}; + +var locale = {}; + +function isEmpty(obj) { + + // null and undefined are "empty" + if (obj == null) return true; + + // Assume if it has a length property with a non-zero value + // that that property is correct. + if (obj.length > 0) return false; + if (obj.length === 0) return true; + + // If it isn't an object at this point + // it is empty, but it can't be anything *but* empty + // Is it empty? Depends on your application. + if (typeof obj !== "object") return true; + + // Otherwise, does it have any properties of its own? + // Note that this doesn't handle + // toString and valueOf enumeration bugs in IE < 9 + for (var key in obj) { + if (hasOwnProperty.call(obj, key)) return false; + } + + return true; +} + +function loadLocales() { + for (lang in $.locales) { + if (!isEmpty($.locales[lang])) { + locale = $.locales[lang] || {}; + } + } +} + +function trans(key) { + if (isEmpty(locale)) { + loadLocales(); + } + + var segments = key.split('.'); + var temp = locale || {}; + + for (i in segments) { + if (isEmpty(temp[segments[i]])) { + return key; + } else { + temp = temp[segments[i]]; + } + } + + return temp; +} + function showModal(msg, title, type, callback) { title = title === undefined ? "Messgae" : title; type = type === undefined ? "default" : type; @@ -39,39 +95,3 @@ function getQueryString(key) { return result[1]; } } - -function logout(with_out_confirm, callback) { - if (!with_out_confirm) { - swal({ - text: '确定要登出吗?', - type: 'warning', - showCancelButton: true, - confirmButtonText: '确定', - cancelButtonText: '取消' - }).then(function() { - do_logout(function(json) { - swal({ - type: 'success', - html: json.msg - }); - window.setTimeout('window.location = "../"', 1000); - }); - }); - } else { - do_logout(function(json) { - if (callback) callback(json); - }); - } -} - -function do_logout(callback) { - $.ajax({ - type: "POST", - url: "../auth/logout", - dataType: "json", - success: function(json) { - if (callback) callback(json); - }, - error: showAjaxError - }); -}