From cfae57061bf2bf8fd76edd807f3ef2ff1248f4c7 Mon Sep 17 00:00:00 2001 From: DioCrafts Date: Fri, 28 Mar 2025 09:03:04 +0100 Subject: [PATCH] fix translation --- static/js/i18n.js | 47 ++++++++++++++++---------- static/js/shared.js | 12 +++++-- static/locales/en.json | 56 ++++++++++++++++++++++++++++++- static/locales/es.json | 54 ++++++++++++++++++++++++++++++ static/shared.html | 76 +++++++++++++++++++++--------------------- 5 files changed, 186 insertions(+), 59 deletions(-) diff --git a/static/js/i18n.js b/static/js/i18n.js index 4fcbb63c..206bde64 100644 --- a/static/js/i18n.js +++ b/static/js/i18n.js @@ -34,12 +34,7 @@ async function loadTranslations(locale) { } try { - const response = await fetch(`/api/i18n/locales/${locale}`); - if (!response.ok) { - throw new Error(`Failed to load translations for ${locale}`); - } - - // Fetch the actual JSON file directly if the API doesn't provide a full translations object + // Load directly from local JSON file const localeData = await fetch(`/locales/${locale}.json`); if (!localeData.ok) { throw new Error(`Failed to load locale file for ${locale}`); @@ -50,17 +45,6 @@ async function loadTranslations(locale) { } catch (error) { console.error('Error loading translations:', error); - // Try to load from file directly as fallback - try { - const fallbackResponse = await fetch(`/locales/${locale}.json`); - if (fallbackResponse.ok) { - translations[locale] = await fallbackResponse.json(); - return translations[locale]; - } - } catch (fallbackError) { - console.error('Error loading fallback translations:', fallbackError); - } - // Return empty object as last resort translations[locale] = {}; return translations[locale]; @@ -74,6 +58,13 @@ async function loadTranslations(locale) { * @returns {string|null} - The translation value or null if not found */ function getNestedValue(obj, path) { + // Try direct key match first + if (obj && typeof obj === 'object' && path in obj) { + const value = obj[path]; + return (typeof value === 'string') ? value : null; + } + + // Try standard dot notation for nested values const keys = path.split('.'); let current = obj; @@ -81,11 +72,21 @@ function getNestedValue(obj, path) { if (current && typeof current === 'object' && key in current) { current = current[key]; } else { + // Key not found in standard dotted path + // Try a last attempt with underscore format if this is a prefix_suffix format key + if (path.includes('_') && !path.includes('.')) { + const [prefix, ...parts] = path.split('_'); + const suffix = parts.join('_'); + + if (obj[prefix] && typeof obj[prefix] === 'object' && suffix in obj[prefix]) { + return obj[prefix][suffix]; + } + } return null; } } - return typeof current === 'string' ? current : null; + return (typeof current === 'string') ? current : null; } /** @@ -103,6 +104,16 @@ function t(key, params = {}) { return key; } + // Special handling for shared_ and share_ prefixed keys + if (key.startsWith('shared_') || key.startsWith('share_')) { + const unprefixedKey = key.replace(/^(shared|share)_/, ''); + const prefixObj = key.startsWith('shared_') ? localeData.shared : localeData.share; + + if (prefixObj && typeof prefixObj === 'object' && unprefixedKey in prefixObj) { + return interpolate(prefixObj[unprefixedKey], params); + } + } + // Get the translation value const value = getNestedValue(localeData, key); if (!value) { diff --git a/static/js/shared.js b/static/js/shared.js index 164f4054..0006dea7 100644 --- a/static/js/shared.js +++ b/static/js/shared.js @@ -26,9 +26,17 @@ function checkAuthentication() { } } -document.addEventListener('DOMContentLoaded', () => { +document.addEventListener('DOMContentLoaded', async () => { // Initialize i18n - initializeI18n(); + await initializeI18n(); + + // Wait a moment for translations to fully load + setTimeout(() => { + // Manually translate all elements with data-i18n attribute + if (window.i18n && window.i18n.translatePage) { + window.i18n.translatePage(); + } + }, 500); // Elements const sharedItemsList = document.getElementById('shared-items-list'); diff --git a/static/locales/en.json b/static/locales/en.json index 0a95d54c..21e1af26 100644 --- a/static/locales/en.json +++ b/static/locales/en.json @@ -50,6 +50,22 @@ "shareUpdated": "Share settings updated successfully", "shareRemoved": "Share removed successfully" }, + "share_dialogTitle": "Share Link", + "share_linkLabel": "Share Link:", + "share_copyLink": "Copy", + "share_permissions": "Permissions:", + "share_permissionRead": "Read", + "share_permissionWrite": "Write", + "share_permissionReshare": "Reshare", + "share_password": "Password Protection:", + "share_generatePassword": "Generate", + "share_expiration": "Expiration Date:", + "share_update": "Update Share", + "share_remove": "Remove Share", + "share_notifyTitle": "Send Notification", + "share_notifyEmailLabel": "Email Address:", + "share_notifyMessageLabel": "Message (optional):", + "share_notifySend": "Send Notification", "shared": { "backToFiles": "Back to Files", "pageTitle": "Shared Resources", @@ -88,7 +104,45 @@ "itemRemoved": "Share removed successfully", "invalidEmail": "Please enter a valid email address", "notificationSent": "Notification sent successfully", - "notificationFailed": "Failed to send notification" + "notificationFailed": "Failed to send notification", + "shared_backToFiles": "Back to Files", + "shared_pageTitle": "Shared Resources", + "shared_pageDescription": "Manage your shared files and folders", + "shared_filterType": "Type:", + "shared_filterAll": "All", + "shared_filterFiles": "Files", + "shared_filterFolders": "Folders", + "shared_sortBy": "Sort by:", + "shared_sortByName": "Name", + "shared_sortByDate": "Date shared", + "shared_sortByExpiration": "Expiration", + "shared_search": "Search", + "shared_colName": "Name", + "shared_colType": "Type", + "shared_colDateShared": "Date Shared", + "shared_colExpiration": "Expiration", + "shared_colPermissions": "Permissions", + "shared_colPassword": "Password", + "shared_colActions": "Actions", + "shared_emptyStateTitle": "No shared resources yet", + "shared_emptyStateDesc": "When you share files or folders, they will appear here", + "shared_goToFiles": "Go to Files", + "shared_typeFile": "File", + "shared_typeFolder": "Folder", + "shared_noExpiration": "No expiration", + "shared_hasPassword": "Yes", + "shared_noPassword": "No", + "shared_editShare": "Edit Share", + "shared_notifyShare": "Notify Someone", + "shared_copyLink": "Copy Link", + "shared_removeShare": "Remove Share", + "shared_linkCopied": "Link copied to clipboard!", + "shared_linkCopyFailed": "Failed to copy link", + "shared_itemUpdated": "Share settings updated successfully", + "shared_itemRemoved": "Share removed successfully", + "shared_invalidEmail": "Please enter a valid email address", + "shared_notificationSent": "Notification sent successfully", + "shared_notificationFailed": "Failed to send notification" }, "files": { "name": "Name", diff --git a/static/locales/es.json b/static/locales/es.json index ae863ae6..00a8897c 100644 --- a/static/locales/es.json +++ b/static/locales/es.json @@ -35,6 +35,22 @@ "shareUpdated": "Configuración de compartido actualizada", "shareRemoved": "Compartido eliminado correctamente" }, + "share_dialogTitle": "Compartir Enlace", + "share_linkLabel": "Enlace compartido:", + "share_copyLink": "Copiar", + "share_permissions": "Permisos:", + "share_permissionRead": "Lectura", + "share_permissionWrite": "Escritura", + "share_permissionReshare": "Recompartir", + "share_password": "Protección con contraseña:", + "share_generatePassword": "Generar", + "share_expiration": "Fecha de caducidad:", + "share_update": "Actualizar compartido", + "share_remove": "Eliminar compartido", + "share_notifyTitle": "Enviar notificación", + "share_notifyEmailLabel": "Dirección de correo:", + "share_notifyMessageLabel": "Mensaje (opcional):", + "share_notifySend": "Enviar notificación", "shared": { "backToFiles": "Volver a Archivos", "pageTitle": "Recursos Compartidos", @@ -75,6 +91,44 @@ "notificationSent": "Notificación enviada correctamente", "notificationFailed": "Error al enviar la notificación" }, + "shared_backToFiles": "Volver a Archivos", + "shared_pageTitle": "Recursos Compartidos", + "shared_pageDescription": "Administra tus archivos y carpetas compartidos", + "shared_filterType": "Tipo:", + "shared_filterAll": "Todos", + "shared_filterFiles": "Archivos", + "shared_filterFolders": "Carpetas", + "shared_sortBy": "Ordenar por:", + "shared_sortByName": "Nombre", + "shared_sortByDate": "Fecha compartido", + "shared_sortByExpiration": "Caducidad", + "shared_search": "Buscar", + "shared_colName": "Nombre", + "shared_colType": "Tipo", + "shared_colDateShared": "Fecha compartido", + "shared_colExpiration": "Caducidad", + "shared_colPermissions": "Permisos", + "shared_colPassword": "Contraseña", + "shared_colActions": "Acciones", + "shared_emptyStateTitle": "Aún no hay recursos compartidos", + "shared_emptyStateDesc": "Cuando compartas archivos o carpetas, aparecerán aquí", + "shared_goToFiles": "Ir a Archivos", + "shared_typeFile": "Archivo", + "shared_typeFolder": "Carpeta", + "shared_noExpiration": "Sin caducidad", + "shared_hasPassword": "Sí", + "shared_noPassword": "No", + "shared_editShare": "Editar compartido", + "shared_notifyShare": "Notificar a alguien", + "shared_copyLink": "Copiar enlace", + "shared_removeShare": "Eliminar compartido", + "shared_linkCopied": "¡Enlace copiado al portapapeles!", + "shared_linkCopyFailed": "Error al copiar el enlace", + "shared_itemUpdated": "Configuración de compartido actualizada", + "shared_itemRemoved": "Compartido eliminado correctamente", + "shared_invalidEmail": "Por favor, introduce una dirección de correo válida", + "shared_notificationSent": "Notificación enviada correctamente", + "shared_notificationFailed": "Error al enviar la notificación", "actions": { "search": "Buscar archivos...", "new_folder": "Nueva carpeta", diff --git a/static/shared.html b/static/shared.html index 18bb439e..4a99e51e 100644 --- a/static/shared.html +++ b/static/shared.html @@ -22,7 +22,7 @@ @@ -30,30 +30,30 @@
-

Shared Resources

-

Manage your shared files and folders

+

Shared Resources

+

Manage your shared files and folders

- +
- +
@@ -61,13 +61,13 @@ - - - - - - - + + + + + + + @@ -78,9 +78,9 @@ @@ -88,7 +88,7 @@
-

Share Link

+

Share Link

@@ -98,43 +98,43 @@
@@ -154,7 +154,7 @@
-

Send Notification

+

Send Notification

@@ -165,18 +165,18 @@
- +
- +
- +
NameTypeDate SharedExpirationPermissionsPasswordActionsNameTypeDate SharedExpirationPermissionsPasswordActions