Skip to content

Commit a413ccd

Browse files
committed
feat: Add new string addition to lang util
1 parent 55156b7 commit a413ccd

File tree

1 file changed

+127
-1
lines changed

1 file changed

+127
-1
lines changed

utils/lang.js

Lines changed: 127 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,9 @@ const yargs = require("yargs");
44
const { hideBin } = require("yargs/helpers");
55
const readline = require("node:readline");
66

7-
const args = yargs(hideBin(process.argv)).alias("a", "all").argv;
7+
const args = yargs(hideBin(process.argv))
8+
.alias("a", "all")
9+
.alias("b", "bulk").argv;
810
const dir = path.resolve(__dirname, "../src/lang");
911
const read = readline.createInterface({
1012
input: process.stdin,
@@ -35,9 +37,17 @@ switch (command) {
3537
case "check":
3638
update();
3739
break;
40+
case "add-all":
41+
addToAllFiles();
42+
break;
43+
case "bulk-add":
44+
bulkAddStrings();
45+
break;
3846
default:
3947
console.error(`Missing/Invalid arguments.
4048
use 'add' to add a new string
49+
use 'add-all <key> <value>' to add the same string to ALL language files at once
50+
use 'bulk-add <json-file>' to add multiple strings from a JSON file to all language files
4151
use 'remove' to remove a string
4252
use 'search' to search a string
4353
use 'update' to update a string
@@ -46,6 +56,122 @@ use 'check' to check a string`);
4656
process.exit();
4757
}
4858

59+
/**
60+
* Adds a key-value pair to ALL language files at once
61+
* Usage: pnpm lang add-all "key" "value"
62+
*/
63+
function addToAllFiles() {
64+
if (!arg || !val) {
65+
console.error('Usage: pnpm lang add-all "<key>" "<value>"');
66+
console.error('Example: pnpm lang add-all "hello world" "Hello World"');
67+
process.exit(1);
68+
}
69+
70+
const key = arg.toLowerCase();
71+
let addedCount = 0;
72+
let skippedCount = 0;
73+
74+
for (const lang of list) {
75+
const file = path.resolve(dir, lang);
76+
const text = fs.readFileSync(file, "utf8");
77+
const strings = JSON.parse(text);
78+
79+
if (key in strings) {
80+
console.log(`${lang}: Skipped (already exists)`);
81+
skippedCount++;
82+
continue;
83+
}
84+
85+
strings[key] = val;
86+
const newText = JSON.stringify(strings, undefined, 2);
87+
fs.writeFileSync(file, newText, "utf8");
88+
console.log(`${lang}: Added ✓`);
89+
addedCount++;
90+
}
91+
92+
console.log(
93+
`\nDone! Added to ${addedCount} files, skipped ${skippedCount} files.`,
94+
);
95+
process.exit(0);
96+
}
97+
98+
/**
99+
* Bulk add multiple strings from a JSON file to ALL language files
100+
* Usage: pnpm lang bulk-add strings.json
101+
*
102+
* JSON file format:
103+
* {
104+
* "key1": "value1",
105+
* "key2": "value2"
106+
* }
107+
*/
108+
function bulkAddStrings() {
109+
if (!arg) {
110+
console.error("Usage: pnpm lang bulk-add <json-file>");
111+
console.error("Example: pnpm lang bulk-add new-strings.json");
112+
console.error("\nJSON file format:");
113+
console.error("{");
114+
console.error(' "key1": "value1",');
115+
console.error(' "key2": "value2"');
116+
console.error("}");
117+
process.exit(1);
118+
}
119+
120+
const jsonFilePath = path.resolve(process.cwd(), arg);
121+
122+
if (!fs.existsSync(jsonFilePath)) {
123+
console.error(`File not found: ${jsonFilePath}`);
124+
process.exit(1);
125+
}
126+
127+
let newStrings;
128+
try {
129+
const jsonContent = fs.readFileSync(jsonFilePath, "utf8");
130+
newStrings = JSON.parse(jsonContent);
131+
} catch (err) {
132+
console.error(`Error parsing JSON file: ${err.message}`);
133+
process.exit(1);
134+
}
135+
136+
const keys = Object.keys(newStrings);
137+
if (keys.length === 0) {
138+
console.error("No strings found in the JSON file.");
139+
process.exit(1);
140+
}
141+
142+
console.log(
143+
`Adding ${keys.length} strings to ${list.length} language files...\n`,
144+
);
145+
146+
for (const lang of list) {
147+
const file = path.resolve(dir, lang);
148+
const text = fs.readFileSync(file, "utf8");
149+
const strings = JSON.parse(text);
150+
let addedCount = 0;
151+
let skippedCount = 0;
152+
153+
for (const key of keys) {
154+
const lowerKey = key.toLowerCase();
155+
if (lowerKey in strings) {
156+
skippedCount++;
157+
continue;
158+
}
159+
strings[lowerKey] = newStrings[key];
160+
addedCount++;
161+
}
162+
163+
if (addedCount > 0) {
164+
const newText = JSON.stringify(strings, undefined, 2);
165+
fs.writeFileSync(file, newText, "utf8");
166+
}
167+
168+
console.log(`${lang}: Added ${addedCount}, Skipped ${skippedCount}`);
169+
}
170+
171+
console.log(`\nDone! Added ${keys.length} strings to all language files.`);
172+
process.exit(0);
173+
}
174+
49175
async function update() {
50176
let key;
51177

0 commit comments

Comments
 (0)