From b45dba0e494dc7331b7fb8b6d9581256b112c3ad Mon Sep 17 00:00:00 2001 From: Ding Date: Mon, 7 Apr 2025 21:23:35 +0800 Subject: [PATCH 1/3] =?UTF-8?q?chore:=20=E5=A2=9E=E5=8A=A0=20nocacheRequir?= =?UTF-8?q?e=20=E5=AF=BC=E5=87=BA=20(#21)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit * feat: 增加 nocacheRequire 导出 * feat: tab style chore: version --- README.md | 6 +++--- package.json | 2 +- src/easy-i18n.js | 9 +++------ src/utils.js | 8 ++++++++ 4 files changed, 15 insertions(+), 10 deletions(-) create mode 100644 src/utils.js diff --git a/README.md b/README.md index b1c2e51..43a4848 100644 --- a/README.md +++ b/README.md @@ -63,11 +63,11 @@ $ ./bin/easy-i18n-cli.js -c ./google-translate.config.js ## Contributors -|[
xudafeng](https://github.com/xudafeng)
|[
snapre](https://github.com/snapre)
|[
ilimei](https://github.com/ilimei)
|[
yangkeni](https://github.com/yangkeni)
| -| :---: | :---: | :---: | :---: | +|[
xudafeng](https://github.com/xudafeng)
|[
snapre](https://github.com/snapre)
|[
yangkeni](https://github.com/yangkeni)
|[
WynterDing](https://github.com/WynterDing)
|[
ilimei](https://github.com/ilimei)
| +| :---: | :---: | :---: | :---: | :---: | -This project follows the git-contributor [spec](https://github.com/xudafeng/git-contributor), auto updated at `Tue Apr 01 2025 15:44:10 GMT+0800`. +This project follows the git-contributor [spec](https://github.com/xudafeng/git-contributor), auto updated at `Mon Apr 07 2025 21:05:31 GMT+0800`. diff --git a/package.json b/package.json index 2ca50d2..d8128f9 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "easy-i18n-cli", - "version": "2.0.1", + "version": "2.0.2", "description": "easy-i18n-cli", "keywords": [ "i18n" diff --git a/src/easy-i18n.js b/src/easy-i18n.js index 0fdf468..7324ad5 100644 --- a/src/easy-i18n.js +++ b/src/easy-i18n.js @@ -7,6 +7,7 @@ const globby = require('globby'); const { promises: fs } = require('fs'); const originFs = require('fs'); const { sync: mkdirp } = require('mkdirp'); +const Utils = require('./utils'); const defaultOptions = { distFileName: 'en-US.js', @@ -155,8 +156,7 @@ class EasyI18n { initData() { const { distFile } = this.options; try { - delete require.cache[distFile]; - const data = require(distFile); + const data = Utils.noCacheRequire(distFile); this.existedData = data.default || data; } catch (e) { console.error(e); @@ -188,7 +188,7 @@ class EasyI18n { async check() { const { distFile } = this.options; - const checkTarget = require(distFile); + const checkTarget = Utils.noCacheRequire(distFile); const lines = []; const lineOffset = await this._getLineOffset(); Object.keys(checkTarget).forEach((key, index) => { @@ -209,9 +209,6 @@ class EasyI18n { async runWithCheck(options = {}) { await this.run(options); - - const { distFile } = this.options; - delete require.cache[distFile]; await this.check(); } } diff --git a/src/utils.js b/src/utils.js new file mode 100644 index 0000000..5157193 --- /dev/null +++ b/src/utils.js @@ -0,0 +1,8 @@ +const Utils = {}; + +Utils.noCacheRequire = function (resolvedPath) { + delete require.cache[resolvedPath]; + return require(resolvedPath); +}; + +module.exports = Utils; From a18903a61b3b99abdbdaa7dd898837581c52764d Mon Sep 17 00:00:00 2001 From: wynterding Date: Tue, 29 Apr 2025 14:33:31 +0800 Subject: [PATCH 2/3] =?UTF-8?q?fix:=20=E6=94=AF=E6=8C=81=E5=85=B7=E5=90=8D?= =?UTF-8?q?=E5=AF=BC=E5=87=BA=20locale=20=E6=96=87=E4=BB=B6?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/easy-i18n.js | 5 +++-- src/utils.js | 12 ++++++++++++ 2 files changed, 15 insertions(+), 2 deletions(-) diff --git a/src/easy-i18n.js b/src/easy-i18n.js index 7324ad5..14bd115 100644 --- a/src/easy-i18n.js +++ b/src/easy-i18n.js @@ -157,7 +157,7 @@ class EasyI18n { const { distFile } = this.options; try { const data = Utils.noCacheRequire(distFile); - this.existedData = data.default || data; + this.existedData = Utils.extractLocaleFromExport(data); } catch (e) { console.error(e); } @@ -188,7 +188,8 @@ class EasyI18n { async check() { const { distFile } = this.options; - const checkTarget = Utils.noCacheRequire(distFile); + let checkTarget = Utils.noCacheRequire(distFile); + checkTarget = Utils.extractLocaleFromExport(checkTarget); const lines = []; const lineOffset = await this._getLineOffset(); Object.keys(checkTarget).forEach((key, index) => { diff --git a/src/utils.js b/src/utils.js index 5157193..365f27a 100644 --- a/src/utils.js +++ b/src/utils.js @@ -5,4 +5,16 @@ Utils.noCacheRequire = function (resolvedPath) { return require(resolvedPath); }; +Utils.extractLocaleFromExport = function (data) { + // 处理各类场景下的 i18n 数据 + const exportValue = data.default || data; + const firstValue = Object.values(exportValue)[0]; + + if (typeof firstValue === 'object') { + return firstValue; + } + + return exportValue; +}; + module.exports = Utils; From d1bbb553b364ea7fb26d375f197ca827c8779fa0 Mon Sep 17 00:00:00 2001 From: wynterding Date: Tue, 29 Apr 2025 14:58:42 +0800 Subject: [PATCH 3/3] chore: publish 2.0.3 --- package.json | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/package.json b/package.json index d8128f9..949e8f0 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "easy-i18n-cli", - "version": "2.0.2", + "version": "2.0.3", "description": "easy-i18n-cli", "keywords": [ "i18n"