Skip to content

Commit eb453b6

Browse files
committed
feat: get locale data from window object
1 parent ebb04ec commit eb453b6

File tree

5 files changed

+125
-64
lines changed

5 files changed

+125
-64
lines changed

README-zh_CN.md

Lines changed: 52 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -4,18 +4,39 @@
44

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

7-
这个插件的作用是在使用 `monaco-editor` 时可以设置语言,主要实现思路是通过覆写 `monaco-editor/esm/vs/nls.js` 文件来实现语言切换的效果。
7+
这个插件的作用是在使用 `monaco-editor` 时可以设置界面语言,主要实现思路是通过覆写 `monaco-editor/esm/vs/nls.js` 文件来实现界面语言切换的效果。
8+
9+
10+
## 版本差异
11+
12+
- 0.x 版本:当你的产品只有一种界面语言时(不需要支持国际化,如仅支持中文),请使用 [0.x](https://github.com/DTStack/monaco-editor-i18n-plugin/tree/0.x-stable) 的版本。
13+
- 最新版本:当你的产品需要支持国际化时(产品页面支持切换界面语言),请使用最新版本。
14+
815

916
## 安装
1017

1118
`npm install monaco-editor-i18n-plugin -D`
1219

13-
## 使用
20+
21+
## 语言
1422

1523
### 简体中文(精简版)
1624

1725
使用到的 `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) 精简的。
1826

27+
### 简体中文(完整版)
28+
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)
30+
31+
### 自定义语言
32+
33+
如果你想使用其他语言或者精简后的 `src/locale/dt-zh-hans.json` 文件不能满足要求,你可以在 [vscode-loc/i18n](https://github.com/microsoft/vscode-loc/tree/release/1.63.3/i18n) 找到其他语言的 JSON 文件,修改后放入你的工程文件夹下,以便使用自定义路径。
34+
35+
36+
## 使用示例
37+
38+
### 配置项
39+
1940
- `ko.config.js` [ko - Project toolkit for React Applications](https://github.com/DTStack/ko)
2041

2142
```js
@@ -30,7 +51,8 @@ const plugin = [
3051
name: 'MonacoEditorI18nPlugin',
3152
fn: () => {
3253
return new MonacoEditorI18nPlugin({
33-
locale: "dt-zh-hans",
54+
windowKey: '__DT_LOCALE__',
55+
localStorageKey: 'dt-locale',
3456
});
3557
},
3658
},
@@ -47,35 +69,47 @@ module.exports = {
4769
...,
4870
plugins: [
4971
new MonacoEditorI18nPlugin({
50-
locale: "dt-zh-hans",
72+
windowKey: '__DT_LOCALE__',
73+
localStorageKey: 'dt-locale',
5174
})
5275
],
5376
...,
5477
};
5578
```
5679

57-
### 简体中文
80+
### 使用说明
5881

59-
使用到的 `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)
82+
需要在工程代码中将使用到的界面语言内容挂载到 `window` 对象上,key 默认值为 `__DT_LOCALE__``en-US``monaco-editor` 的默认语言,值为空对象即可。后续读取界面语言内容将从 `window` 对象中读取。挂载示例如下:
6083

6184
```js
62-
new MonacoEditorI18nPlugin({
63-
locale: "zh-hans",
64-
});
85+
import dtZhHans from 'monaco-editor-i18n-plugin/out/locale/dt-zh-hans.json';
86+
import zhHant from './zh-hant.json';
87+
88+
// 将界面语言内容挂载到 window 对象上
89+
window.__DT_LOCALE__ = {
90+
'zh-hant': zhHant?.contents || {},
91+
'dt-zh-hans': dtZhHans?.contents || {},
92+
'en-US': {},
93+
};
6594
```
6695

67-
### 自定义语言
68-
69-
如果你想使用其他语言或者精简后的 `src/locale/dt-zh-hans.json` 文件不能满足要求,你可以在 [vscode-loc/i18n](https://github.com/microsoft/vscode-loc/tree/release/1.63.3/i18n) 找到其他语言的 JSON 文件。
96+
切换界面语言时需要同步将语言名称存到 localStorage 中。切换示例如下:
7097

7198
```js
72-
new MonacoEditorI18nPlugin({
73-
customLocalePath: path.join(__dirname, "./zh-hant.json"), // 繁体中文
74-
});
99+
function setLocale(value: string) {
100+
window.localStorage.setItem('dt-locale', value);
101+
}
102+
103+
setLocale('dt-zh-hans');
75104
```
76105

106+
> **注意**
107+
>
108+
> - 如果 `monaco-editor` 的界面语言没有随产品页面变化,请注意 `window.__DT_LOCALE__` 的挂载时机应尽可能的早。
109+
110+
77111
## 注意事项
78112

79-
- 目前验证的 `monaco-editor` 版本为 `0.30.1``0.31.1`
80-
- `vscode-loc` 的版本为 `1.63.3`
81-
- 如果自定义语言不生效,可能是上述两个包的版本没有对应,`vscode-loc` 后续版本的 JSON 文件结构是有变化的,请自行验证。
113+
- 目前验证的 `monaco-editor` 版本为 `0.30.1``0.31.1`
114+
- 对应的 `vscode-loc` 版本为 `1.63.3`
115+
- 如果自定义语言不生效,可能是上述两个包的版本没有对应,`vscode-loc` 后续版本的 JSON 文件结构是有变化的,请自行验证。

README.md

Lines changed: 54 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -4,18 +4,42 @@
44

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

7-
The purpose of this plugin is to set the language when using `monaco-editor`. The main implementation idea is to achieve language switching by overriding the `monaco-editor/esm/vs/nls.js`.
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.
8+
9+
10+
## Version difference
11+
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.
14+
815

916
## Install
1017

1118
`npm install monaco-editor-i18n-plugin -D`
1219

13-
## Usage
1420

15-
### dt-zh-hans
21+
## Languages
22+
23+
### dt-zh-hans (Simplified version for dtstack)
1624

1725
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).
1826

27+
### Simplified Chinese (Full version)
28+
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+
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).
34+
35+
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+
38+
39+
## Example usage
40+
41+
### configuration item
42+
1943
- `ko.config.js` [ko - Project toolkit for React Applications](https://github.com/DTStack/ko)
2044

2145
```js
@@ -30,7 +54,8 @@ const plugin = [
3054
name: 'MonacoEditorI18nPlugin',
3155
fn: () => {
3256
return new MonacoEditorI18nPlugin({
33-
locale: "dt-zh-hans",
57+
windowKey: '__DT_LOCALE__',
58+
localStorageKey: 'dt-locale',
3459
});
3560
},
3661
},
@@ -47,35 +72,47 @@ module.exports = {
4772
...,
4873
plugins: [
4974
new MonacoEditorI18nPlugin({
50-
locale: "dt-zh-hans",
75+
windowKey: '__DT_LOCALE__',
76+
localStorageKey: 'dt-locale',
5177
})
5278
],
5379
...,
5480
};
5581
```
5682

57-
### Simplified Chinese
83+
### Instructions
5884

59-
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)
85+
The interface language content used needs to be mounted on the `window` object in the engineering code, with the default key value is `__DT_LOCALE__`. `En US` is the default language for `monaco-editor`, and a value of an empty object is sufficient. The subsequent reading of interface language content will be done from the `window` object. The mounting example is as follows:
6086

6187
```js
62-
new MonacoEditorI18nPlugin({
63-
locale: "zh-hans",
64-
});
88+
import dtZhHans from 'monaco-editor-i18n-plugin/out/locale/dt-zh-hans.json';
89+
import zhHant from './zh-hant.json';
90+
91+
// Mount the interface language content onto the window object
92+
window.__DT_LOCALE__ = {
93+
'zh-hant': zhHant?.contents || {},
94+
'dt-zh-hans': dtZhHans?.contents || {},
95+
'en-US': {},
96+
};
6597
```
6698

67-
### custom languages
68-
69-
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).
99+
When switching interface languages, it is necessary to synchronize the language name to the localStorage. The switching example is as follows:
70100

71101
```js
72-
new MonacoEditorI18nPlugin({
73-
customLocalePath: path.join(__dirname, "./zh-hant.json"), // Traditional Chinese
74-
});
102+
function setLocale(value: string) {
103+
window.localStorage.setItem('dt-locale', value);
104+
}
105+
106+
setLocale('dt-zh-hans');
75107
```
76108

109+
> **Important**
110+
>
111+
> - If the interface language of `monaco-editor` does not change with the product page, please note that the mounting timing of `window.__DT_LOCALE__` should be as early as possible.
112+
113+
77114
## Notice
78115

79116
- The currently verified versions of `monaco-editor` are `0.30.1` and `0.31.1`.
80-
- The version of `vscode-loc` is `1.63.3`.
117+
- The corresponding version of `vscode doc` is `1.63.3`.
81118
- 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.

src/index.ts

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -11,15 +11,16 @@ const replaceLocalizeLoader = require.resolve("./loaders/replaceLocalize");
1111
export declare namespace MonacoEditorI18nPlugin {
1212
interface IMonacoEditorI18nPluginOpts {
1313
/**
14-
* default is en-US
14+
* window key which has locale data
15+
* default value is '__DT_LOCALE__'
1516
*/
16-
locale: "en-US" | "zh-hans" | "dt-zh-hans";
17+
windowKey: string;
1718

1819
/**
19-
* custom locale file path
20-
* eg: path.join(__dirname, './zh-hant.json')
20+
* get locale from localStorage
21+
* default value is 'dt-locale'
2122
*/
22-
customLocalePath?: string;
23+
localStorageKey: string;
2324
}
2425
}
2526

@@ -56,7 +57,8 @@ function addCompilerRules(compiler: webpack.Compiler, rules: webpack.RuleSetRule
5657

5758
class MonacoEditorI18nPlugin {
5859
options: MonacoEditorI18nPlugin.IMonacoEditorI18nPluginOpts = {
59-
locale: "en-US",
60+
windowKey: "__DT_LOCALE__",
61+
localStorageKey: "dt-locale",
6062
};
6163

6264
constructor(options: MonacoEditorI18nPlugin.IMonacoEditorI18nPluginOpts) {

src/loaders/replaceNls.ts

Lines changed: 4 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -11,26 +11,11 @@ const replaceNls: PitchLoaderDefinitionFunction<MonacoEditorI18nPlugin.IMonacoEd
1111
if (!pathRegExp.test(this.resourcePath)) return content;
1212

1313
let _content = fs.readFileSync(nls, { encoding: "utf8" });
14+
if (_content.includes("windowKey") && _content.includes("localStorageKey")) {
15+
const { windowKey, localStorageKey } = this.getOptions() || {};
1416

15-
if (_content.includes("monaco-editor-i18n-plugin")) {
16-
// use options' locale/customLocalePath
17-
const { locale, customLocalePath } = this.getOptions() || {};
18-
19-
let filePath = "";
20-
if (customLocalePath) {
21-
filePath = customLocalePath;
22-
} else if (locale === "zh-hans") {
23-
filePath = "monaco-editor-i18n-plugin/out/locale/zh-hans.json";
24-
} else if (locale === "dt-zh-hans") {
25-
filePath = "monaco-editor-i18n-plugin/out/locale/dt-zh-hans.json";
26-
} else {
27-
/**
28-
* if locale is not zh-hans, use default locale: en-US
29-
*/
30-
_content = _content.replace(/require\("[^"]*"\);/g, `{};`);
31-
return _content;
32-
}
33-
_content = _content.replace(/require\("[^"]*"\);/g, `require("${filePath}");`);
17+
_content = _content.replace(/const windowKey = \("[^"]*"\);/g, `${windowKey};`);
18+
_content = _content.replace(/const localStorageKey = \("[^"]*"\);/g, `${localStorageKey};`);
3419
}
3520

3621
return _content;

src/nls/index.js

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,5 @@
11
module.exports["localize"] = module.exports["loadMessageBundle"] = module.exports["config"] = null;
22

3-
// This file is condensed from vscode-loc 1.63.3
4-
const localeData = require("monaco-editor-i18n-plugin/out/locale/dt-zh-hans.json");
5-
63
// replace monaco-editor/esm/vs/nls.js _format
74
function _format(message, args) {
85
let result;
@@ -20,7 +17,13 @@ function _format(message, args) {
2017
// replace monaco-editor/esm/vs/nls.js localize
2118
function localize(path, data, defaultMessage) {
2219
const key = typeof data === "object" ? data.key : data;
23-
const _data = localeData?.contents || {};
20+
21+
// get locale data from window object
22+
const windowKey = "__DT_LOCALE__";
23+
const localStorageKey = "dt-locale";
24+
const locale = window.localStorage.getItem(localStorageKey) || "en-US";
25+
const _data = window[windowKey]?.[locale] || {};
26+
2427
let message = (_data[path] || {})[key];
2528
if (!message) {
2629
message = defaultMessage;

0 commit comments

Comments
 (0)