All files / backend localeTools.js

36.84% Statements 14/38
40% Branches 6/15
44.44% Functions 4/9
37.5% Lines 12/32

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 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 731x 1x                                                                                           6x 6x   12x   12x 6x     6x 6x         6x   6x         1x      
const fs = require("fs").promises
const path = require("path")
 
async function saveLocale(translations, localeFolder) {
    // Turn translations into locales
    const locales = []
    translations.forEach(translation => {
        // For each localization of the translation
        for (let localeKey in translation.value) {
            let locale = locales.find(x => x.locale === localeKey)
            if (!locale) {
                // Create new locale if missing
                locale = {
                    locale: localeKey,
                    files: []
                }
                locales.push(locale)
            }
            // Check for existence of file
            if (!locale.files.find(x => x.name === translation.file)) {
                locale.files.push({
                    name: translation.file,
                    content: {}
                })
            }
            // Add translation to file content
            locale.files.find(x => x.name === translation.file).content[translation.key] = translation.value[localeKey].value || ""
        }
    })
 
    // Ensure folder structure exists
    for (let locale of locales) {
        await fs.mkdir(path.join(localeFolder, locale.locale), { recursive: true })
    }
 
    // Write locale files
    for (let locale of locales) {
        for (let file of locale.files) {
            let filepath = path.join(localeFolder, locale.locale, file.name)
            let content = JSON.stringify(file.content, null, 4)
            let original = await fs.readFile(filepath)
            if (original !== content) await fs.writeFile(filepath, content)
        }
    }
}
 
async function findLocaleFolder(directory, folder) {
    let foundLocaleFolder = false
    return findLocaleFolderRecurse(directory, folder)
    async function findLocaleFolderRecurse(directory, folder) {
        Iif (foundLocaleFolder || !folder || ["node_modules", ".git", "dist", "build"].includes(folder)) {
            return false
        } else if (folder == "locale") {
            return path.join(directory, "locale")
        } else {
            // Recurse
            try {
                var dirs = await fs.readdir(path.join(directory, folder))
            } catch (e) {
                // No permission or probably a file
                return false
            }
            let results = await Promise.all(dirs.map(dir => findLocaleFolderRecurse(path.join(directory, folder), dir)))
            // console.log(dirs, results)
            return results.find(locale_path => locale_path)
        }
    }
}
 
module.exports = {
    saveLocale,
    findLocaleFolder,
}