From 908ff7f59f73dd38a7cef173d328e405fffd1afd Mon Sep 17 00:00:00 2001 From: sight <26325820+Sight-wcg@users.noreply.github.com> Date: Thu, 22 May 2025 20:34:34 +0800 Subject: [PATCH] =?UTF-8?q?wip(i18n):=20laydate=20=E5=9B=BD=E9=99=85?= =?UTF-8?q?=E5=8C=96?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/layui.js | 161 ++++++++++++++++++++++++++++++++- src/modules/code.js | 12 +-- src/modules/colorpicker.js | 4 +- src/modules/dropdown.js | 2 +- src/modules/flow.js | 4 +- src/modules/form.js | 24 ++--- src/modules/laydate.js | 178 +++++++++++++++++++++++++------------ src/modules/layer.js | 28 +++--- src/modules/laypage.js | 18 ++-- src/modules/table.js | 22 ++--- src/modules/transfer.js | 8 +- src/modules/tree.js | 6 +- src/modules/treeTable.js | 6 +- src/modules/upload.js | 20 ++--- src/modules/util.js | 22 ++--- 15 files changed, 366 insertions(+), 149 deletions(-) diff --git a/src/layui.js b/src/layui.js index e9abf579..573a430f 100644 --- a/src/layui.js +++ b/src/layui.js @@ -11,11 +11,61 @@ var document = window.document; var location = window.location; + var zhCn = { + locale: 'zh-cn', + /** 未使用的字段为保留字段,将来可能会使用 */ + laydate: { + month: { + long: ['一月', '二月', '三月', '四月', '五月', '六月', '七月', '八月', '九月', '十月', '十一月', '十二月'], + short: ['一', '二', '三', '四', '五', '六', '七', '八', '九', '十', '十一', '十二'] + }, + week:{ + long: ['周日', '周一', '周二', '周三', '周四', '周五', '周六'], + short: ['日', '一', '二', '三', '四', '五', '六'] + }, + time: ['时', '分', '秒'], + selectTime: '选择时间', + startTime: '开始时间', + endTime: '结束时间', + selectDate: '选择日期', + tools: { + confirm: '确定', + clear: '清空', + now: '现在', + reset: '重置' + }, + timeout: { + date: '结束日期不能早于开始日期
请重新选择', + time: '结束时间不能早于开始时间
请重新选择' + }, + invalidDate: '不在有效日期或时间范围内', + formatError: ['日期格式不合法
必须遵循下述格式:
', '
已为你重置'], + preview: '当前选中的结果', + panelHeaderFormat: { + year: function(y){return y + ' 年'}, + month: function(m){return m + ' 月'}, + monthBeforeYear: false + }, + /** 面板中某些字符串拼接使用 */ + view: { + year: '年', + month: '月', + week: '周', + day: '天' + } + } + } // 基础配置 var config = { timeout: 10, // 符合规范的模块请求最长等待秒数 debug: false, // 是否开启调试模式 - version: false // 是否在模块请求时加入版本号参数(以更新模块缓存) + version: false, // 是否在模块请求时加入版本号参数(以更新模块缓存) + locale: 'zh-cn', // 设置全局配置的语言 + i18nMessages: { // 语言包,格式为:{locale: {namespace:{component:{...}}}} + 'zh-cn': { + lay: zhCn + } + } }; // 模块加载缓存信息 @@ -55,7 +105,13 @@ // 异常提示 var error = function(msg, type) { type = type || 'log'; - window.console && console[type] && console[type]('layui error hint: ' + msg); + msg = 'layui error hint: ' + msg + + if (window.console && console[type]) { + console[type](msg); + } else if (window.console && console.log) { + console.log(msg); + } }; // 内置模块 @@ -1029,6 +1085,107 @@ } }; + /** + * 根据给定的键从国际化消息中获取翻译后的内容 + * @overload + * @param {string} key 要翻译的键 + * @param {Record} opts 国际化消息对象,替换 {key} 形式的占位符 + * @returns {string} 翻译后的文本 + * @example + * ``` + * message: { + * hello: '{msg} world' + * } + * layui.$t('message.hello', {msg: 'Hello'}) + * ``` + * + * @overload + * @param {string} key 要翻译的键 + * @param {any[]} opts 国际化消息数组,替换 {0}, {1}... 形式的占位符 + * @returns {string} 翻译后的文本 + * @example + * ``` + * message: { + * hello: '{0} world' + * } + * layui.$t('message.hello', ['Hello']) + * ``` + * + * @overload + * @param {string} key 要翻译的键 + * @param {...*} args 国际化消息可变参数,替换 {0}, {1}... 形式的占位符 + * @returns {string} 翻译后的文本 + * @example + * ``` + * message: { + * hello: '{0} world' + * } + * layui.$t('message.hello', 'Hello') + * ``` + * + * @param {string} key + * @param {any[]} args + */ + Class.prototype.i18nTranslation = function(key){ + var that = this; + var args = arguments; + var locale = that.cache.locale; + var i18nMessage = that.cache.i18nMessages[locale]; + + if(!i18nMessage){ + error('Locale "' + locale + '" not found. Please add i18n messages for this locale first.', 'warn'); + } + + var result = get(i18nMessage, key, key); + + // 替换占位符 + if(typeof result === 'string'){ + if(args.length > 1){ + var opts = args[1]; + // 第二个参数为对象或数组,替换占位符 {key} 或 {0}, {1}... + if(typeof opts === 'object'){ + return result.replace(/\{(\w+)\}/g, function(match, key) { + return opts[key] !== undefined ? opts[key] : match; + }); + } + + // 处理可变参数,替换占位符 {0}, {1}... + return result.replace(/\{(\d+)\}/g, function(match, index) { + var arg = args[index + 1]; + return arg !== undefined ? arg : match; + }); + } + } + + return result; + } + + /** + * i18nTranslation 的别名 + */ + Class.prototype.$t = Class.prototype.i18nTranslation; + + /** + * 获取对象中的值,lodash _.get 简易版 + * @param {Record} obj + * @param {string} path + * @param {any} defaultValue + */ + function get(obj, path, defaultValue){ + // 'a[0].b.c' ==> ['a', '0', 'b', 'c'] + var casePath = path.replace(/\[(\d+)\]/g, '.$1').split('.'); + var result = obj; + + for(var i = 0; i < casePath.length; i++) { + result = result && result[casePath[i]]; + if(result === null || result === undefined){ + return defaultValue; + } + } + + return result; + } + // export layui window.layui = new Class(); })(window); diff --git a/src/modules/code.js b/src/modules/code.js index 744e5ca1..ea8009f2 100644 --- a/src/modules/code.js +++ b/src/modules/code.js @@ -201,7 +201,7 @@ layui.define(['lay', 'util', 'element', 'tabs', 'form'], function(exports){ var tools = { copy: { className: 'file-b', - title: ['复制代码'], + title: [layui.$t('复制代码')], event: function(obj){ var code = util.unescape(finalCode(options.code)); var hasOnCopy = typeof options.onCopy === 'function'; @@ -215,14 +215,14 @@ layui.define(['lay', 'util', 'element', 'tabs', 'form'], function(exports){ if(ret === false) return; } - layer.msg('已复制', {icon: 1}); + layer.msg(layui.$t('已复制'), {icon: 1}); }, error: function() { if(hasOnCopy){ var ret = options.onCopy(code, false); if(ret === false) return; } - layer.msg('复制失败', {icon: 2}); + layer.msg(layui.$t('复制失败'), {icon: 2}); } }); } @@ -277,7 +277,7 @@ layui.define(['lay', 'util', 'element', 'tabs', 'form'], function(exports){ $.extend(tools, { 'full': { className: 'screen-full', - title: ['最大化显示', '还原显示'], + title: [layui.$t('最大化显示'), layui.$t('还原显示')], event: function(obj){ var el = obj.elem; var elemView = el.closest('.'+ CONST.ELEM_PREVIEW); @@ -302,7 +302,7 @@ layui.define(['lay', 'util', 'element', 'tabs', 'form'], function(exports){ }, 'window': { className: 'release', - title: ['在新窗口预览'], + title: [layui.$t('在新窗口预览')], event: function(obj){ util.openWin({ content: finalCode(options.code) @@ -562,7 +562,7 @@ layui.define(['lay', 'util', 'element', 'tabs', 'form'], function(exports){ // 若开启复制,且未开启预览,则单独生成复制图标 if(options.copy && !options.preview){ var copyElem = $(['', - '', + layui.$t(''), ''].join('')); // 点击复制 diff --git a/src/modules/colorpicker.js b/src/modules/colorpicker.js index 215391d4..4cf10d09 100644 --- a/src/modules/colorpicker.js +++ b/src/modules/colorpicker.js @@ -285,8 +285,8 @@ layui.define(['jquery', 'lay'], function(exports) { ,'' ,'' ,'
' - ,'' - ,'' + ,layui.$t('') + ,layui.$t('') ,'' ,'
'].join('')) diff --git a/src/modules/dropdown.js b/src/modules/dropdown.js index f0053cd9..eb53559f 100644 --- a/src/modules/dropdown.js +++ b/src/modules/dropdown.js @@ -181,7 +181,7 @@ layui.define(['jquery', 'laytpl', 'lay', 'util'], function(exports) { if(options.data.length > 0 ){ eachItemView(elemUl, options.data) } else { - elemUl.html('
  • 暂无数据
  • '); + elemUl.html(layui.$t('
  • 暂无数据
  • ')); } return elemUl; }; diff --git a/src/modules/flow.js b/src/modules/flow.js index e5e4fff7..751d214f 100644 --- a/src/modules/flow.js +++ b/src/modules/flow.js @@ -20,8 +20,8 @@ layui.define('jquery', function(exports) { var scrollElem = $(options.scrollElem || document); // 滚动条所在元素 var threshold = 'mb' in options ? options.mb : 50; // 临界距离 var isAuto = 'isAuto' in options ? options.isAuto : true; // 否自动滚动加载 - var moreText = options.moreText || "加载更多"; // 手动加载时,加载更多按钮文案 - var end = options.end || '没有更多了'; // “末页”显示文案 + var moreText = options.moreText || layui.$t('加载更多'); // 手动加载时,加载更多按钮文案 + var end = options.end || layui.$t('没有更多了'); // “末页”显示文案 var direction = options.direction || 'bottom'; var isTop = direction === 'top'; diff --git a/src/modules/form.js b/src/modules/form.js index a16ee41f..ed3dcc28 100644 --- a/src/modules/form.js +++ b/src/modules/form.js @@ -31,42 +31,42 @@ layui.define(['lay', 'layer', 'util'], function(exports){ verify: { required: function(value) { if (!/[\S]+/.test(value) || value === undefined || value === null) { - return '必填项不能为空'; + return layui.$t('必填项不能为空'); } }, phone: function(value) { var EXP = /^1\d{10}$/; if (value && !EXP.test(value)) { - return '手机号格式不正确'; + return layui.$t('手机号格式不正确'); } }, email: function(value) { var EXP = /^([a-zA-Z0-9_\.\-])+\@(([a-zA-Z0-9\-])+\.)+([a-zA-Z0-9]{2,4})+$/; if (value && !EXP.test(value)) { - return '邮箱格式不正确'; + return layui.$t('邮箱格式不正确'); } }, url: function(value) { var EXP = /^(#|(http(s?)):\/\/|\/\/)[^\s]+\.[^\s]+$/; if (value && !EXP.test(value)) { - return '链接格式不正确'; + return layui.$t('链接格式不正确'); } }, number: function(value){ if (value && isNaN(value)) { - return '只能填写数字'; + return layui.$t('只能填写数字'); } }, date: function(value){ var EXP = /^(\d{4})[-\/](\d{1}|0\d{1}|1[0-2])([-\/](\d{1}|0\d{1}|[1-2][0-9]|3[0-1]))*$/; if (value && !EXP.test(value)) { - return '日期格式不正确'; + return layui.$t('日期格式不正确'); } }, identity: function(value) { var EXP = /(^\d{15}$)|(^\d{17}(x|X|\d)$)/; if (value && !EXP.test(value)) { - return '身份证号格式不正确'; + return layui.$t('身份证号格式不正确'); } } }, @@ -466,7 +466,7 @@ layui.define(['lay', 'layer', 'util'], function(exports){ // 下拉选择框 ,select: function(elem){ - var TIPS = '请选择'; + var TIPS = layui.$t('请选择'); var CLASS = 'layui-form-select'; var TITLE = 'layui-select-title'; var NONE = 'layui-select-none'; @@ -765,7 +765,7 @@ layui.define(['lay', 'layer', 'util'], function(exports){ } }else{ if(none){ - dl.find('.'+NONE)[0] || dl.append('

    无匹配项

    '); + dl.find('.'+NONE)[0] || dl.append('

    无匹配项

    ')); } else { dl.find('.'+NONE).remove(); } @@ -956,7 +956,7 @@ layui.define(['lay', 'layer', 'util'], function(exports){ } }); if (arr.length === 0) { - arr.push('
    None
    '); + arr.push('
    None
    ')); } return arr.join(''); }(); @@ -1241,7 +1241,7 @@ layui.define(['lay', 'layer', 'util'], function(exports){ } } else { type ? ( - items[type] ? items[type]() : hint.error('不支持的 "'+ type + '" 表单渲染') + items[type] ? items[type]() : hint.error(layui.$t('不支持的 "')+ type + layui.$t('" 表单渲染')) ) : renderItem(); } return that; @@ -1367,7 +1367,7 @@ layui.define(['lay', 'layer', 'util'], function(exports){ return othis; }(), {tips: 1}); } else if(verType === 'alert') { - layer.alert(errorText, {title: '提示', shadeClose: true}); + layer.alert(errorText, {title: layui.$t('提示'), shadeClose: true}); } // 若返回的为字符或数字,则自动弹出默认提示框;否则由 verify 方法中处理提示 else if(/\b(string|number)\b/.test(typeof errorText)) { diff --git a/src/modules/laydate.js b/src/modules/laydate.js index ae5734b1..de8f301f 100644 --- a/src/modules/laydate.js +++ b/src/modules/laydate.js @@ -176,7 +176,7 @@ ,showBottom: true //是否显示底部栏 ,isPreview: true //是否显示值预览 ,btns: ['clear', 'now', 'confirm'] //右下角显示的按钮,会按照数组顺序排列 - ,lang: 'cn' //语言,只支持cn/en,即中文和英文 + ,lang: (isLayui && layui.cache.locale) || 'cn' //语言,只支持cn/en,即中文和英文 ,theme: 'default' //主题 ,position: null //控件定位方式定位, 默认absolute,支持:fixed/absolute/static ,calendar: false //是否开启公历重要节日,仅支持中文版 @@ -191,50 +191,107 @@ //多语言 Class.prototype.lang = function(){ - var that = this - ,options = that.config - ,text = { + var that = this; + var options = that.config; + var enMonthShort = ['Jan', 'Feb', 'Mar', 'Apr', 'May', 'Jun', 'Jul', 'Aug', 'Sep', 'Oct', 'Nov', 'Dec']; + var text = { cn: { - weeks: ['日', '一', '二', '三', '四', '五', '六'] - ,time: ['时', '分', '秒'] - ,timeTips: '选择时间' - ,startTime: '开始时间' - ,endTime: '结束时间' - ,dateTips: '返回日期' - ,month: ['一', '二', '三', '四', '五', '六', '七', '八', '九', '十', '十一', '十二'] - ,tools: { - confirm: '确定' - ,clear: '清空' - ,now: '现在' + month: { + long: ['一月', '二月', '三月', '四月', '五月', '六月', '七月', '八月', '九月', '十月', '十一月', '十二月'], + short: ['一', '二', '三', '四', '五', '六', '七', '八', '九', '十', '十一', '十二'] + }, + week: { + long: ['周日', '周一', '周二', '周三', '周四', '周五', '周六'], + short: ['日', '一', '二', '三', '四', '五', '六'] + }, + time: ['时', '分', '秒'], + selectTime: '选择时间', + startTime: '开始时间', + endTime: '结束时间', + selectDate: '选择日期', + tools: { + confirm: '确定', + clear: '清空', + now: '现在', + reset: '重置' + }, + timeout: { + date: '结束日期不能早于开始日期
    请重新选择', + time: '结束时间不能早于开始时间
    请重新选择' + }, + invalidDate: '不在有效日期或时间范围内', + formatError: ['日期格式不合法
    必须遵循下述格式:
    ', '
    已为你重置'], + preview: '当前选中的结果', + panelHeaderFormat: { + year: function (y) { return y + ' 年' }, + month: function (m) { return m + ' 月' }, + monthBeforeYear: false + }, + /** 面板中某些字符串拼接使用 */ + view: { + year: '年', + month: '月', + week: '周', + day: '天' + }, + }, + en: { + month: { + long: ['January', 'February', 'March', 'April', 'May', 'June', 'July', 'August', 'September', 'October', 'November', 'December'], + short: ['Jan', 'Feb', 'Mar', 'Apr', 'May', 'Jun', 'Jul', 'Aug', 'Sep', 'Oct', 'Nov', 'Dec'] + }, + week: { + long: ['Sunday', 'Monday', 'Tuesday', 'Wednesday', 'Thursday', 'Friday', 'Saturday'], + short: ['Su', 'Mo', 'Tu', 'We', 'Th', 'Fr', 'Sa'] + }, + time: ['Hours', 'Minutes', 'Seconds'], + selectTime: 'Select Time', + startTime: 'Start Time', + endTime: 'End Time', + selectDate: 'Select Date', + tools: { + confirm: 'Confirm', + clear: 'Clear', + now: 'Now', + reset: 'Reset' + }, + timeout: { + date: 'End time cannot be less than start Time
    Please re-select', + time: 'End time cannot be less than start Time
    Please re-select' + }, + invalidDate: 'Invalid date', + formatError: ['The date format error
    Must be followed:
    ', '
    It has been reset'], + preview: 'The selected result', + // IE11- Date.prototype.toLocaleDateString 不支持第二个参数,所以这里直接使用英文短格式 + panelHeaderFormat: { + year: function (y) { return y}, + month: function (m) { return enMonthShort[m - 1]}, + monthBeforeYear: true + }, + /** 面板中某些字符串拼接使用 */ + view: { + year: '', + month: '', + week: '', + day: '' } - ,timeout: '结束时间不能早于开始时间
    请重新选择' - ,invalidDate: '不在有效日期或时间范围内' - ,formatError: ['日期格式不合法
    必须遵循下述格式:
    ', '
    已为你重置'] - ,preview: '当前选中的结果' - } - ,en: { - weeks: ['Su', 'Mo', 'Tu', 'We', 'Th', 'Fr', 'Sa'] - ,time: ['Hours', 'Minutes', 'Seconds'] - ,timeTips: 'Select Time' - ,startTime: 'Start Time' - ,endTime: 'End Time' - ,dateTips: 'Select Date' - ,month: ['Jan', 'Feb', 'Mar', 'Apr', 'May', 'Jun', 'Jul', 'Aug', 'Sep', 'Oct', 'Nov', 'Dec'] - ,tools: { - confirm: 'Confirm' - ,clear: 'Clear' - ,now: 'Now' - } - ,timeout: 'End time cannot be less than start Time
    Please re-select' - ,invalidDate: 'Invalid date' - ,formatError: ['The date format error
    Must be followed:
    ', '
    It has been reset'] - ,preview: 'The selected result' } }; + + if(isLayui){ + var message = layui.cache.i18nMessages[layui.cache.locale]; + if(message){ + text[layui.cache.locale] = message.lay.laydate; + }else{ + window.console && console.log && console.log('layui[laydate]: Locale "' + layui.cache.locale + '" not found. Please add i18n messages for this locale first.'); + } + } + return text[options.lang] || text['cn']; }; Class.prototype.markerOfChineseFestivals = { + // 仅 /(zh|cn)/i 生效,不做国际化 '0-1-1': '元旦', '0-2-14': '情人' , '0-3-8': '妇女', @@ -328,7 +385,7 @@ if (options.weekStart) { if (!/^[0-6]$/.test(options.weekStart)) { var lang = that.lang(); - options.weekStart = lang.weeks.indexOf(options.weekStart); + options.weekStart = lang.week.short.indexOf(options.weekStart); if (options.weekStart === -1) options.weekStart = 0; } } @@ -551,7 +608,7 @@ lay.each(new Array(7), function(j){ if(i === 0){ var th = lay.elem('th'); - th.innerHTML = lang.weeks[(j + options.weekStart) % 7]; + th.innerHTML = lang.week.short[(j + options.weekStart) % 7]; theadTr.appendChild(th); } tr.insertCell(j); @@ -576,7 +633,7 @@ lay(divFooter).html(function(){ var html = [], btns = []; if(options.type === 'datetime'){ - html.push(''+ lang.timeTips +''); + html.push(''+ lang.selectTime +''); } if(!(!options.range && options.type === 'datetime') || options.fullPanel){ html.push('') @@ -585,7 +642,7 @@ lay.each(options.btns, function(i, item){ var title = lang.tools[item] || 'btn'; if(options.range && item === 'now') return; - if(isStatic && item === 'clear') title = options.lang === 'cn' ? '重置' : 'Reset'; + if(isStatic && item === 'clear') title = lang.tools.reset; btns.push(''+ title +''); }); html.push(''); @@ -1101,7 +1158,7 @@ that.markRender(td, YMD, markers); } - if(options.calendar && options.lang === 'cn'){ + if(options.calendar && /(zh|cn)/i.test(options.lang)){ render(that.markerOfChineseFestivals); } @@ -1499,14 +1556,17 @@ if(!that.panelYM) that.panelYM = {}; that.panelYM[index] = {year: dateTime.year, month: dateTime.month}; - if(options.lang === 'cn'){ - lay(elemYM[0]).attr('lay-type', 'year').html(dateTime.year + ' 年') - lay(elemYM[1]).attr('lay-type', 'month').html((dateTime.month + 1) + ' 月'); - } else { - lay(elemYM[0]).attr('lay-type', 'month').html(lang.month[dateTime.month]); - lay(elemYM[1]).attr('lay-type', 'year').html(dateTime.year); + var formatArr = [ + {type: 'year', value: lang.panelHeaderFormat.year(dateTime.year)}, + {type: 'month', value: lang.panelHeaderFormat.month(dateTime.month + 1)} + ]; + if(lang.panelHeaderFormat.monthBeforeYear){ + formatArr = formatArr.reverse(); } + lay(elemYM[0]).attr('lay-type', formatArr[0]['type']).html(formatArr[0]['value']); + lay(elemYM[1]).attr('lay-type', formatArr[1]['type']).html(formatArr[1]['value']); + //初始默认选择器 if(isAlone){ //年、月等独立选择器 if(options.range){ @@ -1518,7 +1578,7 @@ that.list(options.type, 0).list(options.type, 1); //同步按钮可点状态 - options.type === 'time' ? that.setBtnStatus('时间' + options.type === 'time' ? that.setBtnStatus('isTime' ,lay.extend({}, that.systemDate(), that.startTime) ,lay.extend({}, that.systemDate(), that.endTime) ) : that.setBtnStatus(true); @@ -1595,8 +1655,8 @@ ,elemYM = lay(elemHeader[2]).find('span') ,elemCont = that.elemCont[index || 0] ,haveList = lay(elemCont).find('.'+ ELEM_LIST)[0] - ,isCN = options.lang === 'cn' - ,text = isCN ? '年' : '' + ,isMonthBeforeYear = lang.panelHeaderFormat.monthBeforeYear + ,text = lang.view.year ,listYM = that.listYM[index] || {} ,hms = ['hours', 'minutes', 'seconds'] @@ -1644,7 +1704,7 @@ yearNum++; }); - lay(elemYM[isCN ? 0 : 1]).attr('lay-ym', (yearNum - 8) + '-' + listYM[1]) + lay(elemYM[!isMonthBeforeYear ? 0 : 1]).attr('lay-ym', (yearNum - 8) + '-' + listYM[1]) .html((startY + text) + ' - ' + (yearNum - 1 + text)); } @@ -1661,7 +1721,7 @@ }; i + 1 == listYM[1] && lay(li).addClass(THIS); - li.innerHTML = lang.month[i] + (isCN ? '月' : ''); + li.innerHTML = lang.month.short[i] + lang.view.month; ul.appendChild(li); /* @@ -1683,7 +1743,7 @@ that.cellRender(li, {year: listYM[0], month: i + 1, date: 1}, 'month'); }); - lay(elemYM[isCN ? 0 : 1]).attr('lay-ym', listYM[0] + '-' + listYM[1]) + lay(elemYM[!isMonthBeforeYear ? 0 : 1]).attr('lay-ym', listYM[0] + '-' + listYM[1]) .html(listYM[0] + text); } @@ -1843,7 +1903,7 @@ ,haveSpan = lay(elemHeader[2]).find('.'+ ELEM_TIME_TEXT); scroll(); - span.innerHTML = options.range ? [lang.startTime,lang.endTime][index] : lang.timeTips; + span.innerHTML = options.range ? [lang.startTime,lang.endTime][index] : lang.selectTime; lay(that.elemMain[index]).addClass('laydate-time-show'); if(haveSpan[0]) haveSpan.remove(); @@ -1930,7 +1990,7 @@ //是否异常提示 if(tips && isOut) that.hint( - typeof tips === 'string' ? lang.timeout.replace(/日期/g, tips) : lang.timeout + typeof tips === 'string' ? lang.timeout.time : lang.timeout.date ); } }; @@ -2320,13 +2380,13 @@ if(lay(btn).hasClass(DISABLED)) return; that.list('time', 0); options.range && that.list('time', 1); - lay(btn).attr('lay-type', 'date').html(that.lang().dateTips); + lay(btn).attr('lay-type', 'date').html(that.lang().selectDate); } //选择日期 ,date: function(){ that.closeList(); - lay(btn).attr('lay-type', 'datetime').html(that.lang().timeTips); + lay(btn).attr('lay-type', 'datetime').html(that.lang().selectTime); } //清空、重置 @@ -2374,7 +2434,7 @@ : that.startDate && that.endDate && that.newDate(lay.extend({},that.startDate, that.startTime || {})) > that.newDate(lay.extend({},that.endDate, that.endTime || {})); return isTimeout - ? that.hint(options.type === 'time' ? lang.timeout.replace(/日期/g, '时间') : lang.timeout) + ? that.hint(options.type === 'time' ? lang.timeout.time : lang.timeout.date) : that.hint(lang.invalidDate); } } else { diff --git a/src/modules/layer.js b/src/modules/layer.js index e3ae1ae1..a4463e98 100644 --- a/src/modules/layer.js +++ b/src/modules/layer.js @@ -34,7 +34,7 @@ var ready = { events: {resize: {}}, minStackIndex: 0, minStackArr: [], - btn: ['确定', '取消'], + btn: [layui.$t('确定'), layui.$t('取消')], // 五种原始层模式 type: ['dialog', 'page', 'iframe', 'loading', 'tips'], @@ -268,7 +268,7 @@ Class.pt.config = { shade: 0.3, fixed: true, move: doms[1], - title: '信息', + title: layui.$t('信息'), offset: 'auto', area: 'auto', closeBtn: 1, @@ -1444,7 +1444,7 @@ layer.prompt = function(options, yes){ return layer.open($.extend({ type: 1, - btn: ['确定','取消'], + btn: [layui.$t('确定'),layui.$t('取消')], content: content, skin: 'layui-layer-prompt' + skin('prompt'), maxWidth: win.width(), @@ -1457,7 +1457,7 @@ layer.prompt = function(options, yes){ yes: function(index){ var value = prompt.val(); if(value.length > (options.maxlength||500)) { - layer.tips('最多输入'+ (options.maxlength || 500) +'个字符', prompt, {tips: 1}); + layer.tips(layui.$t('最多输入')+ (options.maxlength || 500) +layui.$t('个字符'), prompt, {tips: 1}); } else { yes && yes(value, index, prompt); } @@ -1571,7 +1571,7 @@ layer.photos = function(options, loop, key){ // 不直接弹出 if (!loop) return; } else if (data.length === 0){ - return layer.msg('没有图片'); + return layer.msg(layui.$t('没有图片')); } // 上一张 @@ -1819,12 +1819,12 @@ layer.photos = function(options, loop, key){ if (options.toolbar) { arr.push([ '
    ', - '', - '', - '', - '', - '', - '', + layui.$t(''), + layui.$t(''), + layui.$t(''), + layui.$t(''), + layui.$t(''), + layui.$t(''), '
    ' ].join('')); } @@ -1834,7 +1834,7 @@ layer.photos = function(options, loop, key){ arr.push([''].join('')); } @@ -1856,9 +1856,9 @@ layer.photos = function(options, loop, key){ }, options)); }, function(){ layer.close(dict.loadi); - layer.msg('当前图片地址异常,
    是否继续查看下一张?', { + layer.msg(layui.$t('当前图片地址异常,
    是否继续查看下一张?'), { time: 30000, - btn: ['下一张', '不看了'], + btn: [layui.$t('下一张'), layui.$t('不看了')], yes: function(){ data.length > 1 && dict.imgnext(true,true); } diff --git a/src/modules/laypage.js b/src/modules/laypage.js index b7707c95..0a7eba91 100644 --- a/src/modules/laypage.js +++ b/src/modules/laypage.js @@ -72,8 +72,8 @@ layui.define(function(exports) { groups = config.pages; } - config.prev = 'prev' in config ? config.prev : '上一页'; // 上一页文本 - config.next = 'next' in config ? config.next : '下一页'; // 下一页文本 + config.prev = 'prev' in config ? config.prev : layui.$t('上一页'); // 上一页文本 + config.next = 'next' in config ? config.next : layui.$t('下一页'); // 下一页文本 // 计算当前组 var index = config.pages > groups @@ -100,7 +100,7 @@ layui.define(function(exports) { // 首页 if(index > 1 && config.first !== false && groups !== 0){ - pager.push(''+ (config.first || 1) +''); + pager.push(layui.$t('')+ (config.first || 1) +''); } // 计算当前页码组的起始页 @@ -137,7 +137,7 @@ layui.define(function(exports) { pager.push('...'); } if(groups !== 0){ - pager.push(''+ (config.last || config.pages) +''); + pager.push(layui.$t(''+ (config.last || config.pages) +''); } } @@ -153,7 +153,7 @@ layui.define(function(exports) { // 数据总数 count: function(){ - var countText = typeof config.countText === 'object' ? config.countText : ['共 ', ' 条']; + var countText = typeof config.countText === 'object' ? config.countText : [layui.$t('共')+ ' ', ' ' + layui.$t('条')]; return ''+ countText[0] + config.count + countText[1] +'' }(), @@ -161,7 +161,7 @@ layui.define(function(exports) { limit: function(){ var elemArr = ['', + layui.$t(''), '', '{{# } }}', '
      ', @@ -117,7 +117,7 @@ layui.define(['laytpl', 'form'], function(exports) { // 默认配置 Class.prototype.config = { - title: ['列表一', '列表二'], + title: [layui.$t('列表一'), layui.$t('列表二')], width: 200, height: 360, data: [], // 数据源 @@ -125,8 +125,8 @@ layui.define(['laytpl', 'form'], function(exports) { showSearch: false, // 是否开启搜索 id: '', // 唯一索引,默认自增 index text: { - none: '无数据', - searchNone: '无匹配数据' + none: layui.$t('无数据'), + searchNone: layui.$t('无匹配数据') } }; diff --git a/src/modules/tree.js b/src/modules/tree.js index d7f27e2d..1b41f44a 100644 --- a/src/modules/tree.js +++ b/src/modules/tree.js @@ -108,8 +108,8 @@ layui.define(['form','util'], function(exports) { edit: false, // 是否开启节点的操作图标 text: { - defaultNodeName: '未命名', // 节点默认名称 - none: '无数据' // 数据为空时的文本提示 + defaultNodeName: layui.$t('未命名'), // 节点默认名称 + none: layui.$t('无数据') // 数据为空时的文本提示 } }; @@ -576,7 +576,7 @@ layui.define(['form','util'], function(exports) { // 删除 } else { - layer.confirm('确认删除该节点 "'+ (item[customName.title] || '') +'" 吗?', function(index){ + layer.confirm(layui.$t('确认删除该节点 "')+ (item[customName.title] || '') +layui.$t('" 吗?'), function(index){ options.operate && options.operate(returnObj); // 节点删除的回调 returnObj.status = 'remove'; // 标注节点删除 diff --git a/src/modules/treeTable.js b/src/modules/treeTable.js index afe51963..b72d9eb5 100644 --- a/src/modules/treeTable.js +++ b/src/modules/treeTable.js @@ -400,7 +400,7 @@ layui.define(['table'], function (exports) { Class.prototype.getTreeNode = function (data) { var that = this; if (!data) { - return hint.error('找不到节点数据'); + return hint.error(layui.$t('找不到节点数据')); } var options = that.getOptions(); var treeOptions = options.tree; @@ -422,7 +422,7 @@ layui.define(['table'], function (exports) { var that = this; var treeNodeData = that.getNodeDataByIndex(index); if (!treeNodeData) { - return hint.error('找不到节点数据'); + return hint.error(layui.$t('找不到节点数据')); } var options = that.getOptions(); var treeOptions = options.tree; @@ -881,7 +881,7 @@ layui.define(['table'], function (exports) { * */ treeTable.expandAll = function (id, expandFlag) { if (layui.type(expandFlag) !== 'boolean') { - return hint.error('expandAll 的展开状态参数只接收true/false') + return hint.error(layui.$t('expandAll 的展开状态参数只接收true/false')) } var that = getThisTable(id); diff --git a/src/modules/upload.js b/src/modules/upload.js index d0570391..adb0a6ae 100644 --- a/src/modules/upload.js +++ b/src/modules/upload.js @@ -524,11 +524,11 @@ layui.define(['lay', 'layer'], function(exports) { // 文件类型名称 var typeName = ({ - file: '文件', - images: '图片', - video: '视频', - audio: '音频' - })[options.accept] || '文件'; + file: layui.$t('文件'), + images: layui.$t('图片'), + video: layui.$t('视频'), + audio: layui.$t('音频') + })[options.accept] || layui.$t('文件'); // 校验文件格式 value = value.length === 0 @@ -572,7 +572,7 @@ layui.define(['lay', 'layer'], function(exports) { // 校验失败提示 if(check){ - that.msg(text['check-error'] || ('选择的'+ typeName +'中包含不支持的格式')); + that.msg(text['check-error'] || (layui.$t('选择的')+ typeName +layui.$t('中包含不支持的格式'))); return elemFile.value = ''; } @@ -598,8 +598,8 @@ layui.define(['lay', 'layer'], function(exports) { return that.msg(typeof text['limit-number'] === 'function' ? text['limit-number'](options, that.fileLength) : ( - '同时最多只能上传: '+ options.number + ' 个文件' - +'
      您当前已经选择了: '+ that.fileLength +' 个文件' + layui.$t('同时最多只能上传:')+ ' '+ options.number + ' ' + layui.$t('个文件') + +layui.$t('
      您当前已经选择了:')+ ' '+ that.fileLength +' ' + layui.$t('个文件') )); } @@ -617,7 +617,7 @@ layui.define(['lay', 'layer'], function(exports) { }); if(limitSize) return that.msg(typeof text['limit-size'] === 'function' ? text['limit-size'](options, limitSize) - : '文件大小不能超过 '+ limitSize); + : layui.$t('文件大小不能超过')+ ' '+ limitSize); } send(); @@ -642,7 +642,7 @@ layui.define(['lay', 'layer'], function(exports) { var elemFile = that.elemFile; var item = options.item ? options.item : options.elem; var value = files.length > 1 - ? files.length + '个文件' + ? files.length + layui.$t('个文件') : ((files[0] || {}).name || (elemFile[0].value.match(/[^\/\\]+\..+/g)||[]) || ''); if(elemFile.next().hasClass(ELEM_CHOOSE)){ diff --git a/src/modules/util.js b/src/modules/util.js index d258a2e5..83416c7b 100644 --- a/src/modules/util.js +++ b/src/modules/util.js @@ -230,15 +230,15 @@ layui.define('jquery', function(exports) { // 30 天以内,返回「多久前」 if(stamp >= 1000*60*60*24){ - return ((stamp/1000/60/60/24)|0) + ' 天前'; + return ((stamp/1000/60/60/24)|0) + ' ' + layui.$t('天前'); } else if(stamp >= 1000*60*60){ - return ((stamp/1000/60/60)|0) + ' 小时前'; + return ((stamp/1000/60/60)|0) + ' ' + layui.$t('小时前'); } else if(stamp >= 1000*60*3){ // 3 分钟以内为:刚刚 - return ((stamp/1000/60)|0) + ' 分钟前'; + return ((stamp/1000/60)|0) + ' ' + layui.$t('分钟前'); } else if(stamp < 0){ - return '未来'; + return layui.$t('未来'); } else { - return '刚刚'; + return layui.$t('刚刚'); } }, @@ -305,17 +305,17 @@ layui.define('jquery', function(exports) { var defaultMeridiem = function(hours, minutes){ var hm = hours * 100 + minutes; if (hm < 600) { - return '凌晨'; + return layui.$t('凌晨'); } else if (hm < 900) { - return '早上'; + return layui.$t('早上'); } else if (hm < 1100) { - return '上午'; + return layui.$t('上午'); } else if (hm < 1300) { - return '中午'; + return layui.$t('中午'); } else if (hm < 1800) { - return '下午'; + return layui.$t('下午'); } - return '晚上'; + return layui.$t('晚上'); }; var meridiem = (options && options.customMeridiem) || defaultMeridiem;