From 482314c2b9a3cc64591a1d47f22318b4c6d57403 Mon Sep 17 00:00:00 2001 From: QCQCQC <1220204124@zust.edu.cn> Date: Thu, 20 Mar 2025 14:24:26 +0800 Subject: [PATCH] =?UTF-8?q?=E4=BF=AE=E5=A4=8D=E4=BA=86i18n=E7=9A=84?= =?UTF-8?q?=E7=BC=93=E5=AD=98=E4=B8=8E=E5=86=92=E5=8F=B7=E7=9B=B8=E5=85=B3?= =?UTF-8?q?=E9=97=AE=E9=A2=98?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/plugins/fileWatcher.ts | 6 +++++- src/plugins/oakLocale.ts | 12 ++++++++++-- src/utils/locales.ts | 13 ++++++++++--- 3 files changed, 25 insertions(+), 6 deletions(-) diff --git a/src/plugins/fileWatcher.ts b/src/plugins/fileWatcher.ts index 1e32cfd..6daea46 100644 --- a/src/plugins/fileWatcher.ts +++ b/src/plugins/fileWatcher.ts @@ -188,6 +188,10 @@ export function createFileWatcher(context: vscode.ExtensionContext) { const handleComponentChange = async (uri: vscode.Uri, action: any) => { // 如果不是index.ts,有可能是删除操作导致,也有可能是其他文件变化 if (uri.fsPath.indexOf('index.ts') === -1) { + // 如果是locales目录,需要更新locales + if (uri.fsPath.indexOf('locales') !== -1 && uri.fsPath.endsWith('.json')) { + reloadCachedPathLocale(join(uri.fsPath, "..")); + } // 如果是删除操作,拼接上index.ts,以便重新解析 if (action === 'delete') { removeByPrefixPath(normalizePath(uri.fsPath)); @@ -274,7 +278,7 @@ export function createFileWatcher(context: vscode.ExtensionContext) { const handleLocaleChange = async (path: vscode.Uri) => { // 重新解析locales - reloadCachedPathLocale(path.fsPath); + reloadCachedPathLocale(join(path.fsPath, "..")); }; // 监听locales目录 diff --git a/src/plugins/oakLocale.ts b/src/plugins/oakLocale.ts index 5535a9a..126593c 100644 --- a/src/plugins/oakLocale.ts +++ b/src/plugins/oakLocale.ts @@ -45,7 +45,13 @@ class LocaleDocumentLinkProvider implements vscode.DocumentLinkProvider { getLocalesData(join(document.uri.fsPath, '..')); const handler = (match: RegExpExecArray) => { - const key = match[1]; + let key = match[1]; + + // 如果开头结尾是`"或者',把开头结尾去掉一下 + if (key.startsWith('"') || key.startsWith("'") || key.startsWith('`')) { + // 去掉开头和结尾的引号 + key = key.slice(1, -1); + } if (key.includes('${')) { // 忽略动态key @@ -338,11 +344,13 @@ const addLocaleActionProvider = vscode.languages.registerCodeActionsProvider( } ); +const namespacedOrEntityKeyRegex = /([a-zA-Z0-9_]+):([a-zA-Z0-9_]+)/; + const addLocaleCommand = vscode.commands.registerCommand( 'oak-i18n.addLocaleDefinition', (document: vscode.TextDocument, range: vscode.Range, key: string) => { // 先判断key是不是命名空间的形式或者entity的形式 - if (key.includes(':')) { + if (key.includes(':') && namespacedOrEntityKeyRegex.test(key)) { console.log('命名空间形式的key需要找到对应的文件'); const { path, error } = addKeyToLocale(key, ''); if (error) { diff --git a/src/utils/locales.ts b/src/utils/locales.ts index 2424cde..d5e0c90 100644 --- a/src/utils/locales.ts +++ b/src/utils/locales.ts @@ -169,22 +169,29 @@ export const getLocaleItem = ( key: string, path: string ): LocaleItem | undefined => { + let findItem: LocaleItem | undefined = undefined; // 如果是namespace,则为xxxx::开头 if (key.includes('::')) { // 从cachedLocaleItems中找到对应的值 - return Object.values(cachedLocaleItems.namespaced) + findItem = Object.values(cachedLocaleItems.namespaced) .flat() .find((item) => { return item.value === key; }); + if (findItem) { + return findItem; + } } // 如果是entity。则为entity:开头 if (key.includes(':')) { - return Object.values(cachedLocaleItems.entities) + findItem = Object.values(cachedLocaleItems.entities) .flat() .find((item) => { return item.value === key; }); + if (findItem) { + return findItem; + } } // 如果是component,则为路径开头 if (!path) { @@ -321,7 +328,7 @@ export const getLocalesData = ( } const items = [ - getCachedComponentItems(join(path, 'locales')), + getCachedComponentItems(path), ...Object.values(cachedLocaleItems.entities), ...Object.values(cachedLocaleItems.namespaced), ];