Initial commit: update
This commit is contained in:
99
backend/target/backend-1/assets/i18n.js
Normal file
99
backend/target/backend-1/assets/i18n.js
Normal file
@@ -0,0 +1,99 @@
|
||||
// i18n.js - Internationalization Module
|
||||
let currentLang = localStorage.getItem('language') || 'de';
|
||||
let translations = {};
|
||||
|
||||
// Load translations
|
||||
async function loadTranslations() {
|
||||
try {
|
||||
const response = await fetch('assets/translations.json');
|
||||
translations = await response.json();
|
||||
} catch (error) {
|
||||
console.error('Failed to load translations:', error);
|
||||
}
|
||||
}
|
||||
|
||||
// Get translation by key path (e.g., "index.title")
|
||||
function t(keyPath) {
|
||||
const keys = keyPath.split('.');
|
||||
let value = translations[currentLang];
|
||||
|
||||
for (const key of keys) {
|
||||
value = value?.[key];
|
||||
}
|
||||
|
||||
return value || keyPath;
|
||||
}
|
||||
|
||||
// Change language
|
||||
function setLanguage(lang) {
|
||||
currentLang = lang;
|
||||
localStorage.setItem('language', lang);
|
||||
translatePage();
|
||||
}
|
||||
|
||||
// Translate all elements with data-i18n attribute
|
||||
function translatePage() {
|
||||
document.querySelectorAll('[data-i18n]').forEach(element => {
|
||||
const key = element.getAttribute('data-i18n');
|
||||
const translation = t(key);
|
||||
|
||||
// Skip if translation is just the key (not found)
|
||||
if (translation === key) {
|
||||
console.warn(`Translation not found for key: ${key}`);
|
||||
return;
|
||||
}
|
||||
|
||||
if (element.tagName === 'INPUT' || element.tagName === 'TEXTAREA') {
|
||||
element.placeholder = translation;
|
||||
} else if (element.tagName === 'SMALL') {
|
||||
// For <small> tags, use innerHTML to preserve <b> and <br> tags
|
||||
element.innerHTML = translation;
|
||||
} else if (element.tagName === 'SPAN') {
|
||||
// For <span> tags, check if translation contains HTML
|
||||
if (translation.includes('<b>') || translation.includes('<i>') || translation.includes('<br>')) {
|
||||
element.innerHTML = translation;
|
||||
} else {
|
||||
element.textContent = translation;
|
||||
}
|
||||
} else if (element.tagName === 'P') {
|
||||
// For paragraphs, check if they contain <b> tags
|
||||
if (translation.includes('<b>')) {
|
||||
element.innerHTML = translation;
|
||||
} else {
|
||||
element.textContent = translation;
|
||||
}
|
||||
} else {
|
||||
// Default: use textContent for simple text
|
||||
element.textContent = translation;
|
||||
}
|
||||
});
|
||||
|
||||
// Update page title
|
||||
const titleElement = document.querySelector('[data-i18n-title]');
|
||||
if (titleElement) {
|
||||
const titleKey = titleElement.getAttribute('data-i18n-title');
|
||||
document.title = t(titleKey);
|
||||
}
|
||||
|
||||
// Update active language indicator
|
||||
document.querySelectorAll('.lang-switch').forEach(btn => {
|
||||
btn.classList.remove('active');
|
||||
if (btn.getAttribute('data-lang') === currentLang) {
|
||||
btn.classList.add('active');
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
// Initialize on page load
|
||||
document.addEventListener('DOMContentLoaded', async () => {
|
||||
await loadTranslations();
|
||||
translatePage();
|
||||
|
||||
// Setup language switcher
|
||||
document.querySelectorAll('.lang-switch').forEach(btn => {
|
||||
btn.addEventListener('click', () => {
|
||||
const lang = btn.getAttribute('data-lang');
|
||||
setLanguage(lang);
|
||||
});
|
||||
});
|
||||
});
|
||||
Reference in New Issue
Block a user