monaco-editor-nls-adapter 1.0.1 → 2.1.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/README.md +89 -28
- package/README_zh.md +89 -27
- package/index.d.ts +42 -18
- package/index.js +61 -24
- package/lite.js +27 -0
- package/loader.js +20 -1
- package/locales/cs.json +1 -12669
- package/locales/de.json +1 -12669
- package/locales/es.json +1 -12669
- package/locales/fr.json +1 -12669
- package/locales/it.json +1 -12669
- package/locales/ja.json +1 -12669
- package/locales/ko.json +1 -12669
- package/locales/pl.json +1 -12669
- package/locales/pt-br.json +1 -12669
- package/locales/ru.json +1 -12669
- package/locales/tr.json +1 -12669
- package/locales/zh-hans.json +1 -12669
- package/locales/zh-hant.json +1 -12669
- package/package.json +40 -3
- package/proxy.js +65 -23
- package/transform.js +54 -17
- package/vite-plugin.js +23 -8
- package/locales/qps-ploc.json +0 -12669
package/README.md
CHANGED
|
@@ -13,10 +13,26 @@ Multi-language NLS adapter for Monaco Editor 0.50.0+ (Self-hosted). Bridge the g
|
|
|
13
13
|
|
|
14
14
|
- **Self-hosted Locales**: No external CDN dependencies; all language data is bundled locally.
|
|
15
15
|
- **Monaco 0.50.0+ Ready**: Fully compatible with the latest internal NLS signatures of Monaco Editor.
|
|
16
|
-
- **
|
|
16
|
+
- **SourceMap Support**: Powered by `magic-string` for accurate source-to-bundle mapping.
|
|
17
|
+
- **Ultra-Optimized (Mini & Lite)**:
|
|
18
|
+
- **JSON Minification**: All locale files are pre-minified, reducing the stat size by ~15%.
|
|
19
|
+
- **Slim/Lite Export**: Use `./lite` for a zero-bloat experience with no dynamic scanning.
|
|
17
20
|
- **TypeScript Support**: First-class TS definitions for an excellent developer experience.
|
|
18
|
-
- **
|
|
21
|
+
- **Flexible API**: Advanced interfaces like `getCurrentLocale` and `setMessages` (custom data).
|
|
19
22
|
- **Cross-Bundler**: Native support for **Webpack** (Loader) and **Vite/Rollup** (Plugin).
|
|
23
|
+
- **Zero Configuration**: Automatic detection of `pnpm` and monorepo paths out of the box.
|
|
24
|
+
- **Ultra-lightweight**: Redundant test files removed for even faster installation.
|
|
25
|
+
|
|
26
|
+
## 📦 Supported Frameworks
|
|
27
|
+
|
|
28
|
+
This adapter is compatible with all modern frontend frameworks:
|
|
29
|
+
|
|
30
|
+
- **Vue 2**: Seamless integration with Vue CLI and Webpack projects.
|
|
31
|
+
- **Vue 3**: Full support for Vite and Vue CLI configurations.
|
|
32
|
+
- **React**: Compatible with native React apps and `@monaco-editor/react` (supports disabling CDN).
|
|
33
|
+
- **Angular**: Compatible with Angular CLI (Webpack/Vite) build environments.
|
|
34
|
+
- **SSR Frameworks**: Ready for **Next.js** and **Nuxt** server-side rendering environments.
|
|
35
|
+
- **Universal**: Theoretically supports any web project built with **Webpack**, **Vite**, or **Rollup**.
|
|
20
36
|
|
|
21
37
|
## 🚀 Installation
|
|
22
38
|
|
|
@@ -31,16 +47,23 @@ npm install monaco-editor-nls-adapter
|
|
|
31
47
|
In your `webpack.config.js`, add the adapter's loader to process Monaco Editor's ESM files.
|
|
32
48
|
|
|
33
49
|
```javascript
|
|
50
|
+
const { loader } = require('monaco-editor-nls-adapter');
|
|
51
|
+
|
|
34
52
|
module.exports = {
|
|
35
53
|
module: {
|
|
36
54
|
rules: [
|
|
37
55
|
{
|
|
38
56
|
test: /\.js$/,
|
|
39
|
-
// Crucial: Only process monaco-editor
|
|
40
|
-
include: /
|
|
57
|
+
// Crucial: Only process monaco-editor ESM files
|
|
58
|
+
include: /monaco-editor[\\/]esm/,
|
|
41
59
|
use: [
|
|
42
60
|
{
|
|
43
|
-
loader:
|
|
61
|
+
loader: loader,
|
|
62
|
+
options: {
|
|
63
|
+
// Optional: Custom monaco path fragment
|
|
64
|
+
// Detected automatically in most project structures (npm, pnpm, yarn)
|
|
65
|
+
// monacoPath: 'monaco-editor/esm'
|
|
66
|
+
}
|
|
44
67
|
}
|
|
45
68
|
]
|
|
46
69
|
}
|
|
@@ -54,15 +77,17 @@ module.exports = {
|
|
|
54
77
|
For projects using Vue CLI, you can configure the loader via `chainWebpack`:
|
|
55
78
|
|
|
56
79
|
```javascript
|
|
80
|
+
const { loader } = require('monaco-editor-nls-adapter');
|
|
81
|
+
|
|
57
82
|
module.exports = {
|
|
58
83
|
chainWebpack: config => {
|
|
59
84
|
config.module
|
|
60
85
|
.rule('monaco-editor-nls')
|
|
61
86
|
.test(/\.js$/)
|
|
62
|
-
.include.add(/
|
|
87
|
+
.include.add(/monaco-editor[\\/]esm/)
|
|
63
88
|
.end()
|
|
64
89
|
.use('nls-loader')
|
|
65
|
-
.loader(
|
|
90
|
+
.loader(loader)
|
|
66
91
|
.end();
|
|
67
92
|
}
|
|
68
93
|
};
|
|
@@ -74,50 +99,86 @@ In your `vite.config.js` or `vite.config.ts`:
|
|
|
74
99
|
|
|
75
100
|
```javascript
|
|
76
101
|
import { defineConfig } from 'vite';
|
|
77
|
-
import
|
|
102
|
+
import { vitePlugin } from 'monaco-editor-nls-adapter';
|
|
78
103
|
|
|
79
104
|
export default defineConfig({
|
|
80
105
|
plugins: [
|
|
81
|
-
|
|
106
|
+
vitePlugin({
|
|
107
|
+
// Optional: Custom monaco path fragment
|
|
108
|
+
// Automatically detected (works with pnpm and monorepos)
|
|
109
|
+
// monacoPath: 'monaco-editor/esm'
|
|
110
|
+
})
|
|
82
111
|
]
|
|
83
112
|
});
|
|
84
113
|
```
|
|
85
114
|
|
|
115
|
+
### 4. Bundle Optimization (Optional)
|
|
116
|
+
|
|
117
|
+
If you are concerned about bundle size or the "Stat size" reported by bundle analyzers, you can use the **Lite Version**.
|
|
118
|
+
|
|
119
|
+
The default entry point uses dynamic `require`/`import` with template strings, which may cause build tools (Webpack/Vite) to scan and include all available locales or report a large "original size." The Lite version eliminates this by providing only the core logic.
|
|
120
|
+
|
|
121
|
+
```javascript
|
|
122
|
+
// 1. Import from /lite
|
|
123
|
+
import { setMessages } from 'monaco-editor-nls-adapter/lite';
|
|
124
|
+
|
|
125
|
+
// 2. Manually import ONLY the language you need
|
|
126
|
+
import zhHans from 'monaco-editor-nls-adapter/locales/zh-hans.json';
|
|
127
|
+
|
|
128
|
+
// 3. Initialize BEFORE monaco-editor loads
|
|
129
|
+
setMessages(zhHans, 'zh-hans');
|
|
130
|
+
|
|
131
|
+
// 4. Import monaco-editor as usual
|
|
132
|
+
import * as monaco from 'monaco-editor';
|
|
133
|
+
```
|
|
134
|
+
|
|
86
135
|
## 📖 Usage
|
|
87
136
|
|
|
88
137
|
### Initialization
|
|
89
138
|
|
|
90
139
|
Call `init` or `initAsync` **before** importing `monaco-editor` or creating any editor instances.
|
|
91
140
|
|
|
92
|
-
```typescript
|
|
93
141
|
import * as nlsAdapter from 'monaco-editor-nls-adapter';
|
|
94
142
|
|
|
95
|
-
|
|
96
|
-
* Option 1: Synchronous (Standard)
|
|
97
|
-
* Recommended for small clusters or if you don't mind the initial bundle size.
|
|
98
|
-
*/
|
|
143
|
+
// 1. Synchronous (Standard)
|
|
99
144
|
nlsAdapter.init('zh-hans');
|
|
100
145
|
|
|
101
|
-
|
|
102
|
-
* Option 2: Asynchronous (Performance Optimization)
|
|
103
|
-
* Leverages Webpack/Vite dynamic imports for code splitting.
|
|
104
|
-
*/
|
|
146
|
+
// 2. Asynchronous (Code Splitting)
|
|
105
147
|
// await nlsAdapter.initAsync('zh-hans');
|
|
106
148
|
|
|
107
|
-
|
|
108
|
-
|
|
109
|
-
*/
|
|
110
|
-
// nlsAdapter.init(); // Sync
|
|
111
|
-
// await nlsAdapter.initAsync(); // Async
|
|
112
|
-
|
|
113
|
-
import * as monaco from 'monaco-editor';
|
|
149
|
+
// 3. Get current locale
|
|
150
|
+
console.log(nlsAdapter.getCurrentLocale()); // 'zh-hans'
|
|
114
151
|
|
|
115
|
-
|
|
116
|
-
|
|
117
|
-
|
|
152
|
+
// 4. Custom Messages (Override built-in locales)
|
|
153
|
+
/*
|
|
154
|
+
nlsAdapter.setMessages({
|
|
155
|
+
'vs/editor/common/editorContextKeys': {
|
|
156
|
+
'editor.action.clipboardCopyAction': 'Copy (Custom)'
|
|
157
|
+
}
|
|
118
158
|
});
|
|
159
|
+
*/
|
|
160
|
+
|
|
161
|
+
import * as monaco from 'monaco-editor';
|
|
162
|
+
// ... create editor as usual
|
|
119
163
|
```
|
|
120
164
|
|
|
165
|
+
### Framework Integration (React / Vue)
|
|
166
|
+
|
|
167
|
+
This package works seamlessly with major frameworks. Special note for **React** (@monaco-editor/react): Make sure to use `loader.config({ monaco })` to disable CDN and map to your local, localized monaco instance.
|
|
168
|
+
|
|
169
|
+
See: [Framework Integration Best Practices (Examples)](./examples/framework-integration.md)
|
|
170
|
+
|
|
171
|
+
### API Reference
|
|
172
|
+
|
|
173
|
+
| Function | Description |
|
|
174
|
+
| --- | --- |
|
|
175
|
+
| `init(locale?: string): boolean` | Synchronous initialization. **Note**: This will trigger the bundler to scan all locales in the directory. |
|
|
176
|
+
| `initAsync(locale?: string): Promise<boolean>` | Asynchronous initialization with dynamic imports. |
|
|
177
|
+
| `getCurrentLocale(): string` | Returns the currently active locale code. Returns `custom` if set via `setMessages`. |
|
|
178
|
+
| `setMessages(data: object, locale?: string)` | Manually inject translations. Recommended with `./lite` for minimum footprint. |
|
|
179
|
+
| `vitePlugin(options?: object)` | Vite plugin function. |
|
|
180
|
+
| `loader: string` | Absolute path to the Webpack loader. |
|
|
181
|
+
|
|
121
182
|
## 🗂 Supported Locales
|
|
122
183
|
|
|
123
184
|
| Code | Language |
|
package/README_zh.md
CHANGED
|
@@ -14,9 +14,26 @@
|
|
|
14
14
|
- **自托管语言包**: 无需依赖外部 CDN;所有语言数据在本地打包。
|
|
15
15
|
- **适配 Monaco 0.50.0+**: 完全兼容 Monaco Editor 最新的内部 NLS 调用签名。
|
|
16
16
|
- **性能优化 (懒加载)**: 通过 Webpack/Vite 的动态导入实现按需分包加载。
|
|
17
|
+
- **SourceMap 支持**: 基于 `magic-string` 实现转换后的精准还原,调试无忧。
|
|
18
|
+
- **极致优化 (Mini & Lite)**:
|
|
19
|
+
- **内置 JSON 压缩**: 所有语言包均已移除空格,Stat size 减少约 15%。
|
|
20
|
+
- **Slim/Lite 导出**: 提供 `./lite` 入口,不包含动态加载逻辑,可实现 **0 冗余** 打包。
|
|
17
21
|
- **TypeScript 支持**: 完善的类型定义,为开发提供极佳的智能感知。
|
|
18
|
-
-
|
|
22
|
+
- **灵活的 API**: 提供 `getCurrentLocale` 和 `setMessages` (自定义数据) 等高级接口。
|
|
19
23
|
- **跨构建工具支持**: 原生支持 **Webpack** (Loader) 以及 **Vite/Rollup** (Plugin)。
|
|
24
|
+
- **智能路径探测**: 自动识别 `pnpm` 和 monorepo 路径,**零配置**即可使用。
|
|
25
|
+
- **轻量化**: 移除了冗余测试文件,发布包体积进一步精简。
|
|
26
|
+
|
|
27
|
+
## 📦 支持的框架
|
|
28
|
+
|
|
29
|
+
本适配器与主流前端框架均可完美兼容:
|
|
30
|
+
|
|
31
|
+
- **Vue 2**: 完美适配 Vue CLI 和 Webpack 项目。
|
|
32
|
+
- **Vue 3**: 深度支持 Vite 和 Vue CLI 配置。
|
|
33
|
+
- **React**: 适配原生 React 应用及 `@monaco-editor/react` (支持禁用 CDN)。
|
|
34
|
+
- **Angular**: 支持 Angular CLI (Webpack/Vite) 构建环境。
|
|
35
|
+
- **SSR 框架**: 兼容 **Next.js** 和 **Nuxt** 等服务端渲染环境。
|
|
36
|
+
- **通用**: 理论上支持任何基于 **Webpack**、**Vite** 或 **Rollup** 构建的 web 项目。
|
|
20
37
|
|
|
21
38
|
## 🚀 安装
|
|
22
39
|
|
|
@@ -31,16 +48,23 @@ npm install monaco-editor-nls-adapter
|
|
|
31
48
|
在 `webpack.config.js` 中,添加此适配器的 loader 来处理 Monaco Editor 的 ESM 源码。
|
|
32
49
|
|
|
33
50
|
```javascript
|
|
51
|
+
const { loader } = require('monaco-editor-nls-adapter');
|
|
52
|
+
|
|
34
53
|
module.exports = {
|
|
35
54
|
module: {
|
|
36
55
|
rules: [
|
|
37
56
|
{
|
|
38
57
|
test: /\.js$/,
|
|
39
|
-
// 重要:仅处理 monaco-editor 的 esm
|
|
40
|
-
include: /
|
|
58
|
+
// 重要:仅处理 monaco-editor 的 esm 目录
|
|
59
|
+
include: /monaco-editor[\\/]esm/,
|
|
41
60
|
use: [
|
|
42
61
|
{
|
|
43
|
-
loader:
|
|
62
|
+
loader: loader,
|
|
63
|
+
options: {
|
|
64
|
+
// 可选:自定义 monaco 路径片段
|
|
65
|
+
// 插件会自动探测 node_modules 下的 monaco-editor/esm,通常无需配置
|
|
66
|
+
// monacoPath: 'monaco-editor/esm'
|
|
67
|
+
}
|
|
44
68
|
}
|
|
45
69
|
]
|
|
46
70
|
}
|
|
@@ -54,15 +78,17 @@ module.exports = {
|
|
|
54
78
|
对于使用 Vue CLI 的项目,建议使用 `chainWebpack` 进行配置:
|
|
55
79
|
|
|
56
80
|
```javascript
|
|
81
|
+
const { loader } = require('monaco-editor-nls-adapter');
|
|
82
|
+
|
|
57
83
|
module.exports = {
|
|
58
84
|
chainWebpack: config => {
|
|
59
85
|
config.module
|
|
60
86
|
.rule('monaco-editor-nls')
|
|
61
87
|
.test(/\.js$/)
|
|
62
|
-
.include.add(/
|
|
88
|
+
.include.add(/monaco-editor[\\/]esm/)
|
|
63
89
|
.end()
|
|
64
90
|
.use('nls-loader')
|
|
65
|
-
.loader(
|
|
91
|
+
.loader(loader)
|
|
66
92
|
.end();
|
|
67
93
|
}
|
|
68
94
|
};
|
|
@@ -74,50 +100,86 @@ module.exports = {
|
|
|
74
100
|
|
|
75
101
|
```javascript
|
|
76
102
|
import { defineConfig } from 'vite';
|
|
77
|
-
import
|
|
103
|
+
import { vitePlugin } from 'monaco-editor-nls-adapter';
|
|
78
104
|
|
|
79
105
|
export default defineConfig({
|
|
80
106
|
plugins: [
|
|
81
|
-
|
|
107
|
+
vitePlugin({
|
|
108
|
+
// 可选:自定义 monaco 路径片段
|
|
109
|
+
// 插件会自动探测物理路径(支持 pnpm),通常无需配置
|
|
110
|
+
// monacoPath: 'monaco-editor/esm'
|
|
111
|
+
})
|
|
82
112
|
]
|
|
83
113
|
});
|
|
84
114
|
```
|
|
85
115
|
|
|
116
|
+
### 4. 极致体积优化 (可选)
|
|
117
|
+
|
|
118
|
+
如果你对打包后的 JS 文件体积非常敏感,或者 bundle analyzer 显示 `monaco-editor-nls-adapter` 的 `Stat size` 过大(这是由于默认入口包含所有语言包的动态引用导致的),可以使用 **Lite 版本**。
|
|
119
|
+
|
|
120
|
+
Lite 版本不具备内置的语言包发现逻辑,需要你手动导入所需的语言 JSON。这样构建工具(Webpack/Vite)只会打包你真正使用的那门语言,而不会扫描整个 `./locales` 目录。
|
|
121
|
+
|
|
122
|
+
```javascript
|
|
123
|
+
// 1. 从 /lite 入口导入
|
|
124
|
+
import { setMessages } from 'monaco-editor-nls-adapter/lite';
|
|
125
|
+
|
|
126
|
+
// 2. 手动导入特定的语言 JSON (由你自己决定包含哪门语言)
|
|
127
|
+
import zhHans from 'monaco-editor-nls-adapter/locales/zh-hans.json';
|
|
128
|
+
|
|
129
|
+
// 3. 初始化 (必须要在 monaco 加载前运行)
|
|
130
|
+
setMessages(zhHans, 'zh-hans');
|
|
131
|
+
|
|
132
|
+
// 4. 正常使用 monaco-editor
|
|
133
|
+
import * as monaco from 'monaco-editor';
|
|
134
|
+
```
|
|
135
|
+
|
|
86
136
|
## 📖 使用指南
|
|
87
137
|
|
|
88
138
|
### 初始化
|
|
89
139
|
|
|
90
140
|
在导入 `monaco-editor` 或创建任何实例**之前**,先调用 `init` 或 `initAsync`。
|
|
91
141
|
|
|
92
|
-
```typescript
|
|
93
142
|
import * as nlsAdapter from 'monaco-editor-nls-adapter';
|
|
94
143
|
|
|
95
|
-
|
|
96
|
-
* 方式 1: 同步初始化 (标准用法)
|
|
97
|
-
* 适用于语言包体量较小,或不介意首屏全量包含翻译数据的场景。
|
|
98
|
-
*/
|
|
144
|
+
// 1. 同步初始化 (标准用法)
|
|
99
145
|
nlsAdapter.init('zh-hans');
|
|
100
146
|
|
|
101
|
-
|
|
102
|
-
* 方式 2: 异步初始化 (性能优化推荐)
|
|
103
|
-
* 利用 Webpack/Vite 的动态 import 实现分包,仅加载当前所需语言。
|
|
104
|
-
*/
|
|
147
|
+
// 2. 异步初始化 (分包懒加载)
|
|
105
148
|
// await nlsAdapter.initAsync('zh-hans');
|
|
106
149
|
|
|
107
|
-
|
|
108
|
-
|
|
109
|
-
*/
|
|
110
|
-
// nlsAdapter.init(); // 同步
|
|
111
|
-
// await nlsAdapter.initAsync(); // 异步
|
|
112
|
-
|
|
113
|
-
import * as monaco from 'monaco-editor';
|
|
150
|
+
// 3. 获取当前语言
|
|
151
|
+
console.log(nlsAdapter.getCurrentLocale()); // 'zh-hans'
|
|
114
152
|
|
|
115
|
-
|
|
116
|
-
|
|
117
|
-
|
|
153
|
+
// 4. 使用自定义翻译数据 (不使用内置语言包)
|
|
154
|
+
/*
|
|
155
|
+
nlsAdapter.setMessages({
|
|
156
|
+
'vs/editor/common/editorContextKeys': {
|
|
157
|
+
'editor.action.clipboardCopyAction': '复制 (Custom)'
|
|
158
|
+
}
|
|
118
159
|
});
|
|
160
|
+
*/
|
|
161
|
+
|
|
162
|
+
import * as monaco from 'monaco-editor';
|
|
163
|
+
// ... 之后正常创建编辑器
|
|
119
164
|
```
|
|
120
165
|
|
|
166
|
+
### 框架集成 (React / Vue)
|
|
167
|
+
|
|
168
|
+
本项目与主流框架完美配合。特别注意:若使用 **React** (@monaco-editor/react),请务必通过 `loader.config({ monaco })` 禁用 CDN 并映射到本地实例。
|
|
169
|
+
|
|
170
|
+
详细的框架集成指南请参考:[框架集成最佳实践 (Examples)](./examples/framework-integration.md)
|
|
171
|
+
|
|
172
|
+
### API 详述
|
|
173
|
+
|
|
174
|
+
| 函数 | 说明 |
|
|
175
|
+
| --- | --- |
|
|
176
|
+
| `init(locale?: string): boolean` | 同步初始化。如果不传参数,尝试探测浏览器语言。注意:此方法会导致构建工具引入全量语言包(或触发全量扫描)。 |
|
|
177
|
+
| `initAsync(locale?: string): Promise<boolean>` | 异步初始化。利用动态 import 拆分语言包。 |
|
|
178
|
+
| `getCurrentLocale(): string` | 获取当前已生效的语言代码。如果是自定义数据,返回 `custom`。 |
|
|
179
|
+
| `setMessages(data: object, locale?: string)` | 手动注入翻译字典。建议配合 `./lite` 入口使用以优化体积。 |
|
|
180
|
+
| `vitePlugin(options?: object)` | Vite 插件函数。 |
|
|
181
|
+
| `loader: string` | Webpack Loader 的绝对路径。 |
|
|
182
|
+
|
|
121
183
|
## 🗂 支持的语言列表
|
|
122
184
|
|
|
123
185
|
| 语言代码 | 对应语言 |
|
package/index.d.ts
CHANGED
|
@@ -1,16 +1,5 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
localize2(path: string, data: any, defaultMessage: string, ...args: any[]): { value: string; original: string };
|
|
4
|
-
setLocaleData(data: Record<string, any>): void;
|
|
5
|
-
getConfiguredDefaultLocale(): string | undefined;
|
|
6
|
-
loadMessageBundle(file: string): (path: string, data: any, defaultMessage: string, ...args: any[]) => string;
|
|
7
|
-
config(opt: any): (file: string) => any;
|
|
8
|
-
create(key: string, data: any): {
|
|
9
|
-
localize: (idx: any, defaultValue: string, ...args: any[]) => string;
|
|
10
|
-
localize2: (idx: any, defaultValue: string, ...args: any[]) => { value: string; original: string };
|
|
11
|
-
getConfiguredDefaultLocale: () => string | undefined;
|
|
12
|
-
};
|
|
13
|
-
}
|
|
1
|
+
import { NLSProxy } from './proxy';
|
|
2
|
+
import monacoNlsPlugin from './vite-plugin';
|
|
14
3
|
|
|
15
4
|
/**
|
|
16
5
|
* 支持的语言代码类型
|
|
@@ -21,18 +10,53 @@ export type LocaleCode =
|
|
|
21
10
|
|
|
22
11
|
/**
|
|
23
12
|
* 初始化 Monaco Editor 的语言包
|
|
24
|
-
* @param locale 语言代码,如 'zh-hans'
|
|
13
|
+
* @param locale 语言代码,如 'zh-hans'
|
|
14
|
+
* @param force 是否强制重新初始化
|
|
15
|
+
* @returns 是否加载成功
|
|
25
16
|
*/
|
|
26
|
-
export function init(locale?: LocaleCode | string):
|
|
17
|
+
export function init(locale?: LocaleCode | string, force?: boolean): boolean;
|
|
27
18
|
|
|
28
19
|
/**
|
|
29
20
|
* 异步初始化 Monaco Editor 的语言包
|
|
30
|
-
* 使用动态 import(),支持 Webpack 分包和懒加载。
|
|
31
21
|
* @param locale 语言代码
|
|
22
|
+
* @param force 是否强制重新加载
|
|
23
|
+
* @returns 是否加载成功
|
|
32
24
|
*/
|
|
33
|
-
export function initAsync(locale?: LocaleCode | string): Promise<
|
|
25
|
+
export function initAsync(locale?: LocaleCode | string, force?: boolean): Promise<boolean>;
|
|
34
26
|
|
|
35
27
|
/**
|
|
36
|
-
*
|
|
28
|
+
* 获取当前已生效的语言代码名称
|
|
29
|
+
*/
|
|
30
|
+
export function getLocaleName(): string;
|
|
31
|
+
|
|
32
|
+
/**
|
|
33
|
+
* 获取当前加载的原始翻译数据
|
|
34
|
+
*/
|
|
35
|
+
export function getLocaleData(): Record<string, any> | null;
|
|
36
|
+
|
|
37
|
+
/**
|
|
38
|
+
* 手动设置翻译数据字典
|
|
39
|
+
* @param messages 符合 Monaco NLS 格式的 JSON 对象
|
|
40
|
+
* @param locale 语言代码 (可选标识符)
|
|
41
|
+
*/
|
|
42
|
+
export function setMessages(messages: Record<string, any>, locale?: string): void;
|
|
43
|
+
|
|
44
|
+
/**
|
|
45
|
+
* NLS 代理对象声明
|
|
37
46
|
*/
|
|
38
47
|
export const proxy: NLSProxy;
|
|
48
|
+
|
|
49
|
+
/**
|
|
50
|
+
* Vite 插件导出
|
|
51
|
+
*/
|
|
52
|
+
export const vitePlugin: typeof monacoNlsPlugin;
|
|
53
|
+
|
|
54
|
+
/**
|
|
55
|
+
* Webpack Loader 绝对路径导出 (用于 webpack.config.js)
|
|
56
|
+
*/
|
|
57
|
+
export const loader: string;
|
|
58
|
+
|
|
59
|
+
// 重新导出子模块中的类型,方便从主模块引用
|
|
60
|
+
export { NLSProxy } from './proxy';
|
|
61
|
+
export { PluginOptions } from './vite-plugin';
|
|
62
|
+
export { transform } from './transform';
|
package/index.js
CHANGED
|
@@ -1,4 +1,5 @@
|
|
|
1
|
-
const
|
|
1
|
+
const lite = require('./lite')
|
|
2
|
+
const proxy = lite.proxy
|
|
2
3
|
|
|
3
4
|
/**
|
|
4
5
|
* 语言映射字典,用于将常见的浏览器语言代码还原为本包中支持的代码。
|
|
@@ -11,63 +12,99 @@ const LOCALE_MAP = {
|
|
|
11
12
|
'zh-hk': 'zh-hant',
|
|
12
13
|
'en-us': 'en',
|
|
13
14
|
'en-gb': 'en',
|
|
14
|
-
'en': 'en'
|
|
15
|
+
'en': 'en',
|
|
16
|
+
'ja-jp': 'ja',
|
|
17
|
+
'ja': 'ja',
|
|
18
|
+
'ko-kr': 'ko',
|
|
19
|
+
'ko': 'ko',
|
|
20
|
+
'de-de': 'de',
|
|
21
|
+
'de': 'de',
|
|
22
|
+
'fr-fr': 'fr',
|
|
23
|
+
'fr': 'fr',
|
|
24
|
+
'es-es': 'es',
|
|
25
|
+
'es': 'es',
|
|
26
|
+
'it-it': 'it',
|
|
27
|
+
'it': 'it',
|
|
28
|
+
'ru-ru': 'ru',
|
|
29
|
+
'ru': 'ru',
|
|
30
|
+
'pt-br': 'pt-br'
|
|
15
31
|
}
|
|
16
32
|
|
|
33
|
+
let IS_LOADING = false
|
|
34
|
+
|
|
17
35
|
/**
|
|
18
36
|
* 使用指定的语言代码初始化 Monaco Editor 的本地化。
|
|
19
|
-
*
|
|
20
|
-
* @param {string} locale 语言代码
|
|
37
|
+
* 此方法使用同步 require,会触发构建工具打包所有相关 JSON。
|
|
38
|
+
* @param {string} locale 语言代码
|
|
39
|
+
* @param {boolean} force 是否强制重新初始化
|
|
21
40
|
*/
|
|
22
|
-
function init(locale) {
|
|
23
|
-
|
|
24
|
-
if (!locale &&
|
|
41
|
+
function init(locale, force = false) {
|
|
42
|
+
const isBrowser = typeof window !== 'undefined' && typeof navigator !== 'undefined'
|
|
43
|
+
if (!locale && isBrowser) {
|
|
25
44
|
locale = navigator.language || navigator.userLanguage
|
|
26
45
|
}
|
|
27
46
|
|
|
28
|
-
// 小写化并查找映射
|
|
29
47
|
let targetLocale = (locale || 'zh-hans').toLowerCase()
|
|
30
48
|
if (LOCALE_MAP[targetLocale]) {
|
|
31
49
|
targetLocale = LOCALE_MAP[targetLocale]
|
|
32
50
|
}
|
|
33
51
|
|
|
52
|
+
if (lite.getCurrentLocale() === targetLocale && !force) {
|
|
53
|
+
return true
|
|
54
|
+
}
|
|
55
|
+
|
|
34
56
|
try {
|
|
57
|
+
// 这里的动态 require 是导致全量打包的主要原因,若需精简请改用 lite.setMessages()
|
|
35
58
|
const data = require(`./locales/${targetLocale}.json`)
|
|
36
|
-
|
|
59
|
+
lite.setMessages(data, targetLocale)
|
|
60
|
+
return true
|
|
37
61
|
} catch (e) {
|
|
38
|
-
|
|
62
|
+
if (isBrowser) {
|
|
63
|
+
console.warn(`[monaco-editor-nls-adapter] 无法加载本地语言包: ${targetLocale}`, e)
|
|
64
|
+
}
|
|
65
|
+
return false
|
|
39
66
|
}
|
|
40
67
|
}
|
|
41
68
|
|
|
42
69
|
/**
|
|
43
|
-
*
|
|
44
|
-
*
|
|
45
|
-
* @param {
|
|
46
|
-
* @returns {Promise<void>}
|
|
70
|
+
* 异步加载语言包。
|
|
71
|
+
* @param {string} locale 语言代码
|
|
72
|
+
* @param {boolean} force 是否强制重新初始化
|
|
47
73
|
*/
|
|
48
|
-
async function initAsync(locale) {
|
|
49
|
-
|
|
50
|
-
if (!locale &&
|
|
74
|
+
async function initAsync(locale, force = false) {
|
|
75
|
+
const isBrowser = typeof window !== 'undefined' && typeof navigator !== 'undefined'
|
|
76
|
+
if (!locale && isBrowser) {
|
|
51
77
|
locale = navigator.language || navigator.userLanguage
|
|
52
78
|
}
|
|
53
79
|
|
|
54
|
-
// 小写化并查找映射
|
|
55
80
|
let targetLocale = (locale || 'zh-hans').toLowerCase()
|
|
56
81
|
if (LOCALE_MAP[targetLocale]) {
|
|
57
82
|
targetLocale = LOCALE_MAP[targetLocale]
|
|
58
83
|
}
|
|
59
84
|
|
|
85
|
+
if (lite.getCurrentLocale() === targetLocale && !force) {
|
|
86
|
+
return true
|
|
87
|
+
}
|
|
88
|
+
|
|
89
|
+
if (IS_LOADING && !force) return false
|
|
90
|
+
IS_LOADING = true
|
|
91
|
+
|
|
60
92
|
try {
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
93
|
+
const module = await import(/* webpackChunkName: "nls-[request]" */ `./locales/${targetLocale}.json`)
|
|
94
|
+
lite.setMessages(module.default || module, targetLocale)
|
|
95
|
+
return true
|
|
64
96
|
} catch (e) {
|
|
65
|
-
|
|
97
|
+
if (isBrowser) {
|
|
98
|
+
console.warn(`[monaco-editor-nls-adapter] 无法异步加载语言包: ${targetLocale}`, e)
|
|
99
|
+
}
|
|
100
|
+
return false
|
|
101
|
+
} finally {
|
|
102
|
+
IS_LOADING = false
|
|
66
103
|
}
|
|
67
104
|
}
|
|
68
105
|
|
|
69
106
|
module.exports = {
|
|
107
|
+
...lite,
|
|
70
108
|
init: init,
|
|
71
|
-
initAsync: initAsync
|
|
72
|
-
proxy: proxy
|
|
109
|
+
initAsync: initAsync
|
|
73
110
|
}
|
package/lite.js
ADDED
|
@@ -0,0 +1,27 @@
|
|
|
1
|
+
const proxy = require('./proxy')
|
|
2
|
+
|
|
3
|
+
/**
|
|
4
|
+
* 获取当前已生效的语言代码
|
|
5
|
+
*/
|
|
6
|
+
function getCurrentLocale() {
|
|
7
|
+
return proxy.getLocaleName()
|
|
8
|
+
}
|
|
9
|
+
|
|
10
|
+
/**
|
|
11
|
+
* 手动设置翻译数据字典
|
|
12
|
+
* @param {Object} data 符合 Monaco NLS 格式的 JSON 对象
|
|
13
|
+
* @param {string} locale 语言代码标识 (可选)
|
|
14
|
+
*/
|
|
15
|
+
function setMessages(data, locale = 'custom') {
|
|
16
|
+
proxy.setLocaleData(data, locale)
|
|
17
|
+
}
|
|
18
|
+
|
|
19
|
+
module.exports = {
|
|
20
|
+
getCurrentLocale: getCurrentLocale,
|
|
21
|
+
setMessages: setMessages,
|
|
22
|
+
setLocaleData: proxy.setLocaleData,
|
|
23
|
+
proxy: proxy,
|
|
24
|
+
// 插件导出 (仍保留,以便用户可以从 lite 引用所有工具)
|
|
25
|
+
vitePlugin: require('./vite-plugin'),
|
|
26
|
+
loader: __dirname + '/loader.js'
|
|
27
|
+
}
|
package/loader.js
CHANGED
|
@@ -4,8 +4,27 @@ const { transform } = require('./transform')
|
|
|
4
4
|
* Webpack Loader for Monaco Editor NLS Adapter
|
|
5
5
|
*/
|
|
6
6
|
module.exports = function (source) {
|
|
7
|
+
// 极致性能优化:第一层极速过滤
|
|
8
|
+
if (!this.resourcePath.endsWith('.js') || this.resourcePath.indexOf('monaco-editor') === -1) {
|
|
9
|
+
return source
|
|
10
|
+
}
|
|
11
|
+
|
|
7
12
|
if (this.cacheable) this.cacheable()
|
|
8
13
|
|
|
14
|
+
// 获取 loader options
|
|
15
|
+
const options = (typeof this.getOptions === 'function') ? this.getOptions() : this.query
|
|
16
|
+
|
|
9
17
|
// 使用共享的 transform 逻辑
|
|
10
|
-
|
|
18
|
+
const result = transform(source, this.resourcePath, options || {})
|
|
19
|
+
|
|
20
|
+
// 如果返回包含 SourceMap 的对象,则使用 this.callback 提示 Webpack
|
|
21
|
+
if (result && typeof result === 'object') {
|
|
22
|
+
if (this.callback) {
|
|
23
|
+
this.callback(null, result.code, result.map)
|
|
24
|
+
return
|
|
25
|
+
}
|
|
26
|
+
return result.code
|
|
27
|
+
}
|
|
28
|
+
|
|
29
|
+
return result
|
|
11
30
|
}
|