Skip to content

Commit

Permalink
Create multiLanguageSupport.js
Browse files Browse the repository at this point in the history
  • Loading branch information
KOSASIH authored Dec 4, 2024
1 parent 094cbc7 commit 0a5857a
Showing 1 changed file with 50 additions and 0 deletions.
50 changes: 50 additions & 0 deletions src/ui/multiLanguageSupport.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
// multiLanguageSupport.js

const fs = require('fs');
const path = require('path');

// Supported languages
const supportedLanguages = ['en', 'es', 'fr', 'de', 'zh'];

// Load language files
function loadLanguageFile(language) {
const filePath = path.join(__dirname, 'languages', `${language}.json`);
if (fs.existsSync(filePath)) {
return JSON.parse(fs.readFileSync(filePath, 'utf-8'));
} else {
console.warn(`Language file for ${language} not found. Falling back to English.`);
return loadLanguageFile('en'); // Fallback to English
}
}

// Language data
let currentLanguage = 'en';
let translations = loadLanguageFile(currentLanguage);

// Change language
function changeLanguage(language) {
if (supportedLanguages.includes(language)) {
currentLanguage = language;
translations = loadLanguageFile(currentLanguage);
console.log(`Language changed to ${currentLanguage}`);
} else {
console.warn(`Language ${language} is not supported.`);
}
}

// Translate a key
function translate(key) {
return translations[key] || key; // Return the key if translation is not found
}

// Get the current language
function getCurrentLanguage() {
return currentLanguage;
}

// Exporting functions for use in other modules
module.exports = {
changeLanguage,
translate,
getCurrentLanguage,
};

0 comments on commit 0a5857a

Please sign in to comment.