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/LICENSE ADDED
@@ -0,0 +1,21 @@
1
+ MIT License
2
+
3
+ Copyright (c) 2026
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in all
13
+ copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21
+ SOFTWARE.
package/README.md ADDED
@@ -0,0 +1,134 @@
1
+ # monaco-editor-nls-adapter
2
+
3
+ [![npm version](https://img.shields.io/npm/v/monaco-editor-nls-adapter.svg)](https://www.npmjs.com/package/monaco-editor-nls-adapter)
4
+ [![License](https://img.shields.io/npm/l/monaco-editor-nls-adapter.svg)](https://github.com/your-username/monaco-editor-nls-adapter/blob/main/LICENSE)
5
+
6
+ Multi-language NLS adapter for Monaco Editor 0.50.0+ (Self-hosted). Bridge the gap for localization in modern building environments.
7
+
8
+ [简体中文](./README_zh.md) | English
9
+
10
+ ---
11
+
12
+ ## 🌟 Features
13
+
14
+ - **Self-hosted Locales**: No external CDN dependencies; all language data is bundled locally.
15
+ - **Monaco 0.50.0+ Ready**: Fully compatible with the latest internal NLS signatures of Monaco Editor.
16
+ - **Zero-Config Lazy Loading**: Support for asynchronous initialization with Webpack/Vite chunk splitting.
17
+ - **TypeScript Support**: First-class TS definitions for an excellent developer experience.
18
+ - **Smart Detection**: Automatically detect browser language or manually switch as needed.
19
+ - **Cross-Bundler**: Native support for **Webpack** (Loader) and **Vite/Rollup** (Plugin).
20
+
21
+ ## 🚀 Installation
22
+
23
+ ```bash
24
+ npm install monaco-editor-nls-adapter
25
+ ```
26
+
27
+ ## 🛠 Configuration
28
+
29
+ ### 1. Webpack (Loader)
30
+
31
+ In your `webpack.config.js`, add the adapter's loader to process Monaco Editor's ESM files.
32
+
33
+ ```javascript
34
+ module.exports = {
35
+ module: {
36
+ rules: [
37
+ {
38
+ test: /\.js$/,
39
+ // Crucial: Only process monaco-editor core files to avoid side effects
40
+ include: /node_modules[\\/]monaco-editor[\\/]esm/,
41
+ use: [
42
+ {
43
+ loader: 'monaco-editor-nls-adapter/loader'
44
+ }
45
+ ]
46
+ }
47
+ ]
48
+ }
49
+ }
50
+ ```
51
+
52
+ ### 2. Vite / Rollup (Plugin)
53
+
54
+ In your `vite.config.js` or `vite.config.ts`:
55
+
56
+ ```javascript
57
+ import { defineConfig } from 'vite';
58
+ import monacoNlsPlugin from 'monaco-editor-nls-adapter/vite-plugin';
59
+
60
+ export default defineConfig({
61
+ plugins: [
62
+ monacoNlsPlugin()
63
+ ]
64
+ });
65
+ ```
66
+
67
+ ## 📖 Usage
68
+
69
+ ### Initialization
70
+
71
+ Call `init` or `initAsync` **before** importing `monaco-editor` or creating any editor instances.
72
+
73
+ ```typescript
74
+ import * as nlsAdapter from 'monaco-editor-nls-adapter';
75
+
76
+ /**
77
+ * Option 1: Synchronous (Standard)
78
+ * Recommended for small clusters or if you don't mind the initial bundle size.
79
+ */
80
+ nlsAdapter.init('zh-hans');
81
+
82
+ /**
83
+ * Option 2: Asynchronous (Performance Optimization)
84
+ * Leverages Webpack/Vite dynamic imports for code splitting.
85
+ */
86
+ // await nlsAdapter.initAsync('zh-hans');
87
+
88
+ /**
89
+ * Option 3: Automatic Browser Detection
90
+ */
91
+ // nlsAdapter.init(); // Sync
92
+ // await nlsAdapter.initAsync(); // Async
93
+
94
+ import * as monaco from 'monaco-editor';
95
+
96
+ monaco.editor.create(document.getElementById('container'), {
97
+ value: 'console.log("Hello Localization!");',
98
+ language: 'javascript'
99
+ });
100
+ ```
101
+
102
+ ## 🗂 Supported Locales
103
+
104
+ | Code | Language |
105
+ | --- | --- |
106
+ | `zh-hans` | Simplified Chinese (简体中文) |
107
+ | `zh-hant` | Traditional Chinese (繁体中文) |
108
+ | `en` | English |
109
+ | `ja` | Japanese (日本語) |
110
+ | `ko` | Korean (한국어) |
111
+ | `de` | German (Deutsch) |
112
+ | `fr` | French (Français) |
113
+ | `es` | Spanish (Español) |
114
+ | `it` | Italian (Italiano) |
115
+ | `ru` | Russian (Русский) |
116
+ | `pl` | Polish (Polski) |
117
+ | `tr` | Turkish (Türkçe) |
118
+ | `cs` | Czech (Čeština) |
119
+ | `pt-br` | Portuguese - Brazil (Português) |
120
+
121
+ ## ❓ FAQ
122
+
123
+ ### Why do I need this instead of `monaco-editor-nls`?
124
+ Since version 0.50.0, Monaco Editor updated its internal `localize` function signature. Existing plugins often rely on older versions or external CDNs. This adapter provides a clean, self-hosted proxy and a modern build-time transformation for both Webpack and Vite.
125
+
126
+ ## 📄 License
127
+
128
+ MIT
129
+
130
+ ---
131
+
132
+ ## 💖 Credits & Attribution
133
+
134
+ The localized language packs (`.json` files) in the `/locales` directory are sourced from the [monaco-editor-nls](https://github.com/wang12124468/monaco-editor-nls) project. Special thanks to the original authors for their contributions to the community.
package/README_zh.md ADDED
@@ -0,0 +1,137 @@
1
+ # monaco-editor-nls-adapter
2
+
3
+ [![npm version](https://img.shields.io/npm/v/monaco-editor-nls-adapter.svg)](https://www.npmjs.com/package/monaco-editor-nls-adapter)
4
+ [![License](https://img.shields.io/npm/l/monaco-editor-nls-adapter.svg)](https://github.com/your-username/monaco-editor-nls-adapter/blob/main/LICENSE)
5
+
6
+ 针对 Monaco Editor 0.50.0 及以上版本的多语言本地化适配器。专为现代化构建工具(Webpack, Vite)设计的自托管解决方案。
7
+
8
+ 简体中文 | [English](./README.md)
9
+
10
+ ---
11
+
12
+ ## 🌟 特性
13
+
14
+ - **自托管语言包**: 无需依赖外部 CDN;所有语言数据在本地打包。
15
+ - **适配 Monaco 0.50.0+**: 完全兼容 Monaco Editor 最新的内部 NLS 调用签名。
16
+ - **性能优化 (懒加载)**: 通过 Webpack/Vite 的动态导入实现按需分包加载。
17
+ - **TypeScript 支持**: 完善的类型定义,为开发提供极佳的智能感知。
18
+ - **智能语言识别**: 支持自动识别浏览器语言或手动切换。
19
+ - **跨构建工具支持**: 原生支持 **Webpack** (Loader) 以及 **Vite/Rollup** (Plugin)。
20
+
21
+ ## 🚀 安装
22
+
23
+ ```bash
24
+ npm install monaco-editor-nls-adapter
25
+ ```
26
+
27
+ ## 🛠 配置
28
+
29
+ ### 1. Webpack (Loader)
30
+
31
+ 在 `webpack.config.js` 中,添加此适配器的 loader 来处理 Monaco Editor 的 ESM 源码。
32
+
33
+ ```javascript
34
+ module.exports = {
35
+ module: {
36
+ rules: [
37
+ {
38
+ test: /\.js$/,
39
+ // 重要:仅处理 monaco-editor 的 esm 目录,避免对业务代码产生副作用
40
+ include: /node_modules[\\/]monaco-editor[\\/]esm/,
41
+ use: [
42
+ {
43
+ loader: 'monaco-editor-nls-adapter/loader'
44
+ }
45
+ ]
46
+ }
47
+ ]
48
+ }
49
+ }
50
+ ```
51
+
52
+ ### 2. Vite / Rollup (Plugin)
53
+
54
+ 在 `vite.config.js` 中,添加此插件:
55
+
56
+ ```javascript
57
+ import { defineConfig } from 'vite';
58
+ import monacoNlsPlugin from 'monaco-editor-nls-adapter/vite-plugin';
59
+
60
+ export default defineConfig({
61
+ plugins: [
62
+ monacoNlsPlugin()
63
+ ]
64
+ });
65
+ ```
66
+
67
+ ## 📖 使用指南
68
+
69
+ ### 初始化
70
+
71
+ 在导入 `monaco-editor` 或创建任何实例**之前**,先调用 `init` 或 `initAsync`。
72
+
73
+ ```typescript
74
+ import * as nlsAdapter from 'monaco-editor-nls-adapter';
75
+
76
+ /**
77
+ * 方式 1: 同步初始化 (标准用法)
78
+ * 适用于语言包体量较小,或不介意首屏全量包含翻译数据的场景。
79
+ */
80
+ nlsAdapter.init('zh-hans');
81
+
82
+ /**
83
+ * 方式 2: 异步初始化 (性能优化推荐)
84
+ * 利用 Webpack/Vite 的动态 import 实现分包,仅加载当前所需语言。
85
+ */
86
+ // await nlsAdapter.initAsync('zh-hans');
87
+
88
+ /**
89
+ * 方式 3: 自动识别浏览器语言
90
+ */
91
+ // nlsAdapter.init(); // 同步
92
+ // await nlsAdapter.initAsync(); // 异步
93
+
94
+ import * as monaco from 'monaco-editor';
95
+
96
+ monaco.editor.create(document.getElementById('container'), {
97
+ value: 'console.log("Hello Localization!");',
98
+ language: 'javascript'
99
+ });
100
+ ```
101
+
102
+ ## 🗂 支持的语言列表
103
+
104
+ | 语言代码 | 对应语言 |
105
+ | --- | --- |
106
+ | `zh-hans` | 简体中文 |
107
+ | `zh-hant` | 繁体中文 |
108
+ | `en` | 英语 (English) |
109
+ | `ja` | 日本语 (Japanese) |
110
+ | `ko` | 韩语 (Korean) |
111
+ | `de` | 德语 (German) |
112
+ | `fr` | 法语 (French) |
113
+ | `es` | 西班牙语 (Spanish) |
114
+ | `it` | 意大利语 (Italian) |
115
+ | `ru` | 俄语 (Russian) |
116
+ | `pl` | 波兰语 (Polish) |
117
+ | `tr` | 土耳其语 (Turkish) |
118
+ | `cs` | 捷克语 (Czech) |
119
+ | `pt-br` | 葡萄牙语 - 巴西 (Portuguese) |
120
+
121
+ ## ❓ 常见问题 (FAQ)
122
+
123
+ ### 为什么选择此适配器而不是 `monaco-editor-nls`?
124
+ 自 0.50.0 版本起,Monaco Editor 更新了内部 `localize` 函数的签名。现有许多插件依赖于旧版本或外部 CDN。此适配器提供了一个完全自托管的代理和针对最新版本的现代转换逻辑。
125
+
126
+ ### 它能配合 `@monaco-editor/react` 使用吗?
127
+ 可以。请先在您的构建工具(Webpack/Vite)中配置好插件/Loader,然后在应用入口处生命周期(通常是根组件渲染前)调用 `nlsAdapter.init()` 即可。
128
+
129
+ ## 📄 许可证
130
+
131
+ MIT
132
+
133
+ ---
134
+
135
+ ## 💖 致谢与归属声明
136
+
137
+ `/locales` 目录中的语言包(`.json` 文件)来源于 [monaco-editor-nls](https://github.com/wang12124468/monaco-editor-nls) 项目。在此对原作者及社区的贡献表示衷心的感谢。
package/index.d.ts ADDED
@@ -0,0 +1,38 @@
1
+ export interface NLSProxy {
2
+ localize(path: string, data: any, defaultMessage: string, ...args: any[]): string;
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
+ }
14
+
15
+ /**
16
+ * 支持的语言代码类型
17
+ */
18
+ export type LocaleCode =
19
+ | 'zh-hans' | 'zh-cn' | 'zh-hant' | 'zh-tw'
20
+ | 'en' | 'ja' | 'ko' | 'de' | 'fr' | 'es' | 'it' | 'ru' | 'pl' | 'tr' | 'cs' | 'pt-br';
21
+
22
+ /**
23
+ * 初始化 Monaco Editor 的语言包
24
+ * @param locale 语言代码,如 'zh-hans'。如果不传,将尝试检测浏览器语言。
25
+ */
26
+ export function init(locale?: LocaleCode | string): void;
27
+
28
+ /**
29
+ * 异步初始化 Monaco Editor 的语言包
30
+ * 使用动态 import(),支持 Webpack 分包和懒加载。
31
+ * @param locale 语言代码
32
+ */
33
+ export function initAsync(locale?: LocaleCode | string): Promise<void>;
34
+
35
+ /**
36
+ * NLS 代理对象,由 loader 注入到 Monaco 源码中
37
+ */
38
+ export const proxy: NLSProxy;
package/index.js ADDED
@@ -0,0 +1,73 @@
1
+ const proxy = require('./proxy')
2
+
3
+ /**
4
+ * 语言映射字典,用于将常见的浏览器语言代码还原为本包中支持的代码。
5
+ */
6
+ const LOCALE_MAP = {
7
+ 'zh-cn': 'zh-hans',
8
+ 'zh-hans': 'zh-hans',
9
+ 'zh-tw': 'zh-hant',
10
+ 'zh-hant': 'zh-hant',
11
+ 'zh-hk': 'zh-hant',
12
+ 'en-us': 'en',
13
+ 'en-gb': 'en',
14
+ 'en': 'en'
15
+ }
16
+
17
+ /**
18
+ * 使用指定的语言代码初始化 Monaco Editor 的本地化。
19
+ * 此方法会从本地 /locales 目录加载 JSON 数据。
20
+ * @param {string} locale 语言代码 (例如 'zh-hans', 'ja', 'ko')。如果不传,将尝试检测浏览器语言。
21
+ */
22
+ function init(locale) {
23
+ // 如果没有传递 locale,尝试从浏览器环境获取
24
+ if (!locale && typeof navigator !== 'undefined') {
25
+ locale = navigator.language || navigator.userLanguage
26
+ }
27
+
28
+ // 小写化并查找映射
29
+ let targetLocale = (locale || 'zh-hans').toLowerCase()
30
+ if (LOCALE_MAP[targetLocale]) {
31
+ targetLocale = LOCALE_MAP[targetLocale]
32
+ }
33
+
34
+ try {
35
+ const data = require(`./locales/${targetLocale}.json`)
36
+ proxy.setLocaleData(data)
37
+ } catch (e) {
38
+ console.error(`[monaco-editor-nls-adapter] 无法加载本地语言包: ${targetLocale}。回退到默认设置。`, e)
39
+ }
40
+ }
41
+
42
+ /**
43
+ * 异步初始化 Monaco Editor 的本地化。
44
+ * 使用动态 import(),允许 Webpack 将语言包拆分为独立的 chunk,实现按需加载。
45
+ * @param {string} locale 语言代码。如果不传,将尝试检测浏览器语言。
46
+ * @returns {Promise<void>}
47
+ */
48
+ async function initAsync(locale) {
49
+ // 如果没有传递 locale,尝试从浏览器环境获取
50
+ if (!locale && typeof navigator !== 'undefined') {
51
+ locale = navigator.language || navigator.userLanguage
52
+ }
53
+
54
+ // 小写化并查找映射
55
+ let targetLocale = (locale || 'zh-hans').toLowerCase()
56
+ if (LOCALE_MAP[targetLocale]) {
57
+ targetLocale = LOCALE_MAP[targetLocale]
58
+ }
59
+
60
+ try {
61
+ // 使用动态 import 实现懒加载
62
+ const module = await import(`./locales/${targetLocale}.json`)
63
+ proxy.setLocaleData(module.default || module)
64
+ } catch (e) {
65
+ console.error(`[monaco-editor-nls-adapter] 无法异步加载语言包: ${targetLocale}。`, e)
66
+ }
67
+ }
68
+
69
+ module.exports = {
70
+ init: init,
71
+ initAsync: initAsync,
72
+ proxy: proxy
73
+ }
package/loader.js ADDED
@@ -0,0 +1,11 @@
1
+ const { transform } = require('./transform')
2
+
3
+ /**
4
+ * Webpack Loader for Monaco Editor NLS Adapter
5
+ */
6
+ module.exports = function (source) {
7
+ if (this.cacheable) this.cacheable()
8
+
9
+ // 使用共享的 transform 逻辑
10
+ return transform(source, this.resourcePath)
11
+ }