refactor(i18n): optimize safeTranslate usage by removing unnecessary await calls

This commit is contained in:
Will Miao
2025-08-31 10:32:15 +08:00
parent 867ffd1163
commit 75f3764e6c
8 changed files with 54 additions and 58 deletions

View File

@@ -143,15 +143,15 @@ async function toggleFavorite(card) {
});
if (newFavoriteState) {
const addedText = await safeTranslate('modelCard.favorites.added', {}, 'Added to favorites');
const addedText = safeTranslate('modelCard.favorites.added', {}, 'Added to favorites');
showToast(addedText, 'success');
} else {
const removedText = await safeTranslate('modelCard.favorites.removed', {}, 'Removed from favorites');
const removedText = safeTranslate('modelCard.favorites.removed', {}, 'Removed from favorites');
showToast(removedText, 'success');
}
} catch (error) {
console.error('Failed to update favorite status:', error);
const errorText = await safeTranslate('modelCard.favorites.updateFailed', {}, 'Failed to update favorite status');
const errorText = safeTranslate('modelCard.favorites.updateFailed', {}, 'Failed to update favorite status');
showToast(errorText, 'error');
}
}
@@ -164,8 +164,8 @@ function handleSendToWorkflow(card, replaceMode, modelType) {
sendLoraToWorkflow(loraSyntax, replaceMode, 'lora');
} else {
// Checkpoint send functionality - to be implemented
safeTranslate('modelCard.sendToWorkflow.checkpointNotImplemented', {}, 'Send checkpoint to workflow - feature to be implemented')
.then(text => showToast(text, 'info'));
const text = safeTranslate('modelCard.sendToWorkflow.checkpointNotImplemented', {}, 'Send checkpoint to workflow - feature to be implemented');
showToast(text, 'info');
}
}
@@ -200,8 +200,8 @@ async function handleExampleImagesAccess(card, modelType) {
}
} catch (error) {
console.error('Error checking for example images:', error);
safeTranslate('modelCard.exampleImages.checkError', {}, 'Error checking for example images')
.then(text => showToast(text, 'error'));
const text = safeTranslate('modelCard.exampleImages.checkError', {}, 'Error checking for example images');
showToast(text, 'error');
}
}
@@ -283,8 +283,8 @@ function showExampleAccessModal(card, modelType) {
// Get the model hash
const modelHash = card.dataset.sha256;
if (!modelHash) {
safeTranslate('modelCard.exampleImages.missingHash', {}, 'Missing model hash information.')
.then(text => showToast(text, 'error'));
const text = safeTranslate('modelCard.exampleImages.missingHash', {}, 'Missing model hash information.');
showToast(text, 'error');
return;
}

View File

@@ -63,7 +63,7 @@ async function loadModelDescription() {
const description = await getModelApiClient().fetchModelDescription(filePath);
// Update content
const noDescriptionText = await safeTranslate('modals.model.description.noDescription', {}, 'No model description available');
const noDescriptionText = safeTranslate('modals.model.description.noDescription', {}, 'No model description available');
descriptionContent.innerHTML = description || `<div class="no-description">${noDescriptionText}</div>`;
descriptionContent.dataset.loaded = 'true';
@@ -72,7 +72,7 @@ async function loadModelDescription() {
} catch (error) {
console.error('Error loading model description:', error);
const failedText = await safeTranslate('modals.model.description.failedToLoad', {}, 'Failed to load model description');
const failedText = safeTranslate('modals.model.description.failedToLoad', {}, 'Failed to load model description');
descriptionContent.innerHTML = `<div class="no-description">${failedText}</div>`;
} finally {
// Hide loading state
@@ -96,7 +96,7 @@ export async function setupModelDescriptionEditing(filePath) {
editBtn = document.createElement('button');
editBtn.className = 'edit-model-description-btn';
// Set title using i18n
const editTitle = await safeTranslate('modals.model.description.editTitle', {}, 'Edit model description');
const editTitle = safeTranslate('modals.model.description.editTitle', {}, 'Edit model description');
editBtn.title = editTitle;
editBtn.innerHTML = '<i class="fas fa-pencil-alt"></i>';
descContainer.insertBefore(editBtn, descContent);
@@ -154,7 +154,7 @@ export async function setupModelDescriptionEditing(filePath) {
}
if (!newValue) {
this.innerHTML = originalValue;
const emptyErrorText = await safeTranslate('modals.model.description.validation.cannotBeEmpty', {}, 'Description cannot be empty');
const emptyErrorText = safeTranslate('modals.model.description.validation.cannotBeEmpty', {}, 'Description cannot be empty');
showToast(emptyErrorText, 'error');
exitEditMode();
return;
@@ -163,11 +163,11 @@ export async function setupModelDescriptionEditing(filePath) {
// Save to backend
const { getModelApiClient } = await import('../../api/modelApiFactory.js');
await getModelApiClient().saveModelMetadata(filePath, { modelDescription: newValue });
const successText = await safeTranslate('modals.model.description.messages.updated', {}, 'Model description updated');
const successText = safeTranslate('modals.model.description.messages.updated', {}, 'Model description updated');
showToast(successText, 'success');
} catch (err) {
this.innerHTML = originalValue;
const errorText = await safeTranslate('modals.model.description.messages.updateFailed', {}, 'Failed to update model description');
const errorText = safeTranslate('modals.model.description.messages.updateFailed', {}, 'Failed to update model description');
showToast(errorText, 'error');
} finally {
exitEditMode();

View File

@@ -83,8 +83,8 @@ export function setupModelNameEditing(filePath) {
sel.removeAllRanges();
sel.addRange(range);
safeTranslate('modelMetadata.validation.nameTooLong', {}, 'Model name is limited to 100 characters')
.then(text => showToast(text, 'warning'));
const text = safeTranslate('modelMetadata.validation.nameTooLong', {}, 'Model name is limited to 100 characters');
showToast(text, 'warning');
}
});

View File

@@ -62,9 +62,9 @@ export async function showModelModal(model, modelType) {
}
// Generate tabs based on model type
const examplesText = await safeTranslate('modals.model.tabs.examples', {}, 'Examples');
const descriptionText = await safeTranslate('modals.model.tabs.description', {}, 'Model Description');
const recipesText = await safeTranslate('modals.model.tabs.recipes', {}, 'Recipes');
const examplesText = safeTranslate('modals.model.tabs.examples', {}, 'Examples');
const descriptionText = safeTranslate('modals.model.tabs.description', {}, 'Model Description');
const recipesText = safeTranslate('modals.model.tabs.recipes', {}, 'Recipes');
const tabsContent = modelType === 'loras' ?
`<button class="tab-btn active" data-tab="showcase">${examplesText}</button>
@@ -73,10 +73,10 @@ export async function showModelModal(model, modelType) {
`<button class="tab-btn active" data-tab="showcase">${examplesText}</button>
<button class="tab-btn" data-tab="description">${descriptionText}</button>`;
const loadingExampleImagesText = await safeTranslate('modals.model.loading.exampleImages', {}, 'Loading example images...');
const loadingDescriptionText = await safeTranslate('modals.model.loading.description', {}, 'Loading model description...');
const loadingRecipesText = await safeTranslate('modals.model.loading.recipes', {}, 'Loading recipes...');
const loadingExamplesText = await safeTranslate('modals.model.loading.examples', {}, 'Loading examples...');
const loadingExampleImagesText = safeTranslate('modals.model.loading.exampleImages', {}, 'Loading example images...');
const loadingDescriptionText = safeTranslate('modals.model.loading.description', {}, 'Loading model description...');
const loadingRecipesText = safeTranslate('modals.model.loading.recipes', {}, 'Loading recipes...');
const loadingExamplesText = safeTranslate('modals.model.loading.examples', {}, 'Loading examples...');
const tabPanesContent = modelType === 'loras' ?
`<div id="showcase-tab" class="tab-pane active">

View File

@@ -217,10 +217,10 @@ async function saveTags() {
// Exit edit mode
editBtn.click();
showToast(await safeTranslate('modelTags.messages.updated', {}, 'Tags updated successfully'), 'success');
showToast(safeTranslate('modelTags.messages.updated', {}, 'Tags updated successfully'), 'success');
} catch (error) {
console.error('Error saving tags:', error);
showToast(await safeTranslate('modelTags.messages.updateFailed', {}, 'Failed to update tags'), 'error');
showToast(safeTranslate('modelTags.messages.updateFailed', {}, 'Failed to update tags'), 'error');
}
}
@@ -362,24 +362,24 @@ function addNewTag(tag) {
// Validation: Check length
if (tag.length > 30) {
safeTranslate('modelTags.validation.maxLength', {}, 'Tag should not exceed 30 characters')
.then(text => showToast(text, 'error'));
const text = safeTranslate('modelTags.validation.maxLength', {}, 'Tag should not exceed 30 characters');
showToast(text, 'error');
return;
}
// Validation: Check total number
const currentTags = tagsContainer.querySelectorAll('.metadata-item');
if (currentTags.length >= 30) {
safeTranslate('modelTags.validation.maxCount', {}, 'Maximum 30 tags allowed')
.then(text => showToast(text, 'error'));
const text = safeTranslate('modelTags.validation.maxCount', {}, 'Maximum 30 tags allowed');
showToast(text, 'error');
return;
}
// Validation: Check for duplicates
const existingTags = Array.from(currentTags).map(tag => tag.dataset.tag);
if (existingTags.includes(tag)) {
safeTranslate('modelTags.validation.duplicate', {}, 'This tag already exists')
.then(text => showToast(text, 'error'));
const text = safeTranslate('modelTags.validation.duplicate', {}, 'This tag already exists');
showToast(text, 'error');
return;
}