* chore(localization): remove duplicated locale files * feat(localization): add localization for breadcrumbs * feat(localization): add beadcrumb enhancements * feat(debugging): add npm run debug command * chore(localization): add unit tests * chore(breadcrumbs): fix tests and typos * chore(localization): add docs for translationAvailable * chore(localization): refactor translationAvailable
58 lines
1.4 KiB
JavaScript
58 lines
1.4 KiB
JavaScript
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, '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
|
|
}
|
|
|
|
/**
|
|
* Checks if string is translatable
|
|
* @param symbol string Identifier for static text
|
|
* @param locale string Locale code
|
|
*
|
|
* @returns boolean
|
|
*/
|
|
module.exports.translationAvaliable = (symbol, locale = 'en-US') => {
|
|
return !!(translations[locale] || {})[symbol]
|
|
}
|