All files / backend main.js

53.06% Statements 26/49
25% Branches 3/12
53.33% Functions 8/15
54.55% Lines 24/44

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 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154                                                                              1x 1x   1x 1x       6x   11x 11x     11x                                                                                                   11x         11x               6x 6x 24x 6x 6x     6x 6x 11x 11x 86x 44x   20x         24x                             1x      
/*
    Locales look like this:
    {
        locale: "en",
        files: [{
            name: "locale.json",
            content: {
                name: "Crowdfree",
                description: "Simple localisations without all the fuss"
            }
        }]
    }
 
    A translation should look like this:
    {
        file: "locale.json",
        key: "headertext",
        value: {
            en: {
                value: "English header text",
            },
            no: {
                value: "Norwegian header text",
            },
            es: {
                value: false, // This one isn't translated yet, but is expected due to finding the locale folder
                suggestions: [{
                    type: "google_translate",
                    value: "Habla es espanol?"
                },{
                    type: "similar_source_translation",
                    value: "'en' value is similar to the 'en' value for another string in the project"
                }]
            }
        }
    }
*/
 
// const fs = require("fs-extra")
const fs = require("fs").promises
const path = require("path")
 
const { saveLocale, findLocaleFolder } = require("./localeTools")
const sortTranslations = require("./../frontend/src/util/sortTranslations")
 
async function getTranslations(localeFolder, updatesCallback) {
    // Load locale from files
    const locales = await Promise.all((await fs.readdir(localeFolder))
        .map(async folderName => {
            let localePath = path.join(localeFolder, folderName)
            let files = (await fs.readdir(localePath))
                .map(async file => {
                    // Handle changes in files after first start (after function exit)
                    Iif (updatesCallback) {
                        let watcher = fs.watch(path.join(localePath, file));
                        (async () => {
                            let waitingForRead = false
                            try {
                                for await (const event of watcher) {
                                    if (!waitingForRead) {
                                        // console.log(event)
                                        waitingForRead = true;
                                        setTimeout(async () => {
                                            let newFile = {
                                                name: file,
                                                content: JSON.parse(await fs.readFile(path.join(localePath, file)))
                                            }
                                            waitingForRead = false
 
                                            // Update locale with file
                                            locales.find(x => x.locale === folderName)
                                                .files.find(x => x.name === file)
                                                .content = newFile.content
 
                                            let oldTranslations = JSON.parse(JSON.stringify(translations))
                                            // Update translaitons
                                            populateTranslations(translations, locales)
                                            sortTranslations(translations)
 
                                            // Inform callee about updated translations
                                            let updated = translations.filter(translation => oldTranslations
                                                .find(x =>
                                                    x.file === translation.file
                                                    && x.key === translation.key
                                                    && Object.keys(x.value).find(y =>
                                                        // Value changed
                                                        x.value[y].value != translation.value[y]?.value
                                                        // Either new or old value has to be a string for there to be a change
                                                        && (typeof x.value[y].value === "string" || typeof translation.value[y]?.value === "string")
                                                    )
                                                )
                                            )
                                            updatesCallback?.(updated)
                                        }, 1000)
                                    }
                                }
                            } catch (e) {
                                console.error(e)
                            }
                        })()
                    }
 
                    // Return file contents (at function call)
                    return {
                        name: file,
                        content: JSON.parse(await fs.readFile(path.join(localePath, file)))
                    }
                })
            return {
                locale: folderName,
                files: await Promise.all(files),
            }
        }));
 
    // console.log(JSON.stringify(locales, null, 2))
 
    const translations = []
    populateTranslations(translations, locales)
    translations.forEach(x => x.timestamp = Date.now())
    sortTranslations(translations)
    return { translations, locales }
}
function populateTranslations(translations, locales) {
    while (translations.length) translations.shift()
    locales.forEach(locale => {
        locale.files.forEach(file => {
            for (let key in file.content) {
                let find = translations.find(translation => translation.key === key)
                if (find) {
                    // Extend existing translation
                    find.value[locale.locale] = {
                        value: file.content[key]
                    }
                } else {
                    // Add new translation
                    translations.push({
                        file: file.name,
                        key,
                        value: {
                            [locale.locale]: {
                                value: file.content[key]
                            }
                        }
                    })
                }
            }
        })
    })
}
 
module.exports = {
    getTranslations,
}