-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtranslate.config.js
108 lines (90 loc) · 2.91 KB
/
translate.config.js
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
const fs = require('fs');
const path = require('path');
// Function to read and parse JSON from a file
function getJson(file) {
const configJsonPath = path.join(__dirname, file);
const data = fs.readFileSync(configJsonPath, 'utf8');
return JSON.parse(data);
}
// Function to create a directory if it doesn't exist
function createDirectoryIfNotExists(dirPath) {
if (!fs.existsSync(dirPath)) {
fs.mkdirSync(dirPath, { recursive: true });
}
}
// Function to generate navbar translations
function createNavbarTranslations(langData) {
const navbarData = {};
const navbar = langData.navbar || {};
for (const key in navbar) {
const value = navbar[key];
navbarData[`item.label.${key}`] = {
"message": value,
"description": `Navbar item with label ${key}`
};
}
return navbarData;
}
// Function to generate current translations
function createCurrentTranslations(langData) {
const currentData = {
"version.label": {
"message": "Next",
"description": "The label for version current"
}
};
const sidebars = langData.sidebars || {};
for (const sidebarName in sidebars) {
const categories = sidebars[sidebarName];
for (const key in categories) {
const value = categories[key];
currentData[`sidebar.${sidebarName}.category.${key}`] = {
"message": value,
"description": `The label for category ${key} in sidebar ${sidebarName}`
};
}
}
return currentData;
}
// Function to write JSON data to a file, ensuring the file and its directories exist
function writeJsonFile(filePath, data) {
// Ensure the directory exists
const dir = path.dirname(filePath);
if (!fs.existsSync(dir)) {
fs.mkdirSync(dir, { recursive: true });
}
// Write the JSON data to the file
fs.writeFileSync(filePath, JSON.stringify(data, null, 2));
}
// Function to process translations for a single language
function processLanguage(lang, data) {
const langData = data[lang];
// Create directory for the language inside 'i18n' folder
const langDir = path.join('i18n', lang);
createDirectoryIfNotExists(langDir);
// Generate and write navbar.json
const navbarData = createNavbarTranslations(langData);
writeJsonFile(path.join(langDir, 'docusaurus-theme-classic/navbar.json'), navbarData);
// Generate and write current.json
const currentData = createCurrentTranslations(langData);
writeJsonFile(path.join(langDir, 'docusaurus-plugin-content-docs/current.json'), currentData);
}
// Main function to orchestrate the processing
function main() {
const translationFilePath = 'translations.json';
let data;
try {
data = getJson(translationFilePath);
} catch (err) {
console.error(`Error reading or parsing ${translationFilePath}:`, err);
process.exit(1);
}
if (data) {
// Process each language in the translations
for (const lang in data) {
processLanguage(lang, data);
}
}
}
// Execute the main function
main();