-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathadd_new_translation.ts
145 lines (117 loc) · 3.02 KB
/
add_new_translation.ts
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
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
import { readFileSync, writeFileSync } from 'fs';
import inquirer from 'inquirer';
import { join } from 'path';
import * as readline from 'readline';
const MESSAGES_DIR = './src/i18n';
const rl = readline.createInterface({
input: process.stdin,
output: process.stdout
});
function saveToTranslationFile(data: Record<string, string>, languages: string[]) {
for (const lang of languages) {
const filePath = join(MESSAGES_DIR, `${lang}.json`);
let content = readFileSync(filePath, 'utf-8');
const newTranslation = `"${data.key}": "${data[lang]}"`;
// Remove the last curly brace
content = content.replace(/}$/, '');
// Remove the last line(s) if they're empty
content = content.trimEnd();
// Add a comma to the last line
content += ',';
// Add the new translation
content += `\n\t${newTranslation}`;
// Add the last curly brace
content += '\n}';
writeFileSync(filePath, content);
}
}
async function singleAdd() {
const languages = ['de', 'en'];
const prompts = [
{
type: 'input',
name: 'key',
message: 'Enter the key:'
}
];
for (const lang of languages) {
prompts.push({
type: 'input',
name: lang,
message: `Enter the translation for ${lang}:`
});
}
const prompt = inquirer.createPromptModule();
const data = await prompt(prompts);
if (!data.key) {
console.error('Key is required');
process.exit(1);
}
saveToTranslationFile(data, languages);
console.log('Translations added!');
}
async function bulkAdd() {
console.log('The translations should be in the following format:');
console.log(' key;de;en');
console.log(' key2;de;en');
console.log(' ...');
const languages = ['de', 'en'];
const data = await inquirer.prompt([
{
type: 'input',
name: 'translations',
message: 'Enter the translations:'
}
]);
for (const line of data.translations.split('\n')) {
const translations: Record<string, string> = {};
const [key, ...values] = line.split(';');
if (!key) {
console.error('Key is required');
process.exit(1);
}
translations.key = key;
for (let i = 0; i < languages.length; i++) {
const lang = languages[i];
const translation = values[i] || '';
translations[lang] = translation;
}
saveToTranslationFile(translations, languages);
}
}
async function main() {
const data = await inquirer.prompt([
{
type: 'select',
name: 'singleOrMultiple',
message: 'Do you want to add a single translation or multiple translations?',
choices: ['Single', 'Multiple'],
default: 'Single'
}
]);
if (data.singleOrMultiple === 'Multiple') {
await bulkAdd();
} else {
await singleAdd();
}
// Ask if the user wants to add another translation
const answer = await inquirer.prompt([
{
type: 'confirm',
name: 'continue',
message: 'Do you want to add another translation?',
default: true
}
]);
return answer.continue;
}
let continueLoop = true;
while (continueLoop) {
try {
continueLoop = await main();
} catch (error) {
console.error('An error occurred:', error);
}
}
console.log('Exiting...');
process.exit(0);