This repository was archived by the owner on Oct 5, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathlanguageInit.js
More file actions
89 lines (74 loc) · 3.91 KB
/
languageInit.js
File metadata and controls
89 lines (74 loc) · 3.91 KB
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
const path = require('path');
const { fs, decodeBase64ToObject } = require('aql-utils');
const setLanguage = async (langs, defaultLanguage) => {
const tabLangs = langs.split(',');
const themeName = path.basename(__dirname);
global.aquila = decodeBase64ToObject(global.aquila);
const pathToTheme = path.join(global.aquila.appRoot, 'themes', themeName);
const i18nSamplePath = path.join(pathToTheme, 'i18n.js.sample');
const i18nFilePath = path.join(pathToTheme, 'i18n.js');
const json = require(i18nSamplePath);
json.locales = tabLangs; // Replace or create "locales" property
json.defaultLocale = defaultLanguage; // Replace or create "defaultLocale" property
// Process to retrieve each module folder
const anyPages = json.pages['*'];
const moduleThemePath = path.join(pathToTheme, 'modules');
if (fs.existsSync(moduleThemePath)) {
const moduleList = fs.readdirSync(path.join(pathToTheme, 'modules'), { withFileTypes: true })
.filter((item) => item.isDirectory())
.map((item) => item.name);
for (const m of moduleList) {
const modulePath = `modules/${m}`;
anyPages.push(modulePath);
}
}
json.pages['*'] = anyPages;
const file = fs.readFileSync(i18nSamplePath);
let res = file.toString().replace(/locales[\s\t]*:.*/, `locales: ${JSON.stringify(json.locales)},`);
res = res.replace(/defaultLocale[\s\t]*:.*/, `defaultLocale: ${JSON.stringify(json.defaultLocale)},`);
res = res.replace(/pages[\s\t]*: {[^}]*/s, `pages: ${JSON.stringify(json.pages, null, 4).replace('}', '')}`);
fs.writeFileSync(i18nFilePath, res);
// Create the folder for each language if it does not exist by copying that of the default language
await createTranslationFiles(tabLangs, defaultLanguage);
console.log('Language initialization completed');
};
const createTranslationFiles = async (langs, defaultLanguage) => {
const themeName = path.basename(__dirname);
const pathToTheme = path.join(global.aquila.appRoot, 'themes', themeName);
let pathFrom = path.join(pathToTheme, 'locales');
// "locales" folder
await copyTranslationFiles(langs, defaultLanguage, pathFrom);
// "translations" folder for each module
const moduleThemePath = path.join(pathToTheme, 'modules');
if (fs.existsSync(moduleThemePath)) {
const moduleList = fs.readdirSync(path.join(pathToTheme, 'modules'), { withFileTypes: true })
.filter((item) => item.isDirectory())
.map((item) => item.name);
for (const m of moduleList) {
pathFrom = path.join(pathToTheme, 'modules', m, 'translations');
await copyTranslationFiles(langs, defaultLanguage, pathFrom);
}
}
};
const copyTranslationFiles = async (langs, defaultLanguage, pathFrom) => {
let fromLang = defaultLanguage;
let srcPathTranslation = path.join(pathFrom, defaultLanguage);
const srcPathTranslationFiles = fs.existsSync(srcPathTranslation) ? fs.readdirSync(srcPathTranslation) : null;
if (!srcPathTranslationFiles || !srcPathTranslationFiles.length) {
const rootPathTranslation = path.join(pathFrom);
const otherTranslationLangPath = fs.readdirSync(rootPathTranslation).find((lang) => lang !== defaultLanguage);
if (!otherTranslationLangPath) return;
srcPathTranslation = path.join(pathFrom, otherTranslationLangPath);
fromLang = otherTranslationLangPath;
}
for (const lang of langs) {
const pathTranslationFilesDest = path.join(pathFrom, lang);
if (!fs.existsSync(pathTranslationFilesDest)) {
console.log(`Create folder for language [${lang}] from [${fromLang}] in [${pathFrom}]`);
await fs.copyRecursive(srcPathTranslation, pathTranslationFilesDest);
}
}
};
module.exports = {
setLanguage
};