* Bulk add multiple strings from a JSON file to ALL language files * Usage: pnpm lang bulk-add strings.json * * JSON file format: * { * "key1": "value1", * "key2": "value2" * }
()
| 111 | * } |
| 112 | */ |
| 113 | function bulkAddStrings() { |
| 114 | if (!arg) { |
| 115 | console.error("Usage: pnpm lang bulk-add <json-file>"); |
| 116 | console.error("Example: pnpm lang bulk-add new-strings.json"); |
| 117 | console.error("\nJSON file format:"); |
| 118 | console.error("{"); |
| 119 | console.error(' "key1": "value1",'); |
| 120 | console.error(' "key2": "value2"'); |
| 121 | console.error("}"); |
| 122 | process.exit(1); |
| 123 | } |
| 124 | |
| 125 | const jsonFilePath = path.resolve(process.cwd(), arg); |
| 126 | |
| 127 | if (!fs.existsSync(jsonFilePath)) { |
| 128 | console.error(`File not found: ${jsonFilePath}`); |
| 129 | process.exit(1); |
| 130 | } |
| 131 | |
| 132 | let newStrings; |
| 133 | try { |
| 134 | const jsonContent = fs.readFileSync(jsonFilePath, "utf8"); |
| 135 | newStrings = JSON.parse(jsonContent); |
| 136 | } catch (err) { |
| 137 | console.error(`Error parsing JSON file: ${err.message}`); |
| 138 | process.exit(1); |
| 139 | } |
| 140 | |
| 141 | const keys = Object.keys(newStrings); |
| 142 | if (keys.length === 0) { |
| 143 | console.error("No strings found in the JSON file."); |
| 144 | process.exit(1); |
| 145 | } |
| 146 | |
| 147 | console.log( |
| 148 | `Adding ${keys.length} strings to ${list.length} language files...\n`, |
| 149 | ); |
| 150 | |
| 151 | for (const lang of list) { |
| 152 | const file = path.resolve(dir, lang); |
| 153 | const text = fs.readFileSync(file, "utf8"); |
| 154 | const strings = JSON.parse(text); |
| 155 | let addedCount = 0; |
| 156 | let skippedCount = 0; |
| 157 | |
| 158 | for (const key of keys) { |
| 159 | const lowerKey = key.toLowerCase(); |
| 160 | if (lowerKey in strings) { |
| 161 | skippedCount++; |
| 162 | continue; |
| 163 | } |
| 164 | strings[lowerKey] = newStrings[key]; |
| 165 | addedCount++; |
| 166 | } |
| 167 | |
| 168 | if (addedCount > 0) { |
| 169 | const newText = JSON.stringify(strings, undefined, 2); |
| 170 | fs.writeFileSync(file, newText, "utf8"); |
no test coverage detected