langs_app.js
3.02 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
let propertyMapEnglish={
Alarm: 'Alarm_English',
EquipmentName: 'EquipmentName_English',
EquipmentPropName: 'EquipmentPropName_English',
}
/*获取 Cookie 值的辅助函数 */
String.prototype.GetCookieValue_langs = function (name) {
var value = "; " + document.cookie;
var parts = value.split("; " + name + "=");
if (parts.length === 2) {
return parts.pop().split(";").shift();
}
return null;
}
//解析当前文化的函数【外部只要调用这个】
String.prototype.GetLangConf = function () {
var cookieValue = "".GetCookieValue_langs('.AspNetCore.Culture');
if (!cookieValue) return 'en-us'; // 默认英文
try {
// 解码 URL 编码
cookieValue = decodeURIComponent(cookieValue);
} catch (e) {
console.warn("解码 Cookie 失败,使用原始值:", cookieValue);
}
var culture = 'en-us';
// 方式1:正则表达式
var match = cookieValue.match(/c=([^|;]+)/i);
if (match && match[1]) {
culture = match[1].toLowerCase();
} else {
// 方式2:split 方法
var parts = cookieValue.split('|');
for (var i = 0; i < parts.length; i++) {
var part = parts[i].trim();
if (part.toLowerCase().startsWith('c=')) {
culture = part.substring(2).toLowerCase();
break;
}
}
}
return culture;
}
const lang = "".GetLangConf() || 'en-us';
//表格数据中英文翻译方法 【外部只要调用这个】
String.prototype.ParseDataBefore = function (res) {
// 验证输入参数
if (!Array.isArray(res) || res.length === 0) {
console.warn("【数据是多个对象】未触发表格数据中英文翻译公共方法,可能需要前端自定义处理")
return res || [];
}
if (lang !== 'en-us') return res;
// 检查数组元素是否为对象
const firstItem = res[0];
if (!firstItem || typeof firstItem !== 'object' || firstItem === null) {
console.warn("【数据是数组】未触发表格数据中英文翻译公共方法,需要后台翻译")
return res; // 如果第一个元素不是对象,直接返回
}
const processedData = JSON.parse(JSON.stringify(res));
// 处理每个对象
return processedData.map(item => {
const newItem = { ...item };
// 处理每个配置的属性映射
Object.entries(propertyMapEnglish).forEach(([targetProp, sourceProp]) => {
// target='EquipmentPropName', source='EquipmentPropName_English'
if (newItem[sourceProp] !== undefined) {
// 将英文属性值赋给目标属性
newItem[targetProp] = newItem[sourceProp];
// 删除英文属性
delete newItem[sourceProp];
}
});
return newItem;
});
}
window.$lan = (params) => {
if (lang == "zh-cn") return params;
const data = window.langConf[lang];
if (data && data[params]) {
return data[params];
}
return params;
};