mirror of https://github.com/layui/layui
wip(i18n): 修改国际化消息对象结构
parent
0669d66aa9
commit
2ad09cb3fc
71
src/layui.js
71
src/layui.js
|
@ -75,10 +75,12 @@
|
||||||
timeout: 10, // 符合规范的模块请求最长等待秒数
|
timeout: 10, // 符合规范的模块请求最长等待秒数
|
||||||
debug: false, // 是否开启调试模式
|
debug: false, // 是否开启调试模式
|
||||||
version: false, // 是否在模块请求时加入版本号参数(以更新模块缓存)
|
version: false, // 是否在模块请求时加入版本号参数(以更新模块缓存)
|
||||||
locale: 'zh-cn', // 设置全局配置的语言
|
i18n:{
|
||||||
i18nMessages: { // 语言包,格式为:{locale: {namespace:{component:{...}}}}
|
locale: 'zh-cn', // 设置全局配置的语言
|
||||||
'zh-cn': {
|
messages: { // 全局国际化消息对象,格式为:{locale: {namespace:{component:{...}}}}
|
||||||
lay: zhCn
|
'zh-cn': {
|
||||||
|
lay: zhCn // layui 使用 `lay` 命名空间,外部自定义模块应使用自定义命名空间
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
@ -232,10 +234,17 @@
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 全局配置
|
* 全局配置
|
||||||
* @param {Object} options
|
* @param {Object} options - 配置对象
|
||||||
|
* @param {boolean} deepMerged - 是否深度合并配置信息,默认为 false
|
||||||
*/
|
*/
|
||||||
Class.prototype.config = function(options) {
|
Class.prototype.config = function(options, deepMerged) {
|
||||||
Object.assign(config, options);
|
|
||||||
|
if(deepMerged){
|
||||||
|
deepClone(config, options);
|
||||||
|
}else{
|
||||||
|
Object.assign(config, options);
|
||||||
|
}
|
||||||
|
|
||||||
return this;
|
return this;
|
||||||
};
|
};
|
||||||
|
|
||||||
|
@ -1102,6 +1111,9 @@
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 根据给定的键从国际化消息中获取翻译后的内容
|
* 根据给定的键从国际化消息中获取翻译后的内容
|
||||||
|
*
|
||||||
|
* 未文档化的私有方法,仅限内部使用
|
||||||
|
* @internal
|
||||||
* @overload
|
* @overload
|
||||||
* @param {string} key 要翻译的键
|
* @param {string} key 要翻译的键
|
||||||
* @param {Record<string, any>} opts 国际化消息对象,替换 {key} 形式的占位符
|
* @param {Record<string, any>} opts 国际化消息对象,替换 {key} 形式的占位符
|
||||||
|
@ -1144,11 +1156,11 @@
|
||||||
Class.prototype.i18nTranslation = function(key){
|
Class.prototype.i18nTranslation = function(key){
|
||||||
var that = this;
|
var that = this;
|
||||||
var args = arguments;
|
var args = arguments;
|
||||||
var locale = that.cache.locale;
|
var i18n = that.cache.i18n;
|
||||||
var i18nMessage = that.cache.i18nMessages[locale];
|
var i18nMessage = i18n.messages[i18n.locale];
|
||||||
|
|
||||||
if(!i18nMessage){
|
if(!i18nMessage){
|
||||||
error('Locale "' + locale + '" not found. Please add i18n messages for this locale first.', 'warn');
|
error('Locale "' + i18n.locale + '" not found. Please add i18n messages for this locale first.', 'warn');
|
||||||
}
|
}
|
||||||
|
|
||||||
var result = get(i18nMessage, key, key);
|
var result = get(i18nMessage, key, key);
|
||||||
|
@ -1176,7 +1188,9 @@
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* i18nTranslation 的别名
|
* 根据给定的键从国际化消息中获取翻译后的内容
|
||||||
|
*
|
||||||
|
* layui.i18nTranslation 的别名,用于简化代码书写,未文档化仅限内部使用
|
||||||
*/
|
*/
|
||||||
Class.prototype.$t = Class.prototype.i18nTranslation;
|
Class.prototype.$t = Class.prototype.i18nTranslation;
|
||||||
|
|
||||||
|
@ -1201,6 +1215,41 @@
|
||||||
return result;
|
return result;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 将两个或多个对象的内容深度合并到第一个对象中
|
||||||
|
* 复制自 lay.extend
|
||||||
|
* @callback ExtendFunc
|
||||||
|
* @param {*} target - 一个对象
|
||||||
|
* @param {...*} objectN - 包含额外的属性合并到第一个参数
|
||||||
|
* @returns {*} 返回合并后的对象
|
||||||
|
*/
|
||||||
|
/** @type ExtendFunc*/
|
||||||
|
function deepClone(){
|
||||||
|
var ai = 1;
|
||||||
|
var length;
|
||||||
|
var args = arguments;
|
||||||
|
var clone = function(target, obj){
|
||||||
|
target = target || (layui.type(obj) === 'array' ? [] : {}); // 目标对象
|
||||||
|
for(var i in obj){
|
||||||
|
// 若值为普通对象,则进入递归,继续深度合并
|
||||||
|
target[i] = (obj[i] && obj[i].constructor === Object)
|
||||||
|
? clone(target[i], obj[i])
|
||||||
|
: obj[i];
|
||||||
|
}
|
||||||
|
return target;
|
||||||
|
};
|
||||||
|
|
||||||
|
args[0] = typeof args[0] === 'object' ? args[0] : {};
|
||||||
|
length = args.length
|
||||||
|
|
||||||
|
for(; ai < length; ai++){
|
||||||
|
if(typeof args[ai] === 'object'){
|
||||||
|
clone(args[0], args[ai]);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return args[0];
|
||||||
|
};
|
||||||
|
|
||||||
// export layui
|
// export layui
|
||||||
window.layui = new Class();
|
window.layui = new Class();
|
||||||
})(window);
|
})(window);
|
||||||
|
|
|
@ -193,7 +193,6 @@
|
||||||
Class.prototype.lang = function(){
|
Class.prototype.lang = function(){
|
||||||
var that = this;
|
var that = this;
|
||||||
var options = that.config;
|
var options = that.config;
|
||||||
var enMonthShort = ['Jan', 'Feb', 'Mar', 'Apr', 'May', 'Jun', 'Jul', 'Aug', 'Sep', 'Oct', 'Nov', 'Dec'];
|
|
||||||
var text = {
|
var text = {
|
||||||
cn: {
|
cn: {
|
||||||
month: {
|
month: {
|
||||||
|
@ -262,10 +261,10 @@
|
||||||
invalidDate: 'Invalid date',
|
invalidDate: 'Invalid date',
|
||||||
formatError: ['The date format error<br>Must be followed:<br>', '<br>It has been reset'],
|
formatError: ['The date format error<br>Must be followed:<br>', '<br>It has been reset'],
|
||||||
preview: 'The selected result',
|
preview: 'The selected result',
|
||||||
// IE11- Date.prototype.toLocaleDateString 不支持第二个参数,所以这里直接使用英文短格式
|
// IE11- Date.prototype.toLocaleDateString 不支持第二个参数,所以这里用函数实现,直接使用英文短格式
|
||||||
panelHeaderFormat: {
|
panelHeaderFormat: {
|
||||||
year: function (y) { return y},
|
year: function (y) { return y},
|
||||||
month: function (m) { return enMonthShort[m - 1]},
|
month: function (m) { return text.en.month.short[m - 1]},
|
||||||
monthBeforeYear: true
|
monthBeforeYear: true
|
||||||
},
|
},
|
||||||
/** 面板中某些字符串拼接使用 */
|
/** 面板中某些字符串拼接使用 */
|
||||||
|
@ -279,12 +278,15 @@
|
||||||
};
|
};
|
||||||
|
|
||||||
if(isLayui){
|
if(isLayui){
|
||||||
options.lang = layui.cache.locale;
|
var i18n = layui.cache.i18n;
|
||||||
var message = layui.cache.i18nMessages[layui.cache.locale];
|
var isBuiltinEn = options.lang === 'en';
|
||||||
|
// TODO 此处无法得知 render option 中是否设置了 lang,因此不能遵循选项就近原则
|
||||||
|
options.lang = options.lang === 'en' ? options.lang : i18n.locale;
|
||||||
|
var message = i18n.messages[i18n.locale];
|
||||||
if(message){
|
if(message){
|
||||||
text[layui.cache.locale] = message.lay.laydate;
|
text[i18n.locale] = message.lay.laydate;
|
||||||
}else{
|
}else{
|
||||||
window.console && console.log && console.log('layui[laydate]: Locale "' + layui.cache.locale + '" not found. Please add i18n messages for this locale first.');
|
!isBuiltinEn && window.console && console.log && console.log('layui[laydate]: Locale "' + i18n.locale + '" not found. Please add i18n messages for this locale first.');
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
Loading…
Reference in New Issue