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}`); });