|
| 1 | +#!/usr/bin/env node |
| 2 | + |
| 3 | +/** |
| 4 | + * Merge translated text from a Snap .sps/.spb into the original database |
| 5 | + * while preserving original assets (images/audio/layout metadata). |
| 6 | + * |
| 7 | + * Usage: |
| 8 | + * node scripts/translation/merge-snap-assets.js \ |
| 9 | + * original.sps translated.sps \ |
| 10 | + * merged-with-assets.sps |
| 11 | + */ |
| 12 | + |
| 13 | +const fs = require('fs'); |
| 14 | +const path = require('path'); |
| 15 | +const Database = require('better-sqlite3'); |
| 16 | + |
| 17 | +function printUsage() { |
| 18 | + console.log( |
| 19 | + 'Usage: node scripts/translation/merge-snap-assets.js <original.sps> <translated.sps> [output.sps]' |
| 20 | + ); |
| 21 | + console.log( |
| 22 | + 'If no output path is supplied, the script will append "-with-assets" to the translated file name.' |
| 23 | + ); |
| 24 | +} |
| 25 | + |
| 26 | +function getTableColumns(db, tableName) { |
| 27 | + try { |
| 28 | + const rows = db.prepare(`PRAGMA table_info(${tableName})`).all(); |
| 29 | + return new Set(rows.map((row) => row.name)); |
| 30 | + } catch { |
| 31 | + return new Set(); |
| 32 | + } |
| 33 | +} |
| 34 | + |
| 35 | +async function main() { |
| 36 | + const [, , originalPath, translatedPath, outputArg] = process.argv; |
| 37 | + |
| 38 | + if (!originalPath || !translatedPath) { |
| 39 | + printUsage(); |
| 40 | + process.exit(1); |
| 41 | + } |
| 42 | + |
| 43 | + if (!fs.existsSync(originalPath)) { |
| 44 | + console.error(`Original file not found: ${originalPath}`); |
| 45 | + process.exit(1); |
| 46 | + } |
| 47 | + |
| 48 | + if (!fs.existsSync(translatedPath)) { |
| 49 | + console.error(`Translated file not found: ${translatedPath}`); |
| 50 | + process.exit(1); |
| 51 | + } |
| 52 | + |
| 53 | + const outputPath = |
| 54 | + outputArg || |
| 55 | + path.join( |
| 56 | + path.dirname(translatedPath), |
| 57 | + `${path.basename(translatedPath, path.extname(translatedPath))}-with-assets${path.extname(translatedPath)}` |
| 58 | + ); |
| 59 | + |
| 60 | + if (fs.existsSync(outputPath)) { |
| 61 | + fs.unlinkSync(outputPath); |
| 62 | + } |
| 63 | + fs.copyFileSync(originalPath, outputPath); |
| 64 | + |
| 65 | + const outDb = new Database(outputPath, { readonly: false }); |
| 66 | + const transDb = new Database(translatedPath, { readonly: true }); |
| 67 | + |
| 68 | + try { |
| 69 | + const outPageCols = getTableColumns(outDb, 'Page'); |
| 70 | + const transPageCols = getTableColumns(transDb, 'Page'); |
| 71 | + const outButtonCols = getTableColumns(outDb, 'Button'); |
| 72 | + const transButtonCols = getTableColumns(transDb, 'Button'); |
| 73 | + const outPropsCols = getTableColumns(outDb, 'PageSetProperties'); |
| 74 | + const transPropsCols = getTableColumns(transDb, 'PageSetProperties'); |
| 75 | + |
| 76 | + const canUpdatePage = |
| 77 | + outPageCols.size > 0 && |
| 78 | + transPageCols.size > 0 && |
| 79 | + outPageCols.has('Name') && |
| 80 | + transPageCols.has('Name'); |
| 81 | + const canUpdatePageTitle = outPageCols.has('Title') && transPageCols.has('Title'); |
| 82 | + const canUpdatePageByUniqueId = outPageCols.has('UniqueId') && transPageCols.has('UniqueId'); |
| 83 | + |
| 84 | + const canUpdateButtonLabel = |
| 85 | + outButtonCols.has('Label') && transButtonCols.has('Label') && outButtonCols.has('Id'); |
| 86 | + const canUpdateButtonMessage = |
| 87 | + outButtonCols.has('Message') && transButtonCols.has('Message') && outButtonCols.has('Id'); |
| 88 | + |
| 89 | + const tx = outDb.transaction(() => { |
| 90 | + if (canUpdatePage) { |
| 91 | + if (canUpdatePageByUniqueId) { |
| 92 | + const translatedPages = transDb |
| 93 | + .prepare('SELECT UniqueId, Name, Title FROM Page') |
| 94 | + .all(); |
| 95 | + const updatePage = outDb.prepare( |
| 96 | + 'UPDATE Page SET Name = ?, Title = ? WHERE UniqueId = ?' |
| 97 | + ); |
| 98 | + translatedPages.forEach((row) => { |
| 99 | + const name = row.Name ?? ''; |
| 100 | + const title = canUpdatePageTitle ? row.Title ?? name : name; |
| 101 | + updatePage.run(name, title, row.UniqueId); |
| 102 | + }); |
| 103 | + } else { |
| 104 | + const translatedPages = transDb.prepare('SELECT Id, Name, Title FROM Page').all(); |
| 105 | + const updatePage = outDb.prepare('UPDATE Page SET Name = ?, Title = ? WHERE Id = ?'); |
| 106 | + translatedPages.forEach((row) => { |
| 107 | + const name = row.Name ?? ''; |
| 108 | + const title = canUpdatePageTitle ? row.Title ?? name : name; |
| 109 | + updatePage.run(name, title, row.Id); |
| 110 | + }); |
| 111 | + } |
| 112 | + } |
| 113 | + |
| 114 | + if (canUpdateButtonLabel) { |
| 115 | + const translatedButtons = transDb.prepare('SELECT Id, Label FROM Button').all(); |
| 116 | + const updateLabel = outDb.prepare('UPDATE Button SET Label = ? WHERE Id = ?'); |
| 117 | + translatedButtons.forEach((row) => { |
| 118 | + updateLabel.run(row.Label ?? '', row.Id); |
| 119 | + }); |
| 120 | + } |
| 121 | + |
| 122 | + if (canUpdateButtonMessage) { |
| 123 | + const translatedButtons = transDb.prepare('SELECT Id, Message FROM Button').all(); |
| 124 | + const updateMessage = outDb.prepare('UPDATE Button SET Message = ? WHERE Id = ?'); |
| 125 | + translatedButtons.forEach((row) => { |
| 126 | + updateMessage.run(row.Message ?? '', row.Id); |
| 127 | + }); |
| 128 | + } |
| 129 | + |
| 130 | + if (outPropsCols.size > 0 && transPropsCols.size > 0) { |
| 131 | + const props = transDb.prepare('SELECT * FROM PageSetProperties LIMIT 1').get(); |
| 132 | + if (props) { |
| 133 | + const updates = []; |
| 134 | + const values = []; |
| 135 | + ['Name', 'Description', 'Author', 'Locale'].forEach((key) => { |
| 136 | + if (outPropsCols.has(key) && transPropsCols.has(key) && props[key] !== undefined) { |
| 137 | + updates.push(`${key} = ?`); |
| 138 | + values.push(props[key]); |
| 139 | + } |
| 140 | + }); |
| 141 | + if (updates.length > 0) { |
| 142 | + outDb.prepare(`UPDATE PageSetProperties SET ${updates.join(', ')}`).run(...values); |
| 143 | + } |
| 144 | + } |
| 145 | + } |
| 146 | + }); |
| 147 | + |
| 148 | + tx(); |
| 149 | + } finally { |
| 150 | + outDb.close(); |
| 151 | + transDb.close(); |
| 152 | + } |
| 153 | + |
| 154 | + console.log(`Merged Snap file written to ${outputPath}`); |
| 155 | +} |
| 156 | + |
| 157 | +main().catch((error) => { |
| 158 | + console.error('Failed to merge Snap assets:', error); |
| 159 | + process.exit(1); |
| 160 | +}); |
0 commit comments