Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[bugfix]:fix not find key and support multiple exclude path #20

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
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
6 changes: 6 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,12 @@

All notable changes to the react-i18n-prompt extension will be documented in this file.

### [0.0.4]

- [bugfix]: can not get text when i18n key in array in language package file.
- [bugfix]: can not get script plugin for error workspace path in windows operator system.
- [feature]: change the `react-i18n-prompt.language-package-exclude-path` to support multiple path which separated by ';'

### [0.0.3]

- [bugfix]: the link of i18n key is not working in windows operator system.
Expand Down
5 changes: 3 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
"name": "react-i18n-prompt",
"displayName": "React I18n Prompt Tool",
"description": "i18n prompt tool, show language of i18n key, add link to i18n key, prompt i18n key when user input i18n key",
"version": "0.0.3",
"version": "0.0.4",
"publisher": "Rain",
"engines": {
"vscode": "^1.58.0"
Expand Down Expand Up @@ -45,7 +45,8 @@
},
"react-i18n-prompt.language-package-exclude-path": {
"type": "string",
"default": "src/locale/zh-cn/**/index.{ts,js,tsx,jsx}"
"default": "src/locale/zh-cn/**/index.{ts,js,tsx,jsx}",
"description": "Multiple paths can be entered separated by ';'"
},
"react-i18n-prompt.language-key-prefix": {
"type": "string",
Expand Down
27 changes: 23 additions & 4 deletions src/core/core.ts
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,17 @@ class Core {
VscodeEvent.insertI18nChinese(this._activeEditor, paramsLocations);
}

public filterSameUris(originUris: vscode.Uri[], filterUris: vscode.Uri[]) {
let filterResult: vscode.Uri[] = [];
for (const filterItem of filterUris) {
const sameUri = originUris.find(
(originItem) => originItem.path === filterItem.path
);
if (!!sameUri) filterResult.push(filterItem);
}
return filterResult;
}

public async findAllLanguageDictionary() {
this._statusBarItem.notify('eye', 'Looking for i18n path...', false);
const pluginFind = plugin.getAllI18nKeyAndValue();
Expand All @@ -88,10 +99,18 @@ class Core {
}
this._languageDictionary = new Map();
this._languageFile = new Map();
const uris = await VscodeEvent.getFiles(
config.localePath,
config.localeExcludePath
);
let uris: vscode.Uri[] = [];
const localeExcludePathArray = config.localeExcludePath.split(';');
for (const excludePath of localeExcludePathArray) {
const currentUris = await VscodeEvent.getFiles(
config.localePath,
excludePath
);
uris = this.filterSameUris(
uris.length === 0 ? [...currentUris] : [...uris],
currentUris
);
}
for (const uri of uris) {
const fileContent = await fs.promises.readFile(uri.fsPath);
const ast = this._astTool.parse(fileContent.toString());
Expand Down
2 changes: 1 addition & 1 deletion src/event/VscodeEvent.ts
Original file line number Diff line number Diff line change
Expand Up @@ -99,7 +99,7 @@ class VscodeEvent {
if (!temp || temp.length === 0) {
return;
}
return temp[0].uri.path;
return temp[0].uri.fsPath;
}

public watchFileChanged(path: string) {
Expand Down
20 changes: 20 additions & 0 deletions src/tool/AstTool.ts
Original file line number Diff line number Diff line change
Expand Up @@ -172,6 +172,22 @@ class AstTool {
newKey,
dictionary
);
} else if (this.isArrayExpression(property.value)) {
let newKey = key;
if (prefix.length > 0) {
newKey = `${prefix}.${newKey}`;
}
(property.value as t.ArrayExpression).elements.forEach(
(element, index) => {
if (element) {
this.getAllI18nKeyAndValueFromObject(
element as t.ObjectExpression,
`${newKey}.${index}`,
dictionary
);
}
}
);
} else if (this.isStringLiteral(property.value)) {
let value = property.value.value;
if (value.includes('\n') || value.includes('\r')) {
Expand Down Expand Up @@ -228,6 +244,10 @@ class AstTool {
private isObjectExpression(node: t.Node): node is t.ObjectExpression {
return t.isObjectExpression(node);
}

private isArrayExpression(node: t.Node): node is t.ObjectExpression {
return t.isArrayExpression(node);
}
}

export default AstTool;