monaco-editor-nls-adapter 1.0.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/package.json ADDED
@@ -0,0 +1,41 @@
1
+ {
2
+ "name": "monaco-editor-nls-adapter",
3
+ "version": "1.0.0",
4
+ "description": "Multi-language NLS adapter for Monaco Editor 0.50.0+ (Self-hosted)",
5
+ "main": "index.js",
6
+ "types": "index.d.ts",
7
+ "files": [
8
+ "index.js",
9
+ "index.d.ts",
10
+ "loader.js",
11
+ "proxy.js",
12
+ "transform.js",
13
+ "vite-plugin.js",
14
+ "locales/*.json",
15
+ "README.md",
16
+ "README_zh.md"
17
+ ],
18
+ "keywords": [
19
+ "monaco-editor",
20
+ "nls",
21
+ "localization",
22
+ "internationalization",
23
+ "i18n",
24
+ "vite-plugin",
25
+ "webpack-loader"
26
+ ],
27
+ "author": "leepule",
28
+ "license": "MIT",
29
+ "repository": {
30
+ "type": "git",
31
+ "url": "git+ssh://git@github.com:leepule/monaco-editor-nls-adapter.git"
32
+ },
33
+ "bugs": {
34
+ "url": "https://github.com/leepule/monaco-editor-nls-adapter/issues"
35
+ },
36
+ "homepage": "https://github.com/leepule/monaco-editor-nls-adapter#readme",
37
+ "peerDependencies": {
38
+ "monaco-editor": ">= 0.50.0"
39
+ },
40
+ "dependencies": {}
41
+ }
package/proxy.js ADDED
@@ -0,0 +1,74 @@
1
+ /**
2
+ * 用于 Monaco Editor 0.50.0+ 的自托管 NLS 代理
3
+ * 此文件通过注入 path 参数,替代了对原本 'monaco-editor-nls/vscode-nls' 的内部依赖方式。
4
+ */
5
+
6
+ let CURRENT_LOCALE_DATA = null
7
+
8
+ function _format(message, args) {
9
+ if (args.length === 0) {
10
+ return message
11
+ }
12
+ return String(message).replace(/\{(\d+)\}/g, function (match, rest) {
13
+ const index = rest[0]
14
+ return typeof args[index] !== 'undefined' ? args[index] : match
15
+ })
16
+ }
17
+
18
+ function localize(path, data, defaultMessage, ...args) {
19
+ // 针对 Monaco 0.50.0 签名的参数修正:
20
+ // 1. 当通过 loader 调用时,path 是被注入的。
21
+ // 2. 当通过 create 调用时,path 也会被显式作为第一个参数传入。
22
+ const key = (data && typeof data === 'object') ? data.key : data
23
+ const localeData = CURRENT_LOCALE_DATA || {}
24
+
25
+ let message = (localeData[path] || {})[key]
26
+ // 强化回退逻辑:如果翻译为空或未找到,则回退到默认消息
27
+ if (message === undefined || message === null || message === '') {
28
+ message = defaultMessage
29
+ }
30
+
31
+ return _format(message, args)
32
+ }
33
+
34
+ function localize2(path, data, defaultMessage, ...args) {
35
+ const value = localize(path, data, defaultMessage, ...args)
36
+ return {
37
+ value: value,
38
+ original: value
39
+ }
40
+ }
41
+
42
+ function setLocaleData(data) {
43
+ CURRENT_LOCALE_DATA = data
44
+ }
45
+
46
+ function getConfiguredDefaultLocale() {
47
+ return undefined
48
+ }
49
+
50
+ function loadMessageBundle(file) {
51
+ return localize
52
+ }
53
+
54
+ function config(opt) {
55
+ return loadMessageBundle
56
+ }
57
+
58
+ function create(key, data) {
59
+ return {
60
+ localize: (idx, defaultValue, ...args) => localize(key, idx, defaultValue, ...args),
61
+ localize2: (idx, defaultValue, ...args) => localize2(key, idx, defaultValue, ...args),
62
+ getConfiguredDefaultLocale: () => undefined
63
+ }
64
+ }
65
+
66
+ module.exports = {
67
+ localize,
68
+ localize2,
69
+ setLocaleData,
70
+ getConfiguredDefaultLocale,
71
+ loadMessageBundle,
72
+ config,
73
+ create
74
+ }
package/transform.js ADDED
@@ -0,0 +1,37 @@
1
+ const MONACO_ESM_ROOT = 'node_modules/monaco-editor/esm/'
2
+
3
+ /**
4
+ * 转换 Monaco Editor 源代码以注入本地化路径
5
+ * @param {string} source 源代码内容
6
+ * @param {string} id 文件路径 (resourcePath 或 vite id)
7
+ * @returns {string} 转换后的代码
8
+ */
9
+ function transform(source, id) {
10
+ // 统一路径分隔符
11
+ const resourcePath = id.replace(/\\/g, '/')
12
+
13
+ if (resourcePath.includes(MONACO_ESM_ROOT)) {
14
+ // 1. 计算 Monaco 模块路径 (例如 vs/editor/common/editorContextKeys)
15
+ const startIndex = resourcePath.indexOf(MONACO_ESM_ROOT) + MONACO_ESM_ROOT.length
16
+ const modulePath = resourcePath.substring(startIndex).replace('.js', '')
17
+
18
+ // 2. 将 NLS 引用重定向到本包的代理 (proxy) 文件
19
+ const proxyPath = 'monaco-editor-nls-adapter/proxy'
20
+
21
+ // 替换对 nls.js 的引用
22
+ source = source.replace(/import\s+\*\s+as\s+nls\s+from\s+['"].*?\/nls\.js['"]/g, `import * as nls from '${proxyPath}'`)
23
+ source = source.replace(/require\(['"].*?\/nls\.js['"]\)/g, `require('${proxyPath}')`)
24
+
25
+ // 3. 在 localize 和 localize2 调用中注入路径作为第一个参数
26
+ // 注意:只替换调用,不改变原始代码逻辑
27
+ source = source.replace(/nls\.localize\(/g, `nls.localize('${modulePath}', `)
28
+ source = source.replace(/nls\.localize2\(/g, `nls.localize2('${modulePath}', `)
29
+ }
30
+
31
+ return source
32
+ }
33
+
34
+ module.exports = {
35
+ transform,
36
+ MONACO_ESM_ROOT
37
+ }
package/vite-plugin.js ADDED
@@ -0,0 +1,25 @@
1
+ const { transform, MONACO_ESM_ROOT } = require('./transform')
2
+
3
+ /**
4
+ * Vite Plugin for Monaco Editor NLS Adapter
5
+ * Supports both development and production builds.
6
+ */
7
+ function monacoNlsPlugin() {
8
+ return {
9
+ name: 'vite-plugin-monaco-nls-adapter',
10
+ // 强制此插件在代码压缩和混淆之前执行
11
+ enforce: 'pre',
12
+ transform(code, id) {
13
+ // 检查是否是 Monaco Editor 的 ESM 源码文件
14
+ if (id.replace(/\\/g, '/').includes(MONACO_ESM_ROOT)) {
15
+ return {
16
+ code: transform(code, id),
17
+ // 返回原始 map 或 null,由于是简单的正则匹配转换,不处理 SourceMap
18
+ map: null
19
+ }
20
+ }
21
+ }
22
+ }
23
+ }
24
+
25
+ module.exports = monacoNlsPlugin