1 |
efrain |
1 |
// This file is part of Moodle - http://moodle.org/
|
|
|
2 |
//
|
|
|
3 |
// Moodle is free software: you can redistribute it and/or modify
|
|
|
4 |
// it under the terms of the GNU General Public License as published by
|
|
|
5 |
// the Free Software Foundation, either version 3 of the License, or
|
|
|
6 |
// (at your option) any later version.
|
|
|
7 |
//
|
|
|
8 |
// Moodle is distributed in the hope that it will be useful,
|
|
|
9 |
// but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
10 |
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
11 |
// GNU General Public License for more details.
|
|
|
12 |
//
|
|
|
13 |
// You should have received a copy of the GNU General Public License
|
|
|
14 |
// along with Moodle. If not, see <http://www.gnu.org/licenses/>.
|
|
|
15 |
|
|
|
16 |
import {readdir, readFile, writeFile, mkdir} from 'fs/promises';
|
|
|
17 |
import {join as joinPath} from 'path';
|
|
|
18 |
|
|
|
19 |
const fullyTranslatedLanguage = 'de';
|
|
|
20 |
const maxStringIdentifierLength = 90;
|
|
|
21 |
|
|
|
22 |
const readStringsFromLanguages = async (language) => {
|
|
|
23 |
const fileContent = await readFile(`./langs/${language}.js`, 'utf-8');
|
|
|
24 |
|
|
|
25 |
let translations = {};
|
|
|
26 |
const tinymce = {
|
|
|
27 |
addI18n: (language, strings) => {
|
|
|
28 |
translations = strings;
|
|
|
29 |
},
|
|
|
30 |
};
|
|
|
31 |
|
|
|
32 |
eval(fileContent);
|
|
|
33 |
|
|
|
34 |
return Object.keys(translations).sort().reduce((sortedTranslations, key) => {
|
|
|
35 |
sortedTranslations[key] = translations[key];
|
|
|
36 |
return sortedTranslations;
|
|
|
37 |
}, {});
|
|
|
38 |
};
|
|
|
39 |
|
|
|
40 |
const getStringMap = (strings) => {
|
|
|
41 |
const stringMap = {};
|
|
|
42 |
|
|
|
43 |
const getUniqueKeyForString = (string, modifier = 0) => {
|
|
|
44 |
let stringKey = string.toLowerCase()
|
|
|
45 |
.replaceAll(' ', '_')
|
|
|
46 |
.replaceAll(/\{(\d)\}/g, '$1')
|
|
|
47 |
.replaceAll('#', 'hash')
|
|
|
48 |
.replaceAll(/[^a-z0-9_\-\.]/g, '')
|
|
|
49 |
;
|
|
|
50 |
|
|
|
51 |
if (stringKey === '') {
|
|
|
52 |
throw new Error(`The calculated key for '${string}' was empty`);
|
|
|
53 |
}
|
|
|
54 |
|
|
|
55 |
stringKey = `tiny:${stringKey}`;
|
|
|
56 |
|
|
|
57 |
if (stringKey.length > maxStringIdentifierLength) {
|
|
|
58 |
const modifierLength = modifier === 0 ? 0 : `${modifier}`.length;
|
|
|
59 |
stringKey = stringKey.slice(0, maxStringIdentifierLength - modifierLength);
|
|
|
60 |
}
|
|
|
61 |
|
|
|
62 |
if (modifier > 0) {
|
|
|
63 |
stringKey = `${stringKey}${modifier}`;
|
|
|
64 |
}
|
|
|
65 |
|
|
|
66 |
if (typeof stringMap[stringKey] !== 'undefined') {
|
|
|
67 |
return getUniqueKeyForString(string, ++modifier);
|
|
|
68 |
}
|
|
|
69 |
|
|
|
70 |
return stringKey;
|
|
|
71 |
};
|
|
|
72 |
|
|
|
73 |
strings.forEach((string) => {
|
|
|
74 |
const stringKey = getUniqueKeyForString(string);
|
|
|
75 |
if (typeof stringMap[stringKey] !== 'undefined') {
|
|
|
76 |
throw new Error(`Found existing key ${stringKey}`);
|
|
|
77 |
}
|
|
|
78 |
|
|
|
79 |
stringMap[stringKey] = string;
|
|
|
80 |
});
|
|
|
81 |
|
|
|
82 |
return stringMap;
|
|
|
83 |
};
|
|
|
84 |
|
|
|
85 |
const getPhpStrings = (stringMap, translatedStrings) => Object.entries(stringMap).map(([stringKey, englishString]) => {
|
|
|
86 |
if (translatedStrings[englishString].length === 0) {
|
|
|
87 |
return null;
|
|
|
88 |
}
|
|
|
89 |
return `$string['${stringKey}'] = '${translatedStrings[englishString].replaceAll("'", "\\\'")}';`
|
|
|
90 |
})
|
|
|
91 |
.filter((value) => value !== null)
|
|
|
92 |
.join("\n");
|
|
|
93 |
|
|
|
94 |
const storeEnglishStrings = async(stringMap) => {
|
|
|
95 |
const englishStrings = Object.entries(stringMap).map(([stringKey, stringValue]) => {
|
|
|
96 |
return `$string['${stringKey}'] = '${stringValue.replace("'", "\\\'")}';`
|
|
|
97 |
}).join("\n");
|
|
|
98 |
await writeFile('./strings.php', englishStrings + "\n");
|
|
|
99 |
await writeFile('./tinystrings.json', JSON.stringify(stringMap, null, ' '));
|
|
|
100 |
}
|
|
|
101 |
|
|
|
102 |
const constructTranslationFile = async(language, englishStringMap = null) => {
|
|
|
103 |
const strings = await readStringsFromLanguages(language);
|
|
|
104 |
console.log(`Generating translation data for ${language} with ${Object.keys(strings).length} strings`);
|
|
|
105 |
const stringMap = englishStringMap === null ? getStringMap(Object.keys(strings)) : englishStringMap;
|
|
|
106 |
|
|
|
107 |
const langDir = joinPath('lang', language);
|
|
|
108 |
await mkdir(langDir, {recursive: true});
|
|
|
109 |
|
|
|
110 |
const fileContent = `<?php
|
|
|
111 |
|
|
|
112 |
${getPhpStrings(stringMap, strings)}
|
|
|
113 |
`;
|
|
|
114 |
|
|
|
115 |
await writeFile(joinPath(langDir, `editor_tiny.php`), fileContent);
|
|
|
116 |
|
|
|
117 |
return {
|
|
|
118 |
strings,
|
|
|
119 |
stringMap,
|
|
|
120 |
};
|
|
|
121 |
};
|
|
|
122 |
|
|
|
123 |
const constructTranslationFiles = async() => {
|
|
|
124 |
const {stringMap} = await constructTranslationFile(fullyTranslatedLanguage);
|
|
|
125 |
storeEnglishStrings(stringMap);
|
|
|
126 |
|
|
|
127 |
readdir('./langs/').then((files) => {
|
|
|
128 |
files.forEach(async(file) => {
|
|
|
129 |
const langIdent = file.replace('.js', '');
|
|
|
130 |
if (langIdent === fullyTranslatedLanguage) {
|
|
|
131 |
// This language is already done.
|
|
|
132 |
return;
|
|
|
133 |
}
|
|
|
134 |
await constructTranslationFile(langIdent, stringMap);
|
|
|
135 |
});
|
|
|
136 |
});
|
|
|
137 |
}
|
|
|
138 |
|
|
|
139 |
constructTranslationFiles();
|