forked from cocos/cocos-engine
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathi18n-utils.js
131 lines (117 loc) · 3.16 KB
/
i18n-utils.js
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
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
module.exports.mixin = function(result, ...args) {
for (const arg of args) {
mixinObject(result, arg);
}
return result;
};
module.exports.link = function(root) {
const visited = new Set();
link(root);
return root;
function link(o) {
if (visited.has(o)) {
return;
}
visited.add(o);
const __extends__ = o.__extends__;
const extended = {};
if (typeof __extends__ === 'string') {
const base = find(__extends__);
if (!base) {
console.error(`Can not find the __extends__ ${__extends__}`);
} else {
link(base);
mixinObject(extended, base);
}
}
for (const [k, v] of Object.entries(o)) {
if (typeof v === 'object' && v) {
link(v);
}
}
mixinObject(o, extended);
}
function find(path) {
const items = path.split('.');
if (items.length === 0) {
return undefined;
}
let result = root;
for (const item of items) {
if (!item) {
return undefined;
}
if (!(item in result)) {
return undefined;
}
const r = result[item];
if (typeof r !== 'object' || !r) {
return undefined;
}
result = r;
}
return result;
}
};
function mixinObject(o1, o2, o1FullPath, o2FullPath) {
for (const [k, v2] of Object.entries(o2)) {
if (typeof v2 !== 'object' || !v2) {
if (k in o1) {
reportMixinFailure(k);
} else {
o1[k] = v2;
}
continue;
}
if (Array.isArray(v2)) {
let v1;
if (!(k in o1)) {
v1 = o1[k] = [];
} else if (Array.isArray(o1[k])) {
v1 = o1[k];
} else {
reportMixinFailure(k);
continue;
}
v1.push(...v2);
continue;
}
// v2 is object
{
let v1;
if (!(k in o1)) {
v1 = o1[k] = {};
} else {
v1 = o1[k];
if (typeof v1 !== 'object' || !v1) {
reportMixinFailure(k);
continue;
}
}
mixinObject(v1, v2, getFullPath(o1FullPath, k), getFullPath(o2FullPath, k));
continue;
}
}
function reportMixinFailure(k) {
console.error(
`Can not mix `
+ `${getFullPath(o2FullPath, k)}(type: ${getLogType(o2[k])}) `
+ `into `
+ `${getFullPath(o1FullPath, k)}(type: ${getLogType(o1[k])})`
);
}
function getFullPath(prefix, k) {
return `${prefix}['${k}']`;
}
function getLogType(v) {
if (typeof v !== 'object') {
return typeof v;
} else if (!v) {
return 'null';
} else if (Array.isArray(v)) {
return 'array';
} else {
return 'object';
}
}
}