修复了i18n的缓存与冒号相关问题

This commit is contained in:
Pan Qiancheng 2025-03-20 14:24:26 +08:00
parent 44b91a2f37
commit 482314c2b9
3 changed files with 25 additions and 6 deletions

View File

@ -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目录

View File

@ -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) {

View File

@ -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),
];