monaco-editor-nls-adapter 2.2.0 → 2.2.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.
Files changed (2) hide show
  1. package/codegen.js +37 -0
  2. package/package.json +2 -1
package/codegen.js ADDED
@@ -0,0 +1,37 @@
1
+ /**
2
+ * 生成按需加载语言包的代码片段
3
+ * @param {string[]} languages 语言代码数组
4
+ * @param {boolean} isAsync 是否为异步加载 (ESM/import)
5
+ * @returns {string} 代码片段
6
+ */
7
+ function generateLocalesCode(languages, isAsync) {
8
+ if (!languages || languages.length === 0) {
9
+ return null;
10
+ }
11
+
12
+ if (isAsync) {
13
+ // 异步版本 (用于 initAsync)
14
+ const map = languages.map(lang => ` '${lang}': () => import('./locales/${lang}.json')`).join(',\n');
15
+ return `(async (locale) => {
16
+ const locales = {
17
+ ${map}
18
+ };
19
+ if (locales[locale]) return await locales[locale]();
20
+ throw new Error('[monaco-editor-nls-adapter] Locale not bundled: ' + locale);
21
+ })(targetLocale)`;
22
+ } else {
23
+ // 同步版本 (用于 init)
24
+ const map = languages.map(lang => ` '${lang}': () => require('./locales/${lang}.json')`).join(',\n');
25
+ return `((locale) => {
26
+ const locales = {
27
+ ${map}
28
+ };
29
+ if (locales[locale]) return locales[locale]();
30
+ throw new Error('[monaco-editor-nls-adapter] Locale not bundled: ' + locale);
31
+ })(targetLocale)`;
32
+ }
33
+ }
34
+
35
+ module.exports = {
36
+ generateLocalesCode
37
+ };
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "monaco-editor-nls-adapter",
3
- "version": "2.2.0",
3
+ "version": "2.2.1",
4
4
  "description": "Multi-language NLS adapter for Monaco Editor 0.50.0+ (Self-hosted)",
5
5
  "main": "index.js",
6
6
  "module": "index.js",
@@ -41,6 +41,7 @@
41
41
  "loader.js",
42
42
  "proxy.js",
43
43
  "transform.js",
44
+ "codegen.js",
44
45
  "vite-plugin.js",
45
46
  "locales/",
46
47
  "README.md",