-
Notifications
You must be signed in to change notification settings - Fork 140
Expand file tree
/
Copy pathplugin.ts
More file actions
174 lines (157 loc) · 4.99 KB
/
plugin.ts
File metadata and controls
174 lines (157 loc) · 4.99 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
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
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
import { Plugin as EsbuildPlugin } from 'esbuild';
import * as fs from 'fs';
/**
* 在vite中dev模式下会使用esbuild对node_modules进行预编译,导致找不到映射表中的filepath,
* 需要在预编译之前进行替换
* @param options 替换语言包
* @returns
*/
export function esbuildPluginMonacoEditorNls(): EsbuildPlugin {
return {
name: 'esbuild-plugin-monaco-editor-nls',
setup(build) {
build.onLoad({ filter: /esm[\\\/]vs[\\\/]nls\.js/ }, async () => {
return {
contents: getLocalizeCode(),
loader: 'js',
};
});
// Handle nls.messages.js
build.onLoad({ filter: /esm[\\\/]vs[\\\/]nls\.messages\.js/ }, async () => {
return {
contents: getNLSMessagesCode(),
loader: 'js',
};
});
build.onLoad({ filter: /monaco-editor[\\\/]esm[\\\/]vs.+\.js/ }, async (args) => {
return {
contents: transformLocalizeFuncCode(args.path),
loader: 'js',
};
});
},
};
}
/**
* 替换调用方法接口参数,替换成相应语言包语言
* @param filepath 路径
* @param CURRENT_LOCALE_DATA 替换规则
* @returns
*/
function transformLocalizeFuncCode(filepath: string) {
let code = fs.readFileSync(filepath, 'utf8');
const re = /(?:monaco-editor[\\\/]esm[\\\/])(.+)(?=\.js)/;
if (re.exec(filepath)) {
let path = RegExp.$1;
path = path.replaceAll('\\', '/');
code = code.replace(/localize\(/g, `localize('${path}', `).replace(/localize2\(/g, `localize2('${path}', `);
}
return code;
}
function getLocalizeCode() {
return `
// replace monaco-editor/esm/vs/nls.js
import { getNLSLanguage, getNLSMessages } from './nls.messages.js';
function _format(message, args) {
// Make sure message is a string
if (typeof message !== 'string') {
message = String(message || '');
}
let result;
if (args.length === 0) {
result = message;
} else {
result = message.replace(/\{(\d+)\}/g, function (match, rest) {
const index = rest[0];
const arg = args[index];
let result = match;
if (typeof arg === 'string') {
result = arg;
}
else if (typeof arg === 'number' || typeof arg === 'boolean' || arg === void 0 || arg === null) {
result = String(arg);
}
return result;
});
}
return result;
}
/**
* @skipMangle
*/
export function localize(path, data, defaultMessage) {
const key = typeof data === "object" ? data.key : data;
const lang = document?.documentElement.getAttribute("lang") || "en";
const _data = window.__locale__?.[lang] || {};
let message = (_data[path] || {})[key];
if (!message) {
message = defaultMessage;
}
// Make sure message is a string
if (typeof message !== 'string') {
return defaultMessage || key || '';
}
const args = [];
for (let _i = 3; _i < arguments.length; _i++) {
args[_i - 3] = arguments[_i];
}
return _format(message, args);
}
/**
* Only used when built: Looks up the message in the global NLS table.
* This table is being made available as a global through bootstrapping
* depending on the target context.
*/
function lookupMessage(index, fallback) {
const message = getNLSMessages()?.[index];
if (typeof message !== 'string') {
if (typeof fallback === 'string') {
return fallback;
}
throw new Error(\`!!! NLS MISSING: \${index} !!!\`);
}
return message;
}
/**
* @skipMangle
*/
export function localize2(path, data, originalMessage) {
const key = typeof data === "object" ? data.key : data;
const lang = document?.documentElement.getAttribute("lang") || "en";
const _data = window.__locale__?.[lang] || {};
let message = (_data[path] || {})[key];
if (!message) {
message = originalMessage;
}
// Make sure message is a string
if (typeof message !== 'string') {
message = originalMessage || key || '';
}
if (typeof originalMessage !== 'string') {
originalMessage = key || '';
}
const args = [];
for (let _i = 3; _i < arguments.length; _i++) {
args[_i - 3] = arguments[_i];
}
const value = _format(message, args);
return {
value,
original: originalMessage === message ? value : _format(originalMessage, args)
};
}
// Re-export from nls.messages.js for compatibility
export { getNLSLanguage, getNLSMessages } from './nls.messages.js';
`;
}
function getNLSMessagesCode() {
return `
// replace monaco-editor/esm/vs/nls.messages.js
export function getNLSMessages() {
return globalThis._VSCODE_NLS_MESSAGES;
}
export function getNLSLanguage() {
return document?.documentElement.getAttribute("lang") || "en";
}
`;
}