Refactor localization handling and improve i18n support across the application

- Replaced `safeTranslate` with `translate` in various components for consistent translation handling.
- Updated Chinese (Simplified and Traditional) localization files to include new keys and improved translations for model card actions, metadata, and usage tips.
- Enhanced the ModelCard, ModelDescription, ModelMetadata, ModelModal, and ModelTags components to utilize the new translation functions.
- Improved user feedback messages for actions like copying to clipboard, saving notes, and updating tags with localized strings.
- Ensured all UI elements reflect the correct translations based on the user's language preference.
This commit is contained in:
Will Miao
2025-08-31 11:19:06 +08:00
parent 75f3764e6c
commit 59010ca431
16 changed files with 1029 additions and 208 deletions

View File

@@ -22,29 +22,6 @@ export function translate(key, params = {}, fallback = null) {
return translation;
}
/**
* Safe translation function. Assumes i18n is already ready.
* @param {string} key - Translation key
* @param {Object} params - Parameters for interpolation
* @param {string} fallback - Fallback text if translation fails
* @returns {string} Translated text
*/
export function safeTranslate(key, params = {}, fallback = null) {
if (!window.i18n) {
console.warn('i18n not available');
return fallback || key;
}
const translation = window.i18n.t(key, params);
// If translation returned the key (meaning not found), use fallback
if (translation === key && fallback) {
return fallback;
}
return translation;
}
/**
* Update element text with translation
* @param {HTMLElement|string} element - Element or selector
@@ -56,7 +33,7 @@ export function updateElementText(element, key, params = {}, fallback = null) {
const el = typeof element === 'string' ? document.querySelector(element) : element;
if (!el) return;
const text = safeTranslate(key, params, fallback);
const text = translate(key, params, fallback);
el.textContent = text;
}
@@ -72,7 +49,7 @@ export function updateElementAttribute(element, attribute, key, params = {}, fal
const el = typeof element === 'string' ? document.querySelector(element) : element;
if (!el) return;
const text = safeTranslate(key, params, fallback);
const text = translate(key, params, fallback);
el.setAttribute(attribute, text);
}