feat(localization): add localization for static strings
This commit is contained in:
committed by
Benedikt Rötsch
parent
eaec09a594
commit
68a8052bdf
46
i18n/i18n.js
Normal file
46
i18n/i18n.js
Normal file
@@ -0,0 +1,46 @@
|
||||
const fs = require('fs')
|
||||
const path = require('path')
|
||||
|
||||
let translations = null
|
||||
// Initializes translation dictionary with contents from /public/locales
|
||||
module.exports.initializeTranslations = () => {
|
||||
if (translations) {
|
||||
return
|
||||
}
|
||||
|
||||
translations = {}
|
||||
|
||||
const localesPath = path.join(__dirname, '..', 'public', 'locales')
|
||||
|
||||
try {
|
||||
const files = fs.readdirSync(localesPath)
|
||||
|
||||
files.forEach((file) => {
|
||||
const localeDict = require(path.join(localesPath, file))
|
||||
translations[file.replace('.json', '')] = localeDict
|
||||
})
|
||||
} catch (error) {
|
||||
console.error('Error loading localization files:')
|
||||
console.error(error)
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Translate a static string
|
||||
* @param symbol string Identifier for static text
|
||||
* @param locale string Locale code
|
||||
*
|
||||
* @returns string
|
||||
*/
|
||||
module.exports.translate = (symbol, locale = 'en-US') => {
|
||||
const localeDict = translations[locale]
|
||||
if (!localeDict) {
|
||||
return `Localization file for ${locale} is not available`
|
||||
}
|
||||
const translatedValue = localeDict[symbol]
|
||||
if (!translatedValue) {
|
||||
return `Translation not found for ${symbol} in ${locale}`
|
||||
}
|
||||
|
||||
return translatedValue
|
||||
}
|
||||
Reference in New Issue
Block a user