wip(i18n): 转义翻译结果

pull/2742/head
sight 2025-06-25 17:39:08 +08:00
parent 03dcbb7931
commit b0aeee7bf2
11 changed files with 114 additions and 47 deletions

View File

@ -54,9 +54,9 @@ i18n.set({
now: '现在',
reset: '重置'
},
timeout: '结束时间不能早于开始时间<br>请重新选择',
timeout: '结束时间不能早于开始时间\n请重新选择',
invalidDate: '不在有效日期或时间范围内',
formatError: ['日期格式不合法<br>必须遵循下述格式:<br>', '<br>已为你重置'],
formatError: ['日期格式不合法\n必须遵循下述格式\n', '\n已为你重置'],
preview: '当前选中的结果'
},
layer: {
@ -78,7 +78,7 @@ i18n.set({
},
viewPicture: '查看原图',
urlError: {
prompt: '当前图片地址异常,<br>是否继续查看下一张?',
prompt: '当前图片地址异常,\n是否继续查看下一张?',
confirm: '下一张',
cancel: '不看了'
}

View File

@ -54,9 +54,9 @@ export default {
now: 'Now',
reset: 'Reset'
},
timeout: 'End time cannot be less than start Time<br>Please re-select',
timeout: 'End time cannot be less than start Time\nPlease re-select',
invalidDate: 'Invalid date',
formatError: ['The date format error<br>Must be followed<br>', '<br>It has been reset'],
formatError: ['The date format error\nMust be followed\n', '\nIt has been reset'],
preview: 'The selected result'
},
layer: {
@ -78,7 +78,7 @@ export default {
},
viewPicture: 'View Original',
urlError: {
prompt: 'Image URL is invalid<br>Continue to next one?',
prompt: 'Image URL is invalid, \nContinue to next one?',
confirm: 'Next',
cancel: 'Cancel'
}

View File

@ -54,9 +54,9 @@ export default {
now: 'Ajd.',
reset: 'Réinit.'
},
timeout: 'Lheure de fin ne peut pas être antérieure à lheure de début<br>Veuillez recommencer',
timeout: 'Lheure de fin ne peut pas être antérieure à lheure de début\nVeuillez recommencer',
invalidDate: 'Date ou heure hors plage valide',
formatError: ['Format de date invalide<br>Le format attendu est :<br>', '<br>Il a été réinitialisé pour vous'],
formatError: ['Format de date invalide\nLe format attendu est :\n', '\nIl a été réinitialisé pour vous'],
preview: 'Résultat sélectionné actuel'
},
layer: {
@ -78,7 +78,7 @@ export default {
},
viewPicture: 'Voir limage originale',
urlError: {
prompt: 'Ladresse de limage est invalide<br>Continuer avec la suivante ?',
prompt: 'Ladresse de limage est invalide,\nContinuer avec la suivante ?',
confirm: 'Suivante',
cancel: 'Annuler'
}

View File

@ -53,9 +53,9 @@ export default {
now: '現在',
reset: '重設'
},
timeout: '結束時間不能早於開始時間<br>請重新選擇',
timeout: '結束時間不能早於開始時間\n請重新選擇',
invalidDate: '不在有效日期或時間範圍內',
formatError: ['日期格式不合法<br>必須遵循以下格式:<br>', '<br>已為你重設'],
formatError: ['日期格式不合法\n必須遵循以下格式\n', '\n已為你重設'],
preview: '當前選中的結果'
},
layer: {
@ -77,7 +77,7 @@ export default {
},
viewPicture: '查看原圖',
urlError: {
prompt: '當前圖片地址異常,<br>是否繼續查看下一張?',
prompt: '當前圖片地址異常,\n是否繼續查看下一張?',
confirm: '下一張',
cancel: '不看了'
}

View File

@ -96,7 +96,7 @@ html #layuicss-laydate{display: none; position: absolute; width: 1989px;}
.layui-laydate .laydate-time-list-hide-2 ol li{padding-left: 117px;}
/* 提示 */
.layui-laydate-hint{position: absolute; top: 115px; left: 50%; width: 250px; margin-left: -125px; line-height: 20px; padding: 15px; text-align: center; font-size: 12px; color: #FF5722;}
.layui-laydate-hint{position: absolute; top: 115px; left: 50%; width: 250px; margin-left: -125px; line-height: 20px; padding: 15px; text-align: center; font-size: 12px; color: #FF5722;white-space: pre-line;}
/* 双日历 */

View File

@ -120,6 +120,28 @@
};
}
// Array.prototype.map
// 引用自 https://github.com/inexorabletash/polyfill/blob/716a3f36ca10fad032083014faf1a47c638e2502/es5.js#L300-L345
if (!Array.prototype.map) {
Array.prototype.map = function (fun /*, thisp */) {
if (this === void 0 || this === null) { throw TypeError(); }
var t = Object(this);
var len = t.length >>> 0;
if (typeof fun !== "function") { throw TypeError(); }
var res = []; res.length = len;
var thisp = arguments[1], i;
for (i = 0; i < len; i++) {
if (i in t) {
res[i] = fun.call(thisp, t[i], i, t);
}
}
return res;
};
}
/**
* 节点加载事件
* @param {HTMLElement} node - script link 节点

View File

@ -66,9 +66,9 @@ layui.define('lay', function(exports) {
now: '现在',
reset: '重置'
},
timeout: '结束时间不能早于开始时间<br>请重新选择',
timeout: '结束时间不能早于开始时间\n请重新选择',
invalidDate: '不在有效日期或时间范围内',
formatError: ['日期格式不合法<br>必须遵循下述格式:<br>', '<br>已为你重置'],
formatError: ['日期格式不合法\n必须遵循下述格式\n', '\n已为你重置'],
preview: '当前选中的结果'
},
layer: {
@ -90,7 +90,7 @@ layui.define('lay', function(exports) {
},
viewPicture: '查看原图',
urlError: {
prompt: '当前图片地址异常,<br>是否继续查看下一张?',
prompt: '当前图片地址异常,\n是否继续查看下一张?',
confirm: '下一张',
cancel: '不看了'
}
@ -217,6 +217,30 @@ layui.define('lay', function(exports) {
return result;
}
/**
* 对传入的值进行转义处理
* 若值为字符串直接进行转义若为函数对函数返回的字符串进行转义若为数组对数组中的字符串元素进行转义
* @param {any} value - 需要进行转义处理的值
* @returns {any} - 转义后的结果
*/
function escape(value) {
if(typeof value === 'string'){
value = lay.escape(value);
}else if(typeof value === 'function'){
var origFn = value;
value = function(){
var val = origFn.apply(this, arguments)
return typeof val === 'string' ? lay.escape(val) : val;
}
}else if(layui.type(value) === 'array'){
value = value.map(function(v){
return typeof v === 'string' ? lay.escape(v) : v;
});
}
return value
}
var i18n = {
config: config,
set: function(options) {
@ -271,19 +295,19 @@ layui.define('lay', function(exports) {
var opts = args[1];
// 第二个参数为对象或数组,替换占位符 {key} 或 {0}, {1}...
if (opts !== null && typeof opts === 'object') {
return result.replace(OBJECT_REPLACE_REGEX, function(match, key) {
result = result.replace(OBJECT_REPLACE_REGEX, function(match, key) {
return opts[key] !== undefined ? opts[key] : match;
});
}else{
// 处理可变参数,替换占位符 {0}, {1}...
result = result.replace(INDEX_REPLACE_REGEX, function(match, index) {
var arg = args[index + 1];
return arg !== undefined ? arg : match;
});
}
// 处理可变参数,替换占位符 {0}, {1}...
return result.replace(INDEX_REPLACE_REGEX, function(match, index) {
var arg = args[index + 1];
return arg !== undefined ? arg : match;
});
}
return result;
return escape(result);
};
/**

View File

@ -796,10 +796,46 @@
};
var hasOwnProperty = Object.prototype.hasOwnProperty;
/**
* 检查对象是否具有指定的属性
* @param {Record<string, any>} obj 要检查的对象
* @param {string} prop 要检查的属性名
* @returns {boolean} 如果对象具有指定的属性则为 true否则为 false
*/
lay.hasOwn = function(obj, prop){
return hasOwnProperty.call(obj, prop);
};
/**
* 转义 HTML 字符串中的特殊字符
* @param {string} html 要转义的 HTML 字符串
* @returns {string} 转义后的 HTML 字符串
*/
lay.escape = function (html) {
var exp = /[<"'>]|&(?=#?[a-zA-Z0-9]+)/g;
if (html === undefined || html === null) return '';
html += '';
if (!exp.test(html)) return html;
return html.replace(/&(?=#?[a-zA-Z0-9]+;?)/g, '&amp;')
.replace(/</g, '&lt;').replace(/>/g, '&gt;')
.replace(/'/g, '&#39;').replace(/"/g, '&quot;');
};
/**
* 还原转义的 HTML 字符串中的特殊字符
* @param {string} html 要还原转义的 HTML 字符串
* @returns {string} 还原转义后的 HTML 字符串
*/
lay.unescape = function (html) {
if (html === undefined || html === null) return '';
return String(html).replace(/\&quot;/g, '"').replace(/\&#39;/g, '\'')
.replace(/\&gt;/g, '>').replace(/\&lt;/g, '<')
.replace(/\&amp;/g, '&');
};
/*
* lay 元素操作

View File

@ -199,9 +199,9 @@ layui.define(['lay', 'i18n'], function(exports) {
now: 'Now',
reset: 'Reset'
},
timeout: 'End time cannot be less than start Time<br>Please re-select',
timeout: 'End time cannot be less than start Time\nPlease re-select',
invalidDate: 'Invalid date',
formatError: ['The date format error<br>Must be followed<br>', '<br>It has been reset'],
formatError: ['The date format error\nMust be followed\n', '\nIt has been reset'],
preview: 'The selected result'
}
};

View File

@ -50,7 +50,7 @@ var ready = {
},
viewPicture: '查看原图',
urlError: {
prompt: '当前图片地址异常,<br>是否继续查看下一张?',
prompt: '当前图片地址异常,\n是否继续查看下一张?',
confirm: '下一张',
cancel: '不看了'
}
@ -1921,7 +1921,7 @@ layer.photos = function(options, loop, key){
}, options));
}, function(){
layer.close(dict.loadi);
layer.msg(i18n.$t('layer.photos.urlError.prompt'), {
layer.msg('<span style="white-space: pre-line;">' + i18n.$t('layer.photos.urlError.prompt') + '</span>', {
time: 30000,
btn: [i18n.$t('layer.photos.urlError.confirm'), i18n.$t('layer.photos.urlError.cancel')],
yes: function(){

View File

@ -2,12 +2,13 @@
* util 工具组件
*/
layui.define(['i18n', 'jquery'], function(exports) {
layui.define(['lay', 'i18n', 'jquery'], function(exports) {
"use strict";
var $ = layui.$;
var hint = layui.hint();
var i18n = layui.i18n;
var lay = layui.lay;
// 引用自 dayjs
// https://github.com/iamkun/dayjs/blob/v1.11.9/src/constant.js#L30
@ -335,26 +336,10 @@ layui.define(['i18n', 'jquery'], function(exports) {
},
// 转义 html
escape: function(html){
var exp = /[<"'>]|&(?=#?[a-zA-Z0-9]+)/g;
if (html === undefined || html === null) return '';
html += '';
if (!exp.test(html)) return html;
return html.replace(/&(?=#?[a-zA-Z0-9]+;?)/g, '&amp;')
.replace(/</g, '&lt;').replace(/>/g, '&gt;')
.replace(/'/g, '&#39;').replace(/"/g, '&quot;');
},
escape: lay.escape,
// 还原转义的 html
unescape: function(html){
if (html === undefined || html === null) return '';
return String(html).replace(/\&quot;/g, '"').replace(/\&#39;/g, '\'')
.replace(/\&gt;/g, '>').replace(/\&lt;/g, '<')
.replace(/\&amp;/g, '&');
},
unescape: lay.unescape,
// 打开新窗口
openWin: function(options){