修复了i18n在多组件重复key的情况下出现错误的跳转

This commit is contained in:
Pan Qiancheng 2024-12-16 18:06:32 +08:00
parent df7d7b81dd
commit 8fbaee2cdc
2 changed files with 36 additions and 23 deletions

View File

@ -16,10 +16,10 @@ const diagnosticCollection =
vscode.languages.createDiagnosticCollection('oakLocales');
const tCallRegex =
/(?<![a-zA-Z])t\([\s]*['"`]([^'"`]*)['"`]\s*(\s*|\,\s*({\s*([a-zA-Z_0-9]+:\s*(['"`].*?['"`]|[?.a-zA-Z_0-9\s\|]+)(|\,)\s*)*}|[.a-zA-Z_0-9]+))\s*\)/g;
/(?<![a-zA-Z])t\([\s]*['"`]([^'"`]*)['"`]\s*(\s*|\,\s*({\s*([a-zA-Z_0-9]+:\s*(['"`].*?['"`]|[?!.,a-zA-Z_0-9\s\|]+)(|\,)\s*)*}|[.a-zA-Z_0-9]+))\s*\)/g;
const paramRegex =
/{\s*([a-zA-Z_0-9]+:\s*(['"`].*?['"`]|[?.a-zA-Z_0-9\s\|]+)(|\,)\s*)*}/;
/{\s*([a-zA-Z_0-9]+:\s*(['"`].*?['"`]|[?!.,a-zA-Z_0-9\s\|]+)(|\,)\s*)*}/;
class LocaleDocumentLinkProvider implements vscode.DocumentLinkProvider {
async provideDocumentLinks(
@ -40,6 +40,10 @@ class LocaleDocumentLinkProvider implements vscode.DocumentLinkProvider {
return [];
}
const localePath = join(document.uri.fsPath, '../locales');
getLocalesData(localePath);
const handler = (match: RegExpExecArray) => {
const key = match[1];
@ -49,10 +53,9 @@ class LocaleDocumentLinkProvider implements vscode.DocumentLinkProvider {
return;
}
const item = getLocaleItem(key);
const item = getLocaleItem(key, join(localePath, '../'));
if (item) {
const localePath = getCachedLocaleItemByKey(key);
if (localePath && localePath.path) {
if (item && item.path) {
const group1Start =
match.index + match[0].indexOf(match[1]);
const group1End = group1Start + match[1].length;
@ -61,10 +64,10 @@ class LocaleDocumentLinkProvider implements vscode.DocumentLinkProvider {
const range = new vscode.Range(startPos, endPos);
const documentLink = new vscode.DocumentLink(
range,
vscode.Uri.file(localePath.zhCnFile)
vscode.Uri.file(item.zhCnFile)
);
documentLink.tooltip = localePath.desc
? `CN: ${localePath.desc}`
documentLink.tooltip = item.desc
? `CN: ${item.desc}`
: `[未找到中文] 跳转到定义`;
documentLinks.push(documentLink);
@ -228,7 +231,8 @@ class LocaleDocumentLinkProvider implements vscode.DocumentLinkProvider {
}
};
getLocalesData(join(document.uri.fsPath, '../locales'));
// component path
console.log('component path ', document.uri.fsPath);
handler(match);

View File

@ -95,13 +95,14 @@ export const getAvailableKeys = (
* @returns LocaleData
*/
export const getLocalesByPath = (
path: string
path: string,
isComponentPath?: boolean
): {
path: string;
data: LocaleData;
} => {
// 在当前目录的zh_CN.json文件或者zh-CN.json文件
const localePath = join(path);
const localePath = isComponentPath ? join(path, 'locales') : join(path);
if (!fs.existsSync(localePath)) {
return {
path: localePath,
@ -134,7 +135,7 @@ export const getLocalesByPath = (
try {
data = JSON.parse(fs.readFileSync(localeFile, 'utf-8'));
} catch (error) {
console.error('读取locale文件失败', error);
console.error('读取locale文件失败', localePath, error);
}
return {
path: localeFile,
@ -164,7 +165,10 @@ export const findValueByKey = (
* @param key key
* @returns string
*/
export const getLocaleItem = (key: string): LocaleItem | undefined => {
export const getLocaleItem = (
key: string,
path: string
): LocaleItem | undefined => {
// 如果是namespace则为xxxx::开头
if (key.includes('::')) {
// 从cachedLocaleItems中找到对应的值
@ -183,11 +187,13 @@ export const getLocaleItem = (key: string): LocaleItem | undefined => {
});
}
// 如果是component则为路径开头
return Object.values(cachedLocaleItems.components)
.flat()
.find((item) => {
return item.value === key;
});
if (!path) {
return undefined;
}
const norPath = normalizePath(path);
return cachedLocaleItems.components[norPath].find((item) => {
return item.value === key;
});
};
/**
@ -279,7 +285,8 @@ const isPathCached = (path: string): boolean => {
};
const updatePathCached = (path: string) => {
const got = getLocalesByPath(path);
const got = getLocalesByPath(path, true);
locales.components[path] = {
locales: got.data || {},
zhCNpath: got.path,
@ -319,14 +326,14 @@ export const getLocalesData = (
...Object.values(cachedLocaleItems.namespaced),
];
setLoadingLocale(false);
if (prefix) {
return items.flatMap((item) => {
return item.filter((i) => i.value.startsWith(prefix));
});
}
setLoadingLocale(false);
return items.flat();
};
@ -387,10 +394,12 @@ export const reloadCachedPathLocale = (path: string) => {
updatePathCached(norPath);
};
// 这个函数有问题如果是相同的key会被覆盖
export const getCachedLocaleItemByKey = (
key: string
key: string,
path: string
): LocaleItem | undefined => {
return getLocaleItem(key);
return getLocaleItem(key, path);
};
export const addLocaleToData = (