UNPKG

crowdfree

Version:

A crowdin compatible tool for translation and localisation of websites and applications

43 lines (38 loc) 1.31 kB
const Polyglot = require("node-polyglot") const { findLocaleFolder } = require("./../backend/localeTools") const { getTranslations } = require("./../backend/main") class Crowdfree { constructor() { this.locale = {} this.poly = new Polyglot() } async load(directory, folder) { const localeFolder = await findLocaleFolder(directory, folder) // console.log(localeFolder) const { translations, locales } = await getTranslations(localeFolder) this.translations = translations this.locales = locales this.setLanguage(this.localeCode || locales.map(x => x.locale).includes("en") ? "en" : locales[0].locale) } setLanguage(code) { this.localeCode = code this.locale = this.translations.map(x => { return { file: x.file, key: x.key, value: x.value[code]?.value, } }) this.poly.locale(code) this.updatePoly() } updatePoly(){ let phrases = {} this.locale.forEach(x => phrases[x.key] = x.value) this.poly.replace(phrases) } t(translationKey, options) { return this.poly.t(translationKey, options) } } module.exports = Crowdfree