monaco-editor-nls-adapter 2.1.0 → 2.1.1
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/README.md +5 -1
- package/README_zh.md +5 -1
- package/index.d.ts +38 -19
- package/lite.js +20 -4
- package/package.json +1 -1
- package/proxy.js +13 -8
- package/transform.js +5 -5
package/README.md
CHANGED
|
@@ -21,6 +21,7 @@ Multi-language NLS adapter for Monaco Editor 0.50.0+ (Self-hosted). Bridge the g
|
|
|
21
21
|
- **Flexible API**: Advanced interfaces like `getCurrentLocale` and `setMessages` (custom data).
|
|
22
22
|
- **Cross-Bundler**: Native support for **Webpack** (Loader) and **Vite/Rollup** (Plugin).
|
|
23
23
|
- **Zero Configuration**: Automatic detection of `pnpm` and monorepo paths out of the box.
|
|
24
|
+
- **Resilient**: Global singleton state for micro-frontends and nested dependency environments.
|
|
24
25
|
- **Ultra-lightweight**: Redundant test files removed for even faster installation.
|
|
25
26
|
|
|
26
27
|
## 📦 Supported Frameworks
|
|
@@ -174,8 +175,11 @@ See: [Framework Integration Best Practices (Examples)](./examples/framework-inte
|
|
|
174
175
|
| --- | --- |
|
|
175
176
|
| `init(locale?: string): boolean` | Synchronous initialization. **Note**: This will trigger the bundler to scan all locales in the directory. |
|
|
176
177
|
| `initAsync(locale?: string): Promise<boolean>` | Asynchronous initialization with dynamic imports. |
|
|
177
|
-
| `
|
|
178
|
+
| `getLocaleName(): string` | [New] Returns the currently active locale code name (e.g., 'zh-hans'). |
|
|
179
|
+
| `getLocaleData(): object` | [New] Returns the current raw translation dictionary object. |
|
|
180
|
+
| `getCurrentLocale(): string` | Returns the currently active locale (alias for `getLocaleName`). |
|
|
178
181
|
| `setMessages(data: object, locale?: string)` | Manually inject translations. Recommended with `./lite` for minimum footprint. |
|
|
182
|
+
| `setLocaleData(data: object, locale?: string)` | Alias to `setMessages`. Sets the locale data into the proxy directly. |
|
|
179
183
|
| `vitePlugin(options?: object)` | Vite plugin function. |
|
|
180
184
|
| `loader: string` | Absolute path to the Webpack loader. |
|
|
181
185
|
|
package/README_zh.md
CHANGED
|
@@ -22,6 +22,7 @@
|
|
|
22
22
|
- **灵活的 API**: 提供 `getCurrentLocale` 和 `setMessages` (自定义数据) 等高级接口。
|
|
23
23
|
- **跨构建工具支持**: 原生支持 **Webpack** (Loader) 以及 **Vite/Rollup** (Plugin)。
|
|
24
24
|
- **智能路径探测**: 自动识别 `pnpm` 和 monorepo 路径,**零配置**即可使用。
|
|
25
|
+
- **环境鲁棒性**: 内置全局单例状态管理,支持微前端和多版本依赖嵌套场景。
|
|
25
26
|
- **轻量化**: 移除了冗余测试文件,发布包体积进一步精简。
|
|
26
27
|
|
|
27
28
|
## 📦 支持的框架
|
|
@@ -175,8 +176,11 @@ import * as monaco from 'monaco-editor';
|
|
|
175
176
|
| --- | --- |
|
|
176
177
|
| `init(locale?: string): boolean` | 同步初始化。如果不传参数,尝试探测浏览器语言。注意:此方法会导致构建工具引入全量语言包(或触发全量扫描)。 |
|
|
177
178
|
| `initAsync(locale?: string): Promise<boolean>` | 异步初始化。利用动态 import 拆分语言包。 |
|
|
178
|
-
| `
|
|
179
|
+
| `getLocaleName(): string` | [新] 获取当前生效的语言代码名称(如 'zh-hans')。 |
|
|
180
|
+
| `getLocaleData(): object` | [新] 获取当前已加载的完整本地化字典对象。 |
|
|
181
|
+
| `getCurrentLocale(): string` | 获取当前已生效的语言代码(`getLocaleName` 的存量别名)。 |
|
|
179
182
|
| `setMessages(data: object, locale?: string)` | 手动注入翻译字典。建议配合 `./lite` 入口使用以优化体积。 |
|
|
183
|
+
| `setLocaleData(data: object, locale?: string)` | 同 `setMessages`,直接将数据注入代理层。 |
|
|
180
184
|
| `vitePlugin(options?: object)` | Vite 插件函数。 |
|
|
181
185
|
| `loader: string` | Webpack Loader 的绝对路径。 |
|
|
182
186
|
|
package/index.d.ts
CHANGED
|
@@ -2,61 +2,80 @@ import { NLSProxy } from './proxy';
|
|
|
2
2
|
import monacoNlsPlugin from './vite-plugin';
|
|
3
3
|
|
|
4
4
|
/**
|
|
5
|
-
*
|
|
5
|
+
* 预设的支持语言代码类型 (也可传递任意字符串)
|
|
6
|
+
* Preset of supported locale codes (any string may be used).
|
|
6
7
|
*/
|
|
7
8
|
export type LocaleCode =
|
|
8
|
-
| 'zh-hans' | 'zh-cn' | 'zh-hant' | 'zh-tw'
|
|
9
|
+
| 'zh-hans' | 'zh-cn' | 'zh-hant' | 'zh-tw' | 'zh-hk'
|
|
9
10
|
| 'en' | 'ja' | 'ko' | 'de' | 'fr' | 'es' | 'it' | 'ru' | 'pl' | 'tr' | 'cs' | 'pt-br';
|
|
10
11
|
|
|
11
12
|
/**
|
|
12
|
-
*
|
|
13
|
-
*
|
|
14
|
-
*
|
|
15
|
-
*
|
|
13
|
+
* [全量入口] 使用指定的语言代码初始化 Monaco Editor 的本地化。
|
|
14
|
+
* 注意:由于包含动态 require 逻辑,在 Webpack/Vite 环境下可能会导致扫描并打包全量语言 JSON。
|
|
15
|
+
* [Full Entry] Initialize Monaco Editor localization with a specific locale.
|
|
16
|
+
* WARNING: Uses dynamic require, which may bundle all locales in development/production.
|
|
17
|
+
*
|
|
18
|
+
* @param locale 语言代码 (如 'zh-hans') / Locale code identifier.
|
|
19
|
+
* @param force 是否强制重新初始化 / Force re-initialization.
|
|
20
|
+
* @returns 是否加载成功 / Success status.
|
|
16
21
|
*/
|
|
17
22
|
export function init(locale?: LocaleCode | string, force?: boolean): boolean;
|
|
18
23
|
|
|
19
24
|
/**
|
|
20
|
-
* 异步初始化 Monaco Editor
|
|
21
|
-
*
|
|
22
|
-
*
|
|
23
|
-
*
|
|
25
|
+
* [全量入口] 异步初始化 Monaco Editor 的本地化。
|
|
26
|
+
* 使用动态 import 拆分语言包,有助于减小主包体积。
|
|
27
|
+
* [Full Entry] Asynchronously initialize Monaco localization.
|
|
28
|
+
* Uses dynamic import for chunk splitting.
|
|
29
|
+
*
|
|
30
|
+
* @param locale 语言代码 / Locale code.
|
|
31
|
+
* @param force 是否强制加载 / Force re-load.
|
|
32
|
+
* @returns 是否加载成功 / Success status.
|
|
24
33
|
*/
|
|
25
34
|
export function initAsync(locale?: LocaleCode | string, force?: boolean): Promise<boolean>;
|
|
26
35
|
|
|
27
36
|
/**
|
|
28
|
-
* 获取当前已生效的语言代码名称
|
|
37
|
+
* 获取当前已生效的语言代码名称 / Get the current active locale name.
|
|
29
38
|
*/
|
|
30
39
|
export function getLocaleName(): string;
|
|
31
40
|
|
|
32
41
|
/**
|
|
33
|
-
*
|
|
42
|
+
* 获取当前已生效的语言代码名称 (同 getLocaleName) / Get current locale (alias).
|
|
43
|
+
*/
|
|
44
|
+
export function getCurrentLocale(): string;
|
|
45
|
+
|
|
46
|
+
/**
|
|
47
|
+
* 获取当前加载的原始翻译数据字典 / Get current raw translation data.
|
|
34
48
|
*/
|
|
35
49
|
export function getLocaleData(): Record<string, any> | null;
|
|
36
50
|
|
|
37
51
|
/**
|
|
38
|
-
* 手动设置翻译数据字典
|
|
39
|
-
* @param messages 符合 Monaco NLS 格式的 JSON 对象
|
|
40
|
-
* @param locale
|
|
52
|
+
* 手动设置翻译数据字典 / Set current translation messages manually.
|
|
53
|
+
* @param messages 符合 Monaco NLS 格式的 JSON 对象 / Dictionary following Monaco NLS format.
|
|
54
|
+
* @param locale 语言代码标识符 (可选) / Locale code identifier.
|
|
41
55
|
*/
|
|
42
56
|
export function setMessages(messages: Record<string, any>, locale?: string): void;
|
|
43
57
|
|
|
44
58
|
/**
|
|
45
|
-
*
|
|
59
|
+
* 设置当前的本地化字典数据到代理中 / Set locale data into the proxy directly.
|
|
60
|
+
*/
|
|
61
|
+
export function setLocaleData(data: Record<string, any>, locale?: string): void;
|
|
62
|
+
|
|
63
|
+
/**
|
|
64
|
+
* NLS 代理对象声明 / The NLS proxy instance.
|
|
46
65
|
*/
|
|
47
66
|
export const proxy: NLSProxy;
|
|
48
67
|
|
|
49
68
|
/**
|
|
50
|
-
* Vite 插件导出
|
|
69
|
+
* Vite 插件导出 / Vite plugin for build-time transformation.
|
|
51
70
|
*/
|
|
52
71
|
export const vitePlugin: typeof monacoNlsPlugin;
|
|
53
72
|
|
|
54
73
|
/**
|
|
55
|
-
* Webpack Loader
|
|
74
|
+
* Webpack Loader 导出 (用于 webpack.config.js) / Webpack loader path.
|
|
56
75
|
*/
|
|
57
76
|
export const loader: string;
|
|
58
77
|
|
|
59
|
-
//
|
|
78
|
+
// 重新导出子模块中的类型以便全局引用
|
|
60
79
|
export { NLSProxy } from './proxy';
|
|
61
80
|
export { PluginOptions } from './vite-plugin';
|
|
62
81
|
export { transform } from './transform';
|
package/lite.js
CHANGED
|
@@ -1,12 +1,26 @@
|
|
|
1
1
|
const proxy = require('./proxy')
|
|
2
2
|
|
|
3
3
|
/**
|
|
4
|
-
*
|
|
4
|
+
* 获取当前已生效的语言代码名称 (向后兼容)
|
|
5
5
|
*/
|
|
6
6
|
function getCurrentLocale() {
|
|
7
7
|
return proxy.getLocaleName()
|
|
8
8
|
}
|
|
9
9
|
|
|
10
|
+
/**
|
|
11
|
+
* 获取当前已生效的语言名称
|
|
12
|
+
*/
|
|
13
|
+
function getLocaleName() {
|
|
14
|
+
return proxy.getLocaleName()
|
|
15
|
+
}
|
|
16
|
+
|
|
17
|
+
/**
|
|
18
|
+
* 获取当前加载的原始翻译数据
|
|
19
|
+
*/
|
|
20
|
+
function getLocaleData() {
|
|
21
|
+
return proxy.getLocaleData()
|
|
22
|
+
}
|
|
23
|
+
|
|
10
24
|
/**
|
|
11
25
|
* 手动设置翻译数据字典
|
|
12
26
|
* @param {Object} data 符合 Monaco NLS 格式的 JSON 对象
|
|
@@ -17,10 +31,12 @@ function setMessages(data, locale = 'custom') {
|
|
|
17
31
|
}
|
|
18
32
|
|
|
19
33
|
module.exports = {
|
|
20
|
-
getCurrentLocale
|
|
21
|
-
|
|
34
|
+
getCurrentLocale,
|
|
35
|
+
getLocaleName,
|
|
36
|
+
getLocaleData,
|
|
37
|
+
setMessages,
|
|
22
38
|
setLocaleData: proxy.setLocaleData,
|
|
23
|
-
proxy
|
|
39
|
+
proxy,
|
|
24
40
|
// 插件导出 (仍保留,以便用户可以从 lite 引用所有工具)
|
|
25
41
|
vitePlugin: require('./vite-plugin'),
|
|
26
42
|
loader: __dirname + '/loader.js'
|
package/package.json
CHANGED
package/proxy.js
CHANGED
|
@@ -3,9 +3,12 @@
|
|
|
3
3
|
* 此文件通过注入 path 参数,替代了对原本 'monaco-editor-nls/vscode-nls' 的内部依赖方式。
|
|
4
4
|
*/
|
|
5
5
|
|
|
6
|
-
//
|
|
7
|
-
|
|
8
|
-
|
|
6
|
+
// 使用全局对象存储状态,确保在多实例或多版本共存时(如多版本依赖嵌套)状态依然同步
|
|
7
|
+
const globalObj = typeof globalThis !== 'undefined' ? globalThis : (typeof window !== 'undefined' ? window : global)
|
|
8
|
+
globalObj.__MONACO_NLS_ADAPTER_STATE__ = globalObj.__MONACO_NLS_ADAPTER_STATE__ || { data: null, name: '' }
|
|
9
|
+
|
|
10
|
+
// 辅助访问器
|
|
11
|
+
const getState = () => globalObj.__MONACO_NLS_ADAPTER_STATE__
|
|
9
12
|
|
|
10
13
|
// 提前编译正则表达式以提升运行时性能
|
|
11
14
|
const FORMAT_REGEX = /\{(\d+)\}/g
|
|
@@ -34,9 +37,10 @@ function _format(message, args) {
|
|
|
34
37
|
*/
|
|
35
38
|
function localize(path, data, defaultMessage, ...args) {
|
|
36
39
|
const key = (data && typeof data === 'object') ? data.key : data
|
|
40
|
+
const state = getState()
|
|
37
41
|
|
|
38
42
|
// 优化点:使用可选链或短路评估快速定位翻译
|
|
39
|
-
const fileData = (
|
|
43
|
+
const fileData = (state.data && state.data[path])
|
|
40
44
|
const message = fileData ? fileData[key] : undefined
|
|
41
45
|
|
|
42
46
|
// 强化回退逻辑:如果翻译为空或未找到,则回退到默认消息
|
|
@@ -60,22 +64,23 @@ function localize2(path, data, defaultMessage, ...args) {
|
|
|
60
64
|
* @param {string} locale 语言代码
|
|
61
65
|
*/
|
|
62
66
|
function setLocaleData(data, locale = 'custom') {
|
|
63
|
-
|
|
64
|
-
|
|
67
|
+
const state = getState()
|
|
68
|
+
state.data = data
|
|
69
|
+
state.name = locale
|
|
65
70
|
}
|
|
66
71
|
|
|
67
72
|
/**
|
|
68
73
|
* 获取当前已加载的完整字典数据
|
|
69
74
|
*/
|
|
70
75
|
function getLocaleData() {
|
|
71
|
-
return
|
|
76
|
+
return getState().data
|
|
72
77
|
}
|
|
73
78
|
|
|
74
79
|
/**
|
|
75
80
|
* 获取当前生效的语言名称
|
|
76
81
|
*/
|
|
77
82
|
function getLocaleName() {
|
|
78
|
-
return
|
|
83
|
+
return getState().name
|
|
79
84
|
}
|
|
80
85
|
|
|
81
86
|
// 以下为适配 monaco-editor 内部调用的兼容性 Shim 函数
|
package/transform.js
CHANGED
|
@@ -2,13 +2,13 @@ const MagicString = require('magic-string')
|
|
|
2
2
|
|
|
3
3
|
let CACHED_MONACO_ROOT = null
|
|
4
4
|
|
|
5
|
+
// 提前编译正则表达式以提升运行时性能
|
|
6
|
+
const NLS_IMPORT_REGEX = /(import\s+.*?\s+from\s+['"])(.*?\/nls\.js)(['"])/g
|
|
7
|
+
const NLS_REQUIRE_REGEX = /(require\(['"])(.*?\/nls\.js)(['"]\))/g
|
|
8
|
+
const LOCALIZE_REGEX = /nls\.localize(\d?)\(/g
|
|
9
|
+
|
|
5
10
|
/**
|
|
6
11
|
* 转换 Monaco Editor 源代码以注入本地化路径
|
|
7
|
-
* @param {string} source 源代码内容
|
|
8
|
-
* @param {string} id 文件路径 (resourcePath 或 vite id)
|
|
9
|
-
* @param {Object} options 配置项
|
|
10
|
-
* @param {string} options.monacoPath 匹配 Monaco Editor 的路径片段,默认为 'monaco-editor/esm'
|
|
11
|
-
* @returns {Object|string} 转换后的对象 { code, map } 或原始字符串
|
|
12
12
|
*/
|
|
13
13
|
function transform(source, id, options = {}) {
|
|
14
14
|
// 统一路径分隔符
|