Files
the-example-app-nodejs/i18n/i18n.js
Benedikt Rötsch 5a20ed0dbc new deployment (#59)
* Production (#44)

* test: Fix integration test

* fix: Fix Editorial feature

* chore(localization): move locales to i18n folder

* feat(modules): add image caption and shadow

* fix: clarify German translation🇩🇪

* chore(localization): fix typo (#47)

* Split first paragraph into sections (#46)

* feat(controls): add help text to API dropdown

* fix(style): simplify header and fix mobile design

* fix(style): simplify footer and fix mobile design

* fix(style): update stylesheet and scripts file

* test(e2e): adjust tests and make them fail on CI

* language fixes

* rename labels to use full api name

* Refactor entry state toggle (#48)

* chore(editorialFeatures): extract editorial features toggle

* chore(refactor): rename function to make more contextual sense

* Fix breadcrumb and add test for locales (#49)

* test(unit): add test to check for locale consistency

* fix(breadcrumb): translate lessons route

* Change readme url to http from https (#52)

* tests(e2e): expect only 2 courses

* fix(cookies): fix variable naming and extend to two days maxAge

* Change text on next lesson button (#54)

* Retranslate advancedLabel

* fix(translations): do not break on vim temp files. This is dedicated to JP

* fix(analytics): use correct app-id

* Add instructions to seed a space to readme (#61)

* Change modal text; add link to model's repo to localization files (#57)

* fix(links): link correct GH repository

* fix(i18n): adjust view on github link label

* fix(style): move GitHub link to the left

* new screenshot
2017-11-09 16:36:06 +01:00

59 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)
.filter((filename) => filename.endsWith('.json'))
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]
}