Skip to content

Commit e6caf92

Browse files
committed
chore: upgrade monaco-editor@0.54.0
1 parent f2e01d3 commit e6caf92

14 files changed

Lines changed: 9189 additions & 1555 deletions

.github/workflows/CI.yml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,9 @@ name: CI
22

33
on:
44
push:
5-
branches: ["master"]
5+
branches: ["main"]
66
pull_request:
7-
branches: ["master"]
7+
branches: ["main"]
88

99
jobs:
1010
build:

.npmignore

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
11
.github
2+
.vscode
23
src
34
tsconfig.json
5+
*.tgz

README-zh_CN.md

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -9,9 +9,10 @@
99

1010
## 版本差异
1111

12-
`2.x` `1.x` 的主要区别在于 `monaco-editor` 的版本。
12+
`3.x`, `2.x`, `1.x` 的主要区别在于 `monaco-editor` 的版本。
1313

14-
- 2.x 版本:当你的产品需要支持国际化时(产品页面支持切换界面语言),可以使用 [2.x](https://github.com/DTStack/monaco-editor-i18n-plugin/tree/master) 的版本,已验证 monaco-editor `0.52.2` 版本。
14+
- 3.x 版本:当你的产品需要支持国际化时(产品页面支持切换界面语言),可以使用 [3.x](https://github.com/DTStack/monaco-editor-i18n-plugin/tree/main) 的版本,已验证 monaco-editor `0.54.0` 版本。
15+
- 2.x 版本:当你的产品需要支持国际化时(产品页面支持切换界面语言),可以使用 [2.x](https://github.com/DTStack/monaco-editor-i18n-plugin/tree/2.x-stable) 的版本,已验证 monaco-editor `0.52.2` 版本。
1516
- 1.x 版本:当你的产品需要支持国际化时(产品页面支持切换界面语言),请使用 [1.x](https://github.com/DTStack/monaco-editor-i18n-plugin/tree/1.x-stable) 的版本,已验证 monaco-editor `0.30.1``0.31.1` 版本。
1617
- 0.x 版本:当你的产品只有一种界面语言时(不需要支持国际化,如仅支持中文),请使用 [0.x](https://github.com/DTStack/monaco-editor-i18n-plugin/tree/0.x-stable) 的版本,已验证 monaco-editor `0.30.1``0.31.1` 版本。
1718

@@ -25,15 +26,15 @@
2526

2627
### 简体中文(精简版)
2728

28-
使用到的 `src/locale/dt-zh-hans.json` 是基于 [vscode-loc/i18n/zh-hans](https://github.com/microsoft/vscode-loc/blob/v1.96.2024121109/i18n/vscode-language-pack-zh-hans/translations/main.i18n.json) 精简的。
29+
使用到的 `src/locale/dt-zh-hans.json` 是基于 [vscode-loc/i18n/zh-hans](https://github.com/microsoft/vscode-loc/blob/main/i18n/vscode-language-pack-zh-hans/translations/main.i18n.json) 精简的。
2930

3031
### 简体中文(完整版)
3132

32-
使用到的 `src/locale/zh-hans.json` 来源为 [vscode-loc/i18n/zh-hans](https://github.com/microsoft/vscode-loc/blob/v1.96.2024121109/i18n/vscode-language-pack-zh-hans/translations/main.i18n.json)
33+
使用到的 `src/locale/zh-hans.json` 来源为 [vscode-loc/i18n/zh-hans](https://github.com/microsoft/vscode-loc/blob/main/i18n/vscode-language-pack-zh-hans/translations/main.i18n.json)
3334

3435
### 自定义语言
3536

36-
如果你想使用其他语言或者精简后的 `src/locale/dt-zh-hans.json` 文件不能满足要求,你可以在 [vscode-loc/i18n](https://github.com/microsoft/vscode-loc/tree/v1.96.2024121109/i18n) 找到其他语言的 JSON 文件,放入你的工程文件夹下以便使用自定义路径。
37+
如果你想使用其他语言或者精简后的 `src/locale/dt-zh-hans.json` 文件不能满足要求,你可以在 [vscode-loc/i18n](https://github.com/microsoft/vscode-loc/tree/main/i18n) 找到其他语言的 JSON 文件,放入你的工程文件夹下以便使用自定义路径。
3738

3839

3940
## 使用示例
@@ -113,6 +114,6 @@ setLocale('dt-zh-hans');
113114

114115
## 注意事项
115116

116-
- 目前验证的 `monaco-editor` 版本为 `0.52.2`
117-
- 对应的 `vscode-loc` 版本为 `1.91.1`
117+
- 目前验证的 `monaco-editor` 版本为 `0.54.0`
118+
- 对应的 `vscode-loc` 版本为 `main`
118119
- 如果自定义语言不生效,可能是上述两个包的版本没有对应,`vscode-loc` 不同版本的 JSON 文件结构是有变化的(如 1.63.3 和 1.91.1),其他版本请自行验证。

README.md

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -4,14 +4,15 @@
44

55
English | [简体中文](./README-zh_CN.md)
66

7-
The purpose of this plugin is to set the interface language when using `monaco-editor`. The main implementation idea is to override the `monaco-editor/esm/vs/nls.js` file to achieve the effect of interface language switching.
7+
The purpose of this plugin is to set the interface language when using `monaco-editor`. The main implementation idea is to override the `monaco-editor/esm/vs/nls.messages.js` file to achieve the effect of interface language switching.
88

99

1010
## Version difference
1111

12-
The main difference between `2.x` and `1.x` lies in the version of `monaco-editor`.
12+
The main difference between `3.x`, `2.x` and `1.x` lies in the version of `monaco-editor`.
1313

14-
- The 2.x: When your product needs to support internationalization (the product page supports switching interface languages), please use version [2.x](https://github.com/DTStack/monaco-editor-i18n-plugin/tree/master). It has been verified with `monaco-editor` versions `0.52.2`.
14+
- The 3.x: When your product needs to support internationalization (the product page supports switching interface languages), please use version [3.x](https://github.com/DTStack/monaco-editor-i18n-plugin/tree/main). It has been verified with `monaco-editor` versions `0.54.0`.
15+
- The 2.x: When your product needs to support internationalization (the product page supports switching interface languages), please use version [2.x](https://github.com/DTStack/monaco-editor-i18n-plugin/tree/2.x-stable). It has been verified with `monaco-editor` versions `0.52.2`.
1516
- The 1.x: When your product needs to support internationalization (the product page supports switching interface languages), please use version [1.x](https://github.com/DTStack/monaco-editor-i18n-plugin/tree/1.x-stable). It has been verified with `monaco-editor` versions `0.30.1` and `0.31.1`.
1617
- The 0.x: When your product only has one interface language (does not need to support internationalization, such as only supporting Chinese), please use version [0.x](https://github.com/DTStack/monaco-editor-i18n-plugin/tree/0.x-stable). It has been verified with `monaco-editor` versions `0.30.1` and `0.31.1`.
1718

@@ -25,16 +26,16 @@ The main difference between `2.x` and `1.x` lies in the version of `monaco-edito
2526

2627
### dt-zh-hans (Simplified version for dtstack)
2728

28-
The used `src/locale/dt-zh-hans.json` is lite based on [vscode-loc/i18n/zh-hans](https://github.com/microsoft/vscode-loc/blob/v1.96.2024121109/i18n/vscode-language-pack-zh-hans/translations/main.i18n.json).
29+
The used `src/locale/dt-zh-hans.json` is lite based on [vscode-loc/i18n/zh-hans](https://github.com/microsoft/vscode-loc/blob/main/i18n/vscode-language-pack-zh-hans/translations/main.i18n.json).
2930

3031
### Simplified Chinese (Full version)
3132

32-
The used `src/locale/zh-hans.json` is from [vscode-loc/i18n/zh-hans](https://github.com/microsoft/vscode-loc/blob/v1.96.2024121109/i18n/vscode-language-pack-zh-hans/translations/main.i18n.json)
33+
The used `src/locale/zh-hans.json` is from [vscode-loc/i18n/zh-hans](https://github.com/microsoft/vscode-loc/blob/main/i18n/vscode-language-pack-zh-hans/translations/main.i18n.json)
3334

3435

3536
### custom languages
3637

37-
If you want to use another languages or if the simplified `src/scale/dt-zh-hans.json` file does not meet the requirements, you can find JSON files in other languages in [vscode-loc/i18n](https://github.com/microsoft/vscode-loc/tree/v1.96.2024121109/i18n), and place them in your project folder for custom path usage.
38+
If you want to use another languages or if the simplified `src/scale/dt-zh-hans.json` file does not meet the requirements, you can find JSON files in other languages in [vscode-loc/i18n](https://github.com/microsoft/vscode-loc/tree/main/i18n), and place them in your project folder for custom path usage.
3839

3940

4041
## Example usage
@@ -114,6 +115,6 @@ setLocale('dt-zh-hans');
114115

115116
## Notice
116117

117-
- The currently verified versions of `monaco-editor` are `0.52.2`.
118-
- The corresponding version of `vscode doc` is `1.91.1`.
118+
- The currently verified versions of `monaco-editor` are `0.54.0`.
119+
- The corresponding version of `vscode-loc` is `main`.
119120
- If custom language doesn't work, it may be due to a mismatch between the versions of the two packages. The JSON file structure in later versions of `vscode-loc` has changed (like `1.63.3` and `1.91.1`), Please verify other versions yourself.

package.json

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "monaco-editor-i18n-plugin",
3-
"version": "2.0.2",
3+
"version": "3.0.0",
44
"description": "A i18n plugin for the Monaco editor",
55
"main": "out/index.js",
66
"typings": "./out/index.d.ts",
@@ -25,7 +25,7 @@
2525
"webpack": "^5.68.0"
2626
},
2727
"peerDependencies": {
28-
"monaco-editor": "0.52.2",
28+
"monaco-editor": "0.54.0",
2929
"webpack": "^5"
3030
},
3131
"keywords": [

pnpm-lock.yaml

Lines changed: 20 additions & 5 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/index.ts

Lines changed: 0 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -5,9 +5,7 @@
55

66
import type * as webpack from "webpack";
77

8-
const replaceNls = require.resolve("./loaders/replaceNls");
98
const replaceNlsMessages = require.resolve("./loaders/replaceNlsMessages");
10-
const replaceLocalizeLoader = require.resolve("./loaders/replaceLocalize");
119

1210
export declare namespace MonacoEditorI18nPlugin {
1311
interface IMonacoEditorI18nPluginOpts {
@@ -33,17 +31,10 @@ function createLoaderRules(options: MonacoEditorI18nPlugin.IMonacoEditorI18nPlug
3331
enforce: "pre",
3432
include: /[\\\/]monaco-editor[\\\/]esm/,
3533
use: [
36-
{
37-
loader: replaceNls,
38-
options,
39-
},
4034
{
4135
loader: replaceNlsMessages,
4236
options,
4337
},
44-
{
45-
loader: replaceLocalizeLoader,
46-
},
4738
],
4839
},
4940
];

src/loaders/replaceLocalize.ts

Lines changed: 0 additions & 27 deletions
This file was deleted.

src/loaders/replaceNls.ts

Lines changed: 0 additions & 25 deletions
This file was deleted.

src/loaders/replaceNlsMessages.ts

Lines changed: 39 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -2,24 +2,55 @@ import type { PitchLoaderDefinitionFunction } from "webpack";
22
import { MonacoEditorI18nPlugin } from "..";
33

44
const fs = require("fs");
5-
const nlsMessages = require.resolve("../nls.messages");
5+
const pathModule = require("path");
66

77
// replace monaco-editor/esm/vs/nls.messages.js
88
const replaceNlsMessages: PitchLoaderDefinitionFunction<MonacoEditorI18nPlugin.IMonacoEditorI18nPluginOpts> = function (
99
content: string
1010
) {
11-
const pathRegExp = /monaco-editor[\\\/]esm[\\\/]vs[\\\/]nls\.message\.js$/;
11+
const pathRegExp = /monaco-editor[\\\/]esm[\\\/]vs[\\\/]nls\.messages\.js$/;
1212
if (!pathRegExp.test(this.resourcePath)) return content;
1313

14-
let _content = fs.readFileSync(nlsMessages, { encoding: "utf8" });
15-
if (_content.includes("windowKey") && _content.includes("localStorageKey")) {
16-
const { windowKey, localStorageKey } = this.getOptions() || {};
14+
// Resolve nls.keys.json from monaco-editor package
15+
const esmDir = this.resourcePath.split(/[\\\/]vs[\\\/]nls\.messages\.js$/)[0];
16+
const nlsKeysPath = pathModule.join(esmDir, "nls.keys.json");
17+
const nlsKeys: [string, string[]][] = JSON.parse(fs.readFileSync(nlsKeysPath, "utf8"));
1718

18-
_content = _content.replace(/const windowKey = \("[^"]*"\);/g, `${windowKey};`);
19-
_content = _content.replace(/const localStorageKey = \("[^"]*"\);/g, `${localStorageKey};`);
19+
// Build flat entries: [[modulePath, key, index], ...]
20+
// nls.keys.json may have duplicate keys (same key at multiple indices),
21+
// so we must track every index, not just the last one.
22+
const entries: [string, string, number][] = [];
23+
let idx = 0;
24+
for (const [modulePath, keys] of nlsKeys) {
25+
for (const key of keys) {
26+
entries.push([modulePath, key, idx++]);
27+
}
2028
}
2129

22-
return _content;
30+
const { windowKey = "__DT_LOCALE__", localStorageKey = "dt-locale" } = this.getOptions() || {};
31+
32+
return `
33+
const _nlsEntries = ${JSON.stringify(entries)};
34+
35+
export function getNLSMessages() {
36+
if (typeof window === "undefined") return undefined;
37+
const locale = window.localStorage.getItem("${localStorageKey}") || "en-US";
38+
const localeData = window["${windowKey}"]?.[locale] || {};
39+
const messages = [];
40+
for (const [modulePath, key, index] of _nlsEntries) {
41+
const val = (localeData[modulePath] || {})[key];
42+
if (val !== undefined) messages[index] = val;
43+
}
44+
return messages;
45+
}
46+
47+
export function getNLSLanguage() {
48+
if (typeof window === "undefined") return undefined;
49+
const locale = window.localStorage.getItem("${localStorageKey}") || "en-US";
50+
const map = { "en-US": "en", "zh-CN": "zh-cn" };
51+
return map[locale];
52+
}
53+
`;
2354
};
2455

2556
module.exports = replaceNlsMessages;

0 commit comments

Comments
 (0)