File size: 1,737 Bytes
11452ec
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
import fs from 'fs-extra';
import path from 'path';
import xliffToJSON from 'xliff-to-json';

async function recFindByExt(base,ext,files,result)
{
    files = files || (await fs.readdir(base))
    result = result || []

    for (const file of files) {
        var newbase = path.join(base,file)
        if ( (await fs.stat(newbase)).isDirectory() )
        {
            result = await recFindByExt(newbase,ext,await fs.readdir(newbase),result)
        }
        else
        {
            if ( file.substr(-1*(ext.length+1)) == '.' + ext )
            {
                result.push(newbase)
            }
        }
    }
    return result
}

// outputs array of supported locales
async function createLocalizationJSON() {
    xliffToJSON.convert('src/assets/i18n');
    const files = await recFindByExt(path.join('src', 'assets', 'i18n'), 'json');
    const locales = [];

    for (let i = 0; i < files.length; i++) {
        const file = path.basename(files[i]);
        const file_parts = file.split('.');
        if (file_parts.length !== 3 || file_parts[1] === 'en') continue;
        try {
            const locale_json = fs.readJSONSync(files[i]);
            const locale_json_keys = Object.keys(locale_json);
            let has_defined_keys = false;
            for (let i = 0; i < locale_json_keys.length; i++) {
                if (locale_json[locale_json_keys[i]] !== '') has_defined_keys = true;
            }
            if (has_defined_keys) locales.push(file_parts[1]);
        } catch (err) {
            console.error(err);
        }
    }

    fs.unlinkSync('src/assets/i18n/messages.en.json');
    fs.writeJSONSync('src/assets/i18n/supported_locales.json', {supported_locales: locales});
}

createLocalizationJSON();