Skip to content

Commit 89118bb

Browse files
committed
feat: support [email protected] (#2)
* feat: support [email protected] * feat: optimize replace localize2 with file path
1 parent e902f96 commit 89118bb

File tree

15 files changed

+15854
-10343
lines changed

15 files changed

+15854
-10343
lines changed

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,3 +2,4 @@ node_modules
22
out
33

44
.DS_Store
5+
*.tgz

.vscode/settings.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
{}

README-zh_CN.md

Lines changed: 12 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -9,28 +9,31 @@
99

1010
## 版本差异
1111

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

1518

1619
## 安装
1720

18-
`npm install monaco-editor-i18n-plugin -D`
21+
- `npm install monaco-editor-i18n-plugin -D`
1922

2023

2124
## 语言
2225

2326
### 简体中文(精简版)
2427

25-
使用到的 `src/locale/dt-zh-hans.json` 是基于 [vscode-loc/i18n/zh-hans](https://github.com/microsoft/vscode-loc/blob/release/1.63.3/i18n/vscode-language-pack-zh-hans/translations/main.i18n.json) 精简的。
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) 精简的。
2629

2730
### 简体中文(完整版)
2831

29-
使用到的 `src/locale/zh-hans.json` 来源为 [vscode-loc/i18n/zh-hans](https://github.com/microsoft/vscode-loc/blob/release/1.63.3/i18n/vscode-language-pack-zh-hans/translations/main.i18n.json)
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)
3033

3134
### 自定义语言
3235

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

3538

3639
## 使用示例
@@ -110,6 +113,6 @@ setLocale('dt-zh-hans');
110113

111114
## 注意事项
112115

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

README.md

Lines changed: 13 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -9,31 +9,32 @@ The purpose of this plugin is to set the interface language when using `monaco-e
99

1010
## Version difference
1111

12-
- 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).
13-
- The latest: When your product needs to support internationalization (the product page supports switching interface languages), please use the latest version.
12+
The main difference between `2.x` and `1.x` lies in the version of `monaco-editor`.
13+
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`.
15+
- 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`.
16+
- 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`.
1417

1518

1619
## Install
1720

18-
`npm install monaco-editor-i18n-plugin -D`
21+
- `npm install monaco-editor-i18n-plugin -D`
1922

2023

2124
## Languages
2225

2326
### dt-zh-hans (Simplified version for dtstack)
2427

25-
The used `src/locale/dt-zh-hans.json` is lite based on [vscode-loc/i18n/zh-hans](https://github.com/microsoft/vscode-loc/blob/release/1.63.3/i18n/vscode-language-pack-zh-hans/translations/main.i18n.json).
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).
2629

2730
### Simplified Chinese (Full version)
2831

29-
The used `src/locale/zh-hans.json` is from [vscode-loc/i18n/zh-hans](https://github.com/microsoft/vscode-loc/blob/release/1.63.3/i18n/vscode-language-pack-zh-hans/translations/main.i18n.json)
30-
31-
### custom languages
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)
3233

33-
If you want to use another language or if the `src/locale/dt-zh-hans.json` doesn't meet your requirements, you can get another's JSON file from [vscode-loc/i18n](https://github.com/microsoft/vscode-loc/tree/release/1.63.3/i18n).
3434

35+
### custom languages
3536

36-
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/release/1.63.3/i18n), modify them, and place them in your project folder for custom path usage.
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.
3738

3839

3940
## Example usage
@@ -113,6 +114,6 @@ setLocale('dt-zh-hans');
113114

114115
## Notice
115116

116-
- The currently verified versions of `monaco-editor` are `0.30.1` and `0.31.1`.
117-
- The corresponding version of `vscode doc` is `1.63.3`.
118-
- 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, so please verify it by yourself.
117+
- The currently verified versions of `monaco-editor` are `0.52.2`.
118+
- The corresponding version of `vscode doc` is `1.91.1`.
119+
- 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": "1.0.2",
3+
"version": "2.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.30.1 || 0.31.1",
28+
"monaco-editor": "0.52.2",
2929
"webpack": "^5"
3030
},
3131
"keywords": [

pnpm-lock.yaml

Lines changed: 5 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: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
import type * as webpack from "webpack";
77

88
const replaceNls = require.resolve("./loaders/replaceNls");
9+
const replaceNlsMessages = require.resolve("./loaders/replaceNlsMessages");
910
const replaceLocalizeLoader = require.resolve("./loaders/replaceLocalize");
1011

1112
export declare namespace MonacoEditorI18nPlugin {
@@ -36,6 +37,10 @@ function createLoaderRules(options: MonacoEditorI18nPlugin.IMonacoEditorI18nPlug
3637
loader: replaceNls,
3738
options,
3839
},
40+
{
41+
loader: replaceNlsMessages,
42+
options,
43+
},
3944
{
4045
loader: replaceLocalizeLoader,
4146
},

src/loaders/replaceLocalize.ts

Lines changed: 11 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -10,12 +10,18 @@ const replaceLocalize: PitchLoaderDefinitionFunction<MonacoEditorI18nPlugin.IMon
1010
const vsPath = this.resourcePath.split(/monaco-editor[\\\/]esm[\\\/]/).pop();
1111
if (!vsPath) return content;
1212

13-
// add vscode-loc path to function localize
1413
const path = vsPath.replace(/\\/g, "/").replace(".js", "");
15-
return content.replace(/(\bfunction\s+localize\()|(\blocalize\()/g, function (text) {
16-
if (/function\s+localize/.test(text)) return text;
17-
return `localize('${path}', `;
18-
});
14+
15+
// add vscode-loc path to function localize and localize2
16+
return content
17+
.replace(/(\bfunction\s+localize\()|(\blocalize\()/g, function (text) {
18+
if (/function\s+localize/.test(text)) return text;
19+
return `localize('${path}', `;
20+
})
21+
.replace(/(\bfunction\s+localize2\()|(\blocalize2\()/g, function (text) {
22+
if (/function\s+localize2/.test(text)) return text;
23+
return `localize2('${path}', `;
24+
});
1925
};
2026

2127
module.exports = replaceLocalize;

src/loaders/replaceNls.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import { MonacoEditorI18nPlugin } from "..";
44
const fs = require("fs");
55
const nls = require.resolve("../nls");
66

7+
// replace monaco-editor/esm/vs/nls.js
78
const replaceNls: PitchLoaderDefinitionFunction<MonacoEditorI18nPlugin.IMonacoEditorI18nPluginOpts> = function (
89
content: string
910
) {

src/loaders/replaceNlsMessages.ts

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
import type { PitchLoaderDefinitionFunction } from "webpack";
2+
import { MonacoEditorI18nPlugin } from "..";
3+
4+
const fs = require("fs");
5+
const nlsMessages = require.resolve("../nls.messages");
6+
7+
// replace monaco-editor/esm/vs/nls.messages.js
8+
const replaceNlsMessages: PitchLoaderDefinitionFunction<MonacoEditorI18nPlugin.IMonacoEditorI18nPluginOpts> = function (
9+
content: string
10+
) {
11+
const pathRegExp = /monaco-editor[\\\/]esm[\\\/]vs[\\\/]nls\.message\.js$/;
12+
if (!pathRegExp.test(this.resourcePath)) return content;
13+
14+
let _content = fs.readFileSync(nlsMessages, { encoding: "utf8" });
15+
if (_content.includes("windowKey") && _content.includes("localStorageKey")) {
16+
const { windowKey, localStorageKey } = this.getOptions() || {};
17+
18+
_content = _content.replace(/const windowKey = \("[^"]*"\);/g, `${windowKey};`);
19+
_content = _content.replace(/const localStorageKey = \("[^"]*"\);/g, `${localStorageKey};`);
20+
}
21+
22+
return _content;
23+
};
24+
25+
module.exports = replaceNlsMessages;

0 commit comments

Comments
 (0)