All files / library index.js

100% Statements 22/22
100% Branches 4/4
100% Functions 8/8
100% Lines 20/20

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 431x   1x 1x       6x 6x     6x   6x 6x 6x   11x     10x 10x 40x           10x 10x     10x 40x 10x     13x       1x  
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