fix translation

This commit is contained in:
DioCrafts
2025-03-28 09:03:04 +01:00
parent fb276d9b24
commit cfae57061b
5 changed files with 186 additions and 59 deletions
+29 -18
View File
@@ -34,12 +34,7 @@ async function loadTranslations(locale) {
} }
try { try {
const response = await fetch(`/api/i18n/locales/${locale}`); // Load directly from local JSON file
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
const localeData = await fetch(`/locales/${locale}.json`); const localeData = await fetch(`/locales/${locale}.json`);
if (!localeData.ok) { if (!localeData.ok) {
throw new Error(`Failed to load locale file for ${locale}`); throw new Error(`Failed to load locale file for ${locale}`);
@@ -50,17 +45,6 @@ async function loadTranslations(locale) {
} catch (error) { } catch (error) {
console.error('Error loading translations:', 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 // Return empty object as last resort
translations[locale] = {}; translations[locale] = {};
return translations[locale]; return translations[locale];
@@ -74,6 +58,13 @@ async function loadTranslations(locale) {
* @returns {string|null} - The translation value or null if not found * @returns {string|null} - The translation value or null if not found
*/ */
function getNestedValue(obj, path) { 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('.'); const keys = path.split('.');
let current = obj; let current = obj;
@@ -81,11 +72,21 @@ function getNestedValue(obj, path) {
if (current && typeof current === 'object' && key in current) { if (current && typeof current === 'object' && key in current) {
current = current[key]; current = current[key];
} else { } 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 null;
} }
} }
return typeof current === 'string' ? current : null; return (typeof current === 'string') ? current : null;
} }
/** /**
@@ -103,6 +104,16 @@ function t(key, params = {}) {
return key; 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 // Get the translation value
const value = getNestedValue(localeData, key); const value = getNestedValue(localeData, key);
if (!value) { if (!value) {
+10 -2
View File
@@ -26,9 +26,17 @@ function checkAuthentication() {
} }
} }
document.addEventListener('DOMContentLoaded', () => { document.addEventListener('DOMContentLoaded', async () => {
// Initialize i18n // 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 // Elements
const sharedItemsList = document.getElementById('shared-items-list'); const sharedItemsList = document.getElementById('shared-items-list');
+55 -1
View File
@@ -50,6 +50,22 @@
"shareUpdated": "Share settings updated successfully", "shareUpdated": "Share settings updated successfully",
"shareRemoved": "Share removed 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": { "shared": {
"backToFiles": "Back to Files", "backToFiles": "Back to Files",
"pageTitle": "Shared Resources", "pageTitle": "Shared Resources",
@@ -88,7 +104,45 @@
"itemRemoved": "Share removed successfully", "itemRemoved": "Share removed successfully",
"invalidEmail": "Please enter a valid email address", "invalidEmail": "Please enter a valid email address",
"notificationSent": "Notification sent successfully", "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": { "files": {
"name": "Name", "name": "Name",
+54
View File
@@ -35,6 +35,22 @@
"shareUpdated": "Configuración de compartido actualizada", "shareUpdated": "Configuración de compartido actualizada",
"shareRemoved": "Compartido eliminado correctamente" "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": { "shared": {
"backToFiles": "Volver a Archivos", "backToFiles": "Volver a Archivos",
"pageTitle": "Recursos Compartidos", "pageTitle": "Recursos Compartidos",
@@ -75,6 +91,44 @@
"notificationSent": "Notificación enviada correctamente", "notificationSent": "Notificación enviada correctamente",
"notificationFailed": "Error al enviar la notificación" "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": { "actions": {
"search": "Buscar archivos...", "search": "Buscar archivos...",
"new_folder": "Nueva carpeta", "new_folder": "Nueva carpeta",
+38 -38
View File
@@ -22,7 +22,7 @@
</select> </select>
</div> </div>
<button id="go-to-files" class="header-button"> <button id="go-to-files" class="header-button">
<span data-i18n="shared_backToFiles">Back to Files</span> <span data-i18n="shared.backToFiles">Back to Files</span>
</button> </button>
</div> </div>
</div> </div>
@@ -30,30 +30,30 @@
<main class="shared-page-container"> <main class="shared-page-container">
<div class="shared-header"> <div class="shared-header">
<h2 data-i18n="shared_pageTitle">Shared Resources</h2> <h2 data-i18n="shared.pageTitle">Shared Resources</h2>
<p data-i18n="shared_pageDescription">Manage your shared files and folders</p> <p data-i18n="shared.pageDescription">Manage your shared files and folders</p>
</div> </div>
<div class="shared-filters"> <div class="shared-filters">
<div class="filter-group"> <div class="filter-group">
<label for="filter-type" data-i18n="shared_filterType">Type:</label> <label for="filter-type" data-i18n="shared.filterType">Type:</label>
<select id="filter-type"> <select id="filter-type">
<option value="all" data-i18n="shared_filterAll">All</option> <option value="all" data-i18n="shared.filterAll">All</option>
<option value="file" data-i18n="shared_filterFiles">Files</option> <option value="file" data-i18n="shared.filterFiles">Files</option>
<option value="folder" data-i18n="shared_filterFolders">Folders</option> <option value="folder" data-i18n="shared.filterFolders">Folders</option>
</select> </select>
</div> </div>
<div class="filter-group"> <div class="filter-group">
<label for="sort-by" data-i18n="shared_sortBy">Sort by:</label> <label for="sort-by" data-i18n="shared.sortBy">Sort by:</label>
<select id="sort-by"> <select id="sort-by">
<option value="name" data-i18n="shared_sortByName">Name</option> <option value="name" data-i18n="shared.sortByName">Name</option>
<option value="date" data-i18n="shared_sortByDate">Date shared</option> <option value="date" data-i18n="shared.sortByDate">Date shared</option>
<option value="expiration" data-i18n="shared_sortByExpiration">Expiration</option> <option value="expiration" data-i18n="shared.sortByExpiration">Expiration</option>
</select> </select>
</div> </div>
<div class="search-box"> <div class="search-box">
<input type="text" id="shared-search" placeholder="Search shared items..."> <input type="text" id="shared-search" placeholder="Search shared items...">
<button id="shared-search-btn"><span data-i18n="shared_search">Search</span></button> <button id="shared-search-btn"><span data-i18n="shared.search">Search</span></button>
</div> </div>
</div> </div>
@@ -61,13 +61,13 @@
<table class="shared-list"> <table class="shared-list">
<thead> <thead>
<tr> <tr>
<th data-i18n="shared_colName">Name</th> <th data-i18n="shared.colName">Name</th>
<th data-i18n="shared_colType">Type</th> <th data-i18n="shared.colType">Type</th>
<th data-i18n="shared_colDateShared">Date Shared</th> <th data-i18n="shared.colDateShared">Date Shared</th>
<th data-i18n="shared_colExpiration">Expiration</th> <th data-i18n="shared.colExpiration">Expiration</th>
<th data-i18n="shared_colPermissions">Permissions</th> <th data-i18n="shared.colPermissions">Permissions</th>
<th data-i18n="shared_colPassword">Password</th> <th data-i18n="shared.colPassword">Password</th>
<th data-i18n="shared_colActions">Actions</th> <th data-i18n="shared.colActions">Actions</th>
</tr> </tr>
</thead> </thead>
<tbody id="shared-items-list"> <tbody id="shared-items-list">
@@ -78,9 +78,9 @@
<div id="empty-shared-state" class="empty-state" style="display:none;"> <div id="empty-shared-state" class="empty-state" style="display:none;">
<div class="empty-state-icon">📂</div> <div class="empty-state-icon">📂</div>
<h3 data-i18n="shared_emptyStateTitle">No shared resources yet</h3> <h3 data-i18n="shared.emptyStateTitle">No shared resources yet</h3>
<p data-i18n="shared_emptyStateDesc">When you share files or folders, they will appear here</p> <p data-i18n="shared.emptyStateDesc">When you share files or folders, they will appear here</p>
<a href="/" class="button primary" data-i18n="shared_goToFiles">Go to Files</a> <a href="/" class="button primary" data-i18n="shared.goToFiles">Go to Files</a>
</div> </div>
</main> </main>
@@ -88,7 +88,7 @@
<div id="share-dialog" class="dialog"> <div id="share-dialog" class="dialog">
<div class="dialog-content"> <div class="dialog-content">
<div class="dialog-header"> <div class="dialog-header">
<h3 data-i18n="share_dialogTitle">Share Link</h3> <h3 data-i18n="share.dialogTitle">Share Link</h3>
<button class="close-dialog-btn">&times;</button> <button class="close-dialog-btn">&times;</button>
</div> </div>
<div class="dialog-body"> <div class="dialog-body">
@@ -98,43 +98,43 @@
</div> </div>
<div class="share-link-section"> <div class="share-link-section">
<label for="share-link-url" data-i18n="share_linkLabel">Share Link:</label> <label for="share-link-url" data-i18n="share.linkLabel">Share Link:</label>
<div class="share-link-container"> <div class="share-link-container">
<input type="text" id="share-link-url" readonly> <input type="text" id="share-link-url" readonly>
<button id="copy-link-btn" data-i18n="share_copyLink">Copy</button> <button id="copy-link-btn" data-i18n="share.copyLink">Copy</button>
</div> </div>
</div> </div>
<div class="share-settings"> <div class="share-settings">
<div class="share-setting"> <div class="share-setting">
<label data-i18n="share_permissions">Permissions:</label> <label data-i18n="share.permissions">Permissions:</label>
<div class="permissions-options"> <div class="permissions-options">
<label> <label>
<input type="checkbox" id="permission-read" checked> <input type="checkbox" id="permission-read" checked>
<span data-i18n="share_permissionRead">Read</span> <span data-i18n="share.permissionRead">Read</span>
</label> </label>
<label> <label>
<input type="checkbox" id="permission-write"> <input type="checkbox" id="permission-write">
<span data-i18n="share_permissionWrite">Write</span> <span data-i18n="share.permissionWrite">Write</span>
</label> </label>
<label> <label>
<input type="checkbox" id="permission-reshare"> <input type="checkbox" id="permission-reshare">
<span data-i18n="share_permissionReshare">Reshare</span> <span data-i18n="share.permissionReshare">Reshare</span>
</label> </label>
</div> </div>
</div> </div>
<div class="share-setting"> <div class="share-setting">
<label for="share-password" data-i18n="share_password">Password Protection:</label> <label for="share-password" data-i18n="share.password">Password Protection:</label>
<div class="password-setting"> <div class="password-setting">
<input type="checkbox" id="enable-password"> <input type="checkbox" id="enable-password">
<input type="password" id="share-password" placeholder="Enter password" disabled> <input type="password" id="share-password" placeholder="Enter password" disabled>
<button id="generate-password" data-i18n="share_generatePassword">Generate</button> <button id="generate-password" data-i18n="share.generatePassword">Generate</button>
</div> </div>
</div> </div>
<div class="share-setting"> <div class="share-setting">
<label for="share-expiration" data-i18n="share_expiration">Expiration Date:</label> <label for="share-expiration" data-i18n="share.expiration">Expiration Date:</label>
<div class="expiration-setting"> <div class="expiration-setting">
<input type="checkbox" id="enable-expiration"> <input type="checkbox" id="enable-expiration">
<input type="date" id="share-expiration" disabled> <input type="date" id="share-expiration" disabled>
@@ -143,8 +143,8 @@
</div> </div>
<div class="share-actions"> <div class="share-actions">
<button id="update-share-btn" class="button primary" data-i18n="share_update">Update Share</button> <button id="update-share-btn" class="button primary" data-i18n="share.update">Update Share</button>
<button id="remove-share-btn" class="button danger" data-i18n="share_remove">Remove Share</button> <button id="remove-share-btn" class="button danger" data-i18n="share.remove">Remove Share</button>
</div> </div>
</div> </div>
</div> </div>
@@ -154,7 +154,7 @@
<div id="share-notification-dialog" class="dialog"> <div id="share-notification-dialog" class="dialog">
<div class="dialog-content"> <div class="dialog-content">
<div class="dialog-header"> <div class="dialog-header">
<h3 data-i18n="share_notifyTitle">Send Notification</h3> <h3 data-i18n="share.notifyTitle">Send Notification</h3>
<button class="close-dialog-btn">&times;</button> <button class="close-dialog-btn">&times;</button>
</div> </div>
<div class="dialog-body"> <div class="dialog-body">
@@ -165,18 +165,18 @@
<div class="notification-form"> <div class="notification-form">
<div class="form-group"> <div class="form-group">
<label for="notification-email" data-i18n="share_notifyEmailLabel">Email Address:</label> <label for="notification-email" data-i18n="share.notifyEmailLabel">Email Address:</label>
<input type="email" id="notification-email" placeholder="Enter recipient email"> <input type="email" id="notification-email" placeholder="Enter recipient email">
</div> </div>
<div class="form-group"> <div class="form-group">
<label for="notification-message" data-i18n="share_notifyMessageLabel">Message (optional):</label> <label for="notification-message" data-i18n="share.notifyMessageLabel">Message (optional):</label>
<textarea id="notification-message" placeholder="Add a personal message" rows="3"></textarea> <textarea id="notification-message" placeholder="Add a personal message" rows="3"></textarea>
</div> </div>
</div> </div>
<div class="notification-actions"> <div class="notification-actions">
<button id="send-notification-btn" class="button primary" data-i18n="share_notifySend">Send Notification</button> <button id="send-notification-btn" class="button primary" data-i18n="share.notifySend">Send Notification</button>
</div> </div>
</div> </div>
</div> </div>