#
zhou zhou
12 小时以前 58b41dc039c5c9d1b758c9e190a7c35225ad7585
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
const fs = require('fs');
const path = require('path');
 
function unflatten(data) {
    const result = {};
 
    // Sort keys so that base objects are created before properties are added
    const keys = Object.keys(data);
 
    for (const key of keys) {
        const value = data[key];
 
        // If it's already an object, merge its keys
        if (typeof value === 'object' && value !== null && !Array.isArray(value)) {
            if (!result[key] || typeof result[key] !== 'object') {
                result[key] = result[key] || {};
            }
            Object.assign(result[key], value);
        } else {
            // Split by dot and nest
            const parts = key.split('.');
            let current = result;
 
            for (let i = 0; i < parts.length - 1; i++) {
                const part = parts[i];
                if (!current[part] || typeof current[part] !== 'object') {
                    current[part] = {};
                }
                current = current[part];
            }
            current[parts[parts.length - 1]] = value;
        }
    }
 
    return result;
}
 
const dir = path.join(__dirname, 'locale');
const files = fs.readdirSync(dir).filter(f => f.endsWith('.json'));
 
files.forEach(file => {
    const filePath = path.join(dir, file);
    let content = fs.readFileSync(filePath, 'utf8');
    let data;
    try {
        data = JSON.parse(content);
    } catch (e) {
        console.log(`Error parsing ${file}`);
        return;
    }
 
    const nestedData = unflatten(data);
    fs.writeFileSync(filePath, JSON.stringify(nestedData, null, 2), 'utf8');
    console.log(`Updated ${file}`);
});