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 {
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) {
+10 -2
View File
@@ -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');