Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -2,3 +2,4 @@ node_modules
out

.DS_Store
*.tgz
1 change: 1 addition & 0 deletions .vscode/settings.json
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
{}
21 changes: 12 additions & 9 deletions README-zh_CN.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,28 +9,31 @@

## 版本差异

- 0.x 版本:当你的产品只有一种界面语言时(不需要支持国际化,如仅支持中文),请使用 [0.x](https://github.com/DTStack/monaco-editor-i18n-plugin/tree/0.x-stable) 的版本。
- 最新版本:当你的产品需要支持国际化时(产品页面支持切换界面语言),请使用最新版本。
`2.x` 和 `1.x` 的主要区别在于 `monaco-editor` 的版本。

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


## 安装

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


## 语言

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

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

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

使用到的 `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)。
使用到的 `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)。

### 自定义语言

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


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

## 注意事项

- 目前验证的 `monaco-editor` 版本为 `0.30.1` 和 `0.31.1`。
- 对应的 `vscode-loc` 版本为 `1.63.3`。
- 如果自定义语言不生效,可能是上述两个包的版本没有对应,`vscode-loc` 后续版本的 JSON 文件结构是有变化的,请自行验证
- 目前验证的 `monaco-editor` 版本为 `0.52.2`。
- 对应的 `vscode-loc` 版本为 `1.91.1`。
- 如果自定义语言不生效,可能是上述两个包的版本没有对应,`vscode-loc` 不同版本的 JSON 文件结构是有变化的(如 1.63.3 和 1.91.1),其他版本请自行验证
25 changes: 13 additions & 12 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,31 +9,32 @@ The purpose of this plugin is to set the interface language when using `monaco-e

## Version difference

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

- 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`.
- 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`.
- 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`.


## Install

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


## Languages

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

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

### Simplified Chinese (Full version)

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)。

### custom languages
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)。

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

### custom languages

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.
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.


## Example usage
Expand Down Expand Up @@ -113,6 +114,6 @@ setLocale('dt-zh-hans');

## Notice

- The currently verified versions of `monaco-editor` are `0.30.1` and `0.31.1`.
- The corresponding version of `vscode doc` is `1.63.3`.
- 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.
- The currently verified versions of `monaco-editor` are `0.52.2`.
- The corresponding version of `vscode doc` is `1.91.1`.
- 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.
4 changes: 2 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "monaco-editor-i18n-plugin",
"version": "1.0.2",
"version": "2.0.0-beta.2",
"description": "A i18n plugin for the Monaco editor",
"main": "out/index.js",
"typings": "./out/index.d.ts",
Expand All @@ -25,7 +25,7 @@
"webpack": "^5.68.0"
},
"peerDependencies": {
"monaco-editor": "0.30.1 || 0.31.1",
"monaco-editor": "0.52.2",
"webpack": "^5"
},
"keywords": [
Expand Down
10 changes: 5 additions & 5 deletions pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

5 changes: 5 additions & 0 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
import type * as webpack from "webpack";

const replaceNls = require.resolve("./loaders/replaceNls");
const replaceNlsMessages = require.resolve("./loaders/replaceNlsMessages");
const replaceLocalizeLoader = require.resolve("./loaders/replaceLocalize");

export declare namespace MonacoEditorI18nPlugin {
Expand Down Expand Up @@ -36,6 +37,10 @@ function createLoaderRules(options: MonacoEditorI18nPlugin.IMonacoEditorI18nPlug
loader: replaceNls,
options,
},
{
loader: replaceNlsMessages,
options,
},
{
loader: replaceLocalizeLoader,
},
Expand Down
16 changes: 11 additions & 5 deletions src/loaders/replaceLocalize.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,12 +10,18 @@ const replaceLocalize: PitchLoaderDefinitionFunction<MonacoEditorI18nPlugin.IMon
const vsPath = this.resourcePath.split(/monaco-editor[\\\/]esm[\\\/]/).pop();
if (!vsPath) return content;

// add vscode-loc path to function localize
const path = vsPath.replace(/\\/g, "/").replace(".js", "");
return content.replace(/(\bfunction\s+localize\()|(\blocalize\()/g, function (text) {
if (/function\s+localize/.test(text)) return text;
return `localize('${path}', `;
});

// add vscode-loc path to function localize and localize2
return content
.replace(/(\bfunction\s+localize\()|(\blocalize\()/g, function (text) {
if (/function\s+localize/.test(text)) return text;
return `localize('${path}', `;
})
.replace(/(\bfunction\s+localize2\()|(\blocalize2\()/g, function (text) {
if (/function\s+localize2/.test(text)) return text;
return `localize2('${path}', `;
});
};

module.exports = replaceLocalize;
1 change: 1 addition & 0 deletions src/loaders/replaceNls.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import { MonacoEditorI18nPlugin } from "..";
const fs = require("fs");
const nls = require.resolve("../nls");

// replace monaco-editor/esm/vs/nls.js
const replaceNls: PitchLoaderDefinitionFunction<MonacoEditorI18nPlugin.IMonacoEditorI18nPluginOpts> = function (
content: string
) {
Expand Down
25 changes: 25 additions & 0 deletions src/loaders/replaceNlsMessages.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
import type { PitchLoaderDefinitionFunction } from "webpack";
import { MonacoEditorI18nPlugin } from "..";

const fs = require("fs");
const nlsMessages = require.resolve("../nls.messages");

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

let _content = fs.readFileSync(nlsMessages, { encoding: "utf8" });
if (_content.includes("windowKey") && _content.includes("localStorageKey")) {
const { windowKey, localStorageKey } = this.getOptions() || {};

_content = _content.replace(/const windowKey = \("[^"]*"\);/g, `${windowKey};`);
_content = _content.replace(/const localStorageKey = \("[^"]*"\);/g, `${localStorageKey};`);
}

return _content;
};

module.exports = replaceNlsMessages;
Loading