-
-
Notifications
You must be signed in to change notification settings - Fork 32
/
webpack.translation.js
102 lines (92 loc) · 2.9 KB
/
webpack.translation.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
const path = require ('path');
const fs = require ('fs-extra');
const _ = require ('lodash');
const plugins = require ('./webpack.plugins.js');
class TranslationPlugin {
constructor (options)
{
this.target = options.target;
}
apply(compiler) {
compiler.hooks.environment.tap('Translation Plugin', () => {
console.log('Writing translation files');
// TODO apply merge here
let allPlugins = plugins.loadPlugins (this.target).plugins;
var TRANSLATION_WRITE = 'source/plugins/studio/workspace/translations.json';
var languages = {};
var translations = {};
for(let plugin of allPlugins)
{
try
{
let TRANSLATION_READ = 'source/plugins/'+plugin.folder+'/'+plugin.name+'/translations/';
let languageList = fs.readdirSync (TRANSLATION_READ);
for (let file of languageList)
{
try
{
let fileTranslated = path.basename( file, '.json' );
if (fileTranslated.startsWith('messages-'))
{
let newObject = {};
let language = require('./'+path.join (TRANSLATION_READ,fileTranslated));
let languageKey = fileTranslated.substring(9);
if (!translations[languageKey]) translations[languageKey] = {};
for (let key in language) {
let value = language[key];
newObject[key] = value.message;
if (key === 'LANGUAGE')
{
languages[languageKey] = value.message;
}
}
_.assign (translations[languageKey], newObject);
// try
// {
// result[languageKey] = JSON.parse(fs.readFileSync(path.join(TRANSLATION_READ,file)).toString()).LANGUAGE.message;
// }
// catch(e)
// {
// console.log('Error in file: ' + path.join(TRANSLATION_READ,file) + '.' + e.message);
// }
// console.log ('Language ' + fileTranslated.substring(9).toString() + ' added.');
// console.log (newObject);
// languages.push(newObject);
}
}
catch(e)
{
console.log('Error: ' + e.message);
}
}
}
catch(e)
{
if (e.message.indexOf ('ENOENT') === -1)
{
console.log(e.message);
}
}
}
// var allLanguages = Object.values(result);
// var allLanguageKeys = Object.keys(result);
// let final = {};
// for(let eachLanguage in allLanguages)
// {
// var allTranslations = {};
// for(let object of languages)
// if(object.LANGUAGE === allLanguages[eachLanguage])
// {
// let allKeys = Object.keys(object);
// let allValues = Object.values(object);
// for(let key in allKeys)
// allTranslations[allKeys[key]] = allValues[key];
// }
// final[allLanguageKeys[eachLanguage]] = allTranslations;
// }
//cheia
fs.writeFileSync (TRANSLATION_WRITE, JSON.stringify ({LANGUAGES: languages, TRANSLATION: translations}, null, 3), {'flags': 'a'});
});
}
}
module.exports = TranslationPlugin;