Files
novafarma/src/systems/LocalizationSystem.js
2025-12-12 13:40:51 +01:00

319 lines
10 KiB
JavaScript

/**
* LOCALIZATION SYSTEM
* Multi-language support with JSON translation files
*/
class LocalizationSystem {
constructor() {
this.currentLang = 'en'; // Default language
this.translations = {};
this.supportedLanguages = ['slo', 'en', 'de', 'it', 'cn'];
// Load from localStorage
const savedLang = localStorage.getItem('novafarma_language');
if (savedLang && this.supportedLanguages.includes(savedLang)) {
this.currentLang = savedLang;
}
this.loadTranslations();
}
loadTranslations() {
// Embedded translations (inline for simplicity)
this.translations = {
'slo': {
// UI
'ui.inventory': 'Inventar',
'ui.crafting': 'Izdelovanje',
'ui.health': 'Zdravje',
'ui.hunger': 'Lakota',
'ui.oxygen': 'Kisik',
'ui.day': 'Dan',
'ui.season': 'Letni čas',
'ui.hp': 'ZDR',
'ui.hun': 'LAK',
'ui.h2o': 'H2O',
'ui.xp': 'IZK',
'ui.lv': 'NIV',
// Items
'item.wood': 'Les',
'item.stone': 'Kamen',
'item.seeds': 'Semena',
'item.wheat': 'Pšenica',
'item.corn': 'Koruza',
// Actions
'action.plant': 'Posadi',
'action.harvest': 'Požanji',
'action.craft': 'Izdelaj',
'action.build': 'Zgradi',
// Seasons
'season.spring': 'Pomlad',
'season.summer': 'Poletje',
'season.autumn': 'Jesen',
'season.winter': 'Zima',
// Pause Menu
'pause.title': 'PAVZA',
'pause.resume': '▶ Nadaljuj',
'pause.save': '💾 Shrani igro',
'pause.settings': '⚙️ Nastavitve',
'pause.quit': '🚪 Izhod v meni',
'pause.hint': 'Pritisni ESC za nadaljevanje',
// Messages
'msg.demo_end': 'Demo končan! Hvala za igranje.',
'msg.freezing': '❄️ Zmrzuješ!',
'msg.overheating': '🔥 Pregrevanje!'
},
'en': {
// UI
'ui.inventory': 'Inventory',
'ui.crafting': 'Crafting',
'ui.health': 'Health',
'ui.hunger': 'Hunger',
'ui.oxygen': 'Oxygen',
'ui.day': 'Day',
'ui.season': 'Season',
'ui.hp': 'HP',
'ui.hun': 'HUN',
'ui.h2o': 'H2O',
'ui.xp': 'XP',
'ui.lv': 'LV',
// Items
'item.wood': 'Wood',
'item.stone': 'Stone',
'item.seeds': 'Seeds',
'item.wheat': 'Wheat',
'item.corn': 'Corn',
// Actions
'action.plant': 'Plant',
'action.harvest': 'Harvest',
'action.craft': 'Craft',
'action.build': 'Build',
// Seasons
'season.spring': 'Spring',
'season.summer': 'Summer',
'season.autumn': 'Autumn',
'season.winter': 'Winter',
// Pause Menu
'pause.title': 'PAUSED',
'pause.resume': '▶ Resume',
'pause.save': '💾 Save Game',
'pause.settings': '⚙️ Settings',
'pause.quit': '🚪 Quit to Menu',
'pause.hint': 'Press ESC to resume',
// Messages
'msg.demo_end': 'Demo Ended! Thanks for playing.',
'msg.freezing': '❄️ Freezing!',
'msg.overheating': '🔥 Overheating!'
},
'de': {
// UI
'ui.inventory': 'Inventar',
'ui.crafting': 'Handwerk',
'ui.health': 'Gesundheit',
'ui.hunger': 'Hunger',
'ui.oxygen': 'Sauerstoff',
'ui.day': 'Tag',
'ui.season': 'Jahreszeit',
'ui.hp': 'LP',
'ui.hun': 'HUN',
'ui.h2o': 'H2O',
'ui.xp': 'EP',
'ui.lv': 'ST',
// Items
'item.wood': 'Holz',
'item.stone': 'Stein',
'item.seeds': 'Samen',
'item.wheat': 'Weizen',
'item.corn': 'Mais',
// Actions
'action.plant': 'Pflanzen',
'action.harvest': 'Ernten',
'action.craft': 'Herstellen',
'action.build': 'Bauen',
// Seasons
'season.spring': 'Frühling',
'season.summer': 'Sommer',
'season.autumn': 'Herbst',
'season.winter': 'Winter',
// Pause Menu
'pause.title': 'PAUSIERT',
'pause.resume': '▶ Fortsetzen',
'pause.save': '💾 Spiel speichern',
'pause.settings': '⚙️ Einstellungen',
'pause.quit': '🚪 Zum Menü',
'pause.hint': 'ESC drücken zum Fortsetzen',
// Messages
'msg.demo_end': 'Demo beendet! Danke fürs Spielen.',
'msg.freezing': '❄️ Du erfrierst!',
'msg.overheating': '🔥 Überhitzung!'
},
'it': {
// UI
'ui.inventory': 'Inventario',
'ui.crafting': 'Artigianato',
'ui.health': 'Salute',
'ui.hunger': 'Fame',
'ui.oxygen': 'Ossigeno',
'ui.day': 'Giorno',
'ui.season': 'Stagione',
'ui.hp': 'PS',
'ui.hun': 'FAM',
'ui.h2o': 'H2O',
'ui.xp': 'ESP',
'ui.lv': 'LIV',
// Items
'item.wood': 'Legno',
'item.stone': 'Pietra',
'item.seeds': 'Semi',
'item.wheat': 'Grano',
'item.corn': 'Mais',
// Actions
'action.plant': 'Pianta',
'action.harvest': 'Raccogli',
'action.craft': 'Crea',
'action.build': 'Costruisci',
// Seasons
'season.spring': 'Primavera',
'season.summer': 'Estate',
'season.autumn': 'Autunno',
'season.winter': 'Inverno',
// Pause Menu
'pause.title': 'IN PAUSA',
'pause.resume': '▶ Riprendi',
'pause.save': '💾 Salva gioco',
'pause.settings': '⚙️ Impostazioni',
'pause.quit': '🚪 Esci al menu',
'pause.hint': 'Premi ESC per riprendere',
// Messages
'msg.demo_end': 'Demo terminata! Grazie per aver giocato.',
'msg.freezing': '❄️ Stai congelando!',
'msg.overheating': '🔥 Surriscaldamento!'
},
'cn': {
// UI
'ui.inventory': '库存',
'ui.crafting': '制作',
'ui.health': '健康',
'ui.hunger': '饥饿',
'ui.oxygen': '氧气',
'ui.day': '天数',
'ui.season': '季节',
'ui.hp': '生命',
'ui.hun': '饥饿',
'ui.h2o': '水',
'ui.xp': '经验',
'ui.lv': '等级',
// Items
'item.wood': '木材',
'item.stone': '石头',
'item.seeds': '种子',
'item.wheat': '小麦',
'item.corn': '玉米',
// Actions
'action.plant': '种植',
'action.harvest': '收获',
'action.craft': '制作',
'action.build': '建造',
// Seasons
'season.spring': '春天',
'season.summer': '夏天',
'season.autumn': '秋天',
'season.winter': '冬天',
// Pause Menu
'pause.title': '暂停',
'pause.resume': '▶ 继续',
'pause.save': '💾 保存游戏',
'pause.settings': '⚙️ 设置',
'pause.quit': '🚪 退出到菜单',
'pause.hint': '按ESC继续',
// Messages
'msg.demo_end': '演示结束!感谢游玩。',
'msg.freezing': '❄️ 你在冻僵!',
'msg.overheating': '🔥 过热!'
}
};
}
/**
* Get translated string
* @param {string} key - Translation key (e.g. 'ui.inventory')
* @param {string} fallback - Fallback text if translation missing
*/
t(key, fallback = key) {
const lang = this.translations[this.currentLang];
if (lang && lang[key]) {
return lang[key];
}
// Fallback to English
const enLang = this.translations['en'];
if (enLang && enLang[key]) {
return enLang[key];
}
return fallback;
}
/**
* Set current language
*/
setLanguage(langCode) {
if (this.supportedLanguages.includes(langCode)) {
this.currentLang = langCode;
localStorage.setItem('novafarma_language', langCode);
console.log(`🌍 Language changed to: ${langCode}`);
return true;
}
return false;
}
getCurrentLanguage() {
return this.currentLang;
}
getSupportedLanguages() {
return this.supportedLanguages.map(code => ({
code,
name: this.getLanguageName(code)
}));
}
getLanguageName(code) {
const names = {
'slo': 'Slovenščina',
'en': 'English',
'de': 'Deutsch',
'it': 'Italiano',
'cn': '中文'
};
return names[code] || code;
}
}
// Global instance
window.LocalizationSystem = LocalizationSystem;