summaryrefslogtreecommitdiff
path: root/tools/lpgen/translate.js
blob: a8b000ed2645be331a7ee4dcdf6fb8e9810cc1c0 (plain)
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
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
//**********************************************************************************//
//* Name:         translate.js                                                     *//
//* Language:     JScript                                                          *//
//* Function:     Parse Miranda-NG translation templates and get translated strings*//
//* Usage:        cscript /nologo translate.js  to run generation in batches       *//
//* Usage:        cscript /nologo translate.js /log:"yes" to enable console logging*//
//* Usage:        cscript /nologo translate.js /path:"path\file" for one template  *//
//* Author:       BasiL                                                            *//
//**********************************************************************************//

//Init Variables
//Create FileSystemObject FSO
var FSO=WScript.CreateObject("Scripting.FileSystemObject");
//FileSystemObject vars
var ForReading=1;
var TristateUseDefault=-2;
var overwritefile=true;
var unicode=false;
//disabling log by default
var log="no"
//Path variables
var scriptpath=FSO.GetParentFolderName(WScript.ScriptFullName);
//crazy way to get path two layers upper "\tools\lpgen\"
var trunk=FSO.GetFolder(FSO.GetParentFolderName(FSO.GetParentFolderName(scriptpath)));
//plugins path
var plugins=FSO.BuildPath(trunk,"langpacks\\ru\\Plugins\\");
//init translate db arrays
CoreTranslateDB=new Array;
DupesTranslateDB=new Array;

//*********************************************************************************//
//                         Checking command line parameters                       *//
//*********************************************************************************//

// if console param /log: specified, put it to var log. To enable log, specify /log:"yes"
if (WScript.Arguments.Named.Item("log")) log=WScript.Arguments.Named.Item("log").toLowerCase();

//If script run by double click, open choose folder dialog to choose plugin folder to parse. If Cancel pressed, quit script.
if (WScript.FullName.toLowerCase().charAt(WScript.FullName.length - 11)=="w") {
   WScript.Echo("Please run from command line!");
   WScript.Quit();
}

//when /plugin: specified, parse only this path and quit
if (WScript.Arguments.Named.Item("path")) {
    //First, generate DB of translations from Core and Dupes files
    DupesAndCoreTranslationDB();
    //Path from command line:
    var cmdline_file=new String(WScript.Arguments.Named.Item("path"));
    //init array for our file translation
    var cmdline_file_array=new Array;
    //Output filename variable
    var traslated_cmdline_file=new String(FSO.BuildPath(scriptpath,FSO.GetFileName(cmdline_file)));
    if (log=="yes") WScript.Echo("translating "+cmdline_file);
    //Call TranslateTemplateFile for path specified in command line argument /path:"path/to/template", output result to "scriptpath"
    TranslateTemplateFile(WScript.Arguments.Named.Item("path"),cmdline_file_array);
    //Output result to scriptpath folder.
    WriteToFile(cmdline_file_array,traslated_cmdline_file);
    if (log=="yes") WScript.Echo("translated file here: "+traslated_cmdline_file);
    //We are done, quit.
    WScript.Quit();
}

//*********************************************************************************//
//                                    Main part                                   *//
//*********************************************************************************//

if (log=="yes") WScript.Echo("Translation begin");
//Generate Translation DB as two-dimensional array of Dupes and Core translations.
DupesAndCoreTranslationDB ();

//Array for translated core
Traslate_Core=new Array;
if (log=="yes") WScript.Echo("Translating Core");
//Call function for translate core template
TranslateTemplateFile(FSO.BuildPath(trunk,"langpacks\\en\\=CORE=.txt"),Traslate_Core);
//var with translated core template file path
Translated_Core=trunk+"\\langpacks\\en\\translated_=CORE=.txt"
if (log=="yes") WScript.Echo("Output to: "+Translated_Core);
//output traslated core into file
WriteToFile(Traslate_Core,Translated_Core);

//Init array of template files
TemplateFilesArray=new Array;
//Find all template files and put them to array
FindFiles(FSO.BuildPath(trunk,"langpacks\\en\\plugins\\"),"\\.txt$",TemplateFilesArray)
//create enumerator filesenum from filelist
filesenum=new Enumerator(TemplateFilesArray);
//cycle through file list
 while (!filesenum.atEnd()) {
     //intit Array with translated strings
     TranslatedTemplate=new Array;
     //curfile is our current file in files enumerator
     curfile=filesenum.item();
     //Log output to console
     if (log=="yes") WScript.Echo("translating "+curfile);
     //path to result file
     traslatedtemplatefile=trunk+"\\langpacks\\en\\plugins\\translated_"+FSO.GetFileName(curfile);
     //now put strings from template and translations into array
     TranslateTemplateFile(curfile,TranslatedTemplate);
     //Write array into file;
     WriteToFile(TranslatedTemplate,traslatedtemplatefile);
     //Log output to console
     if (log=="yes") WScript.Echo("Output to: "+traslatedtemplatefile);
     //move to next file
     filesenum.moveNext();
    };
if (log=="yes") WScript.Echo("Translation end");


//*********************************************************************************//
//                                    Functions                                   *//
//*********************************************************************************//

//Generate DB with translations from Core and Dupes files
function DupesAndCoreTranslationDB () {
//path variables
var CoreTranslateFile=FSO.BuildPath(trunk,"langpacks\\ru\\=CORE=.txt");
var DupesTranslateFile=FSO.BuildPath(trunk,"langpacks\\ru\\=DUPES=.txt");
//Generate Core and Dupes translate DBs
GenerateTransalteArray(CoreTranslateFile,CoreTranslateDB);
GenerateTransalteArray(DupesTranslateFile,DupesTranslateDB);
}

//Generate two-dimensional array with english sting + translated string from langpack file
function GenerateTransalteArray (file,array) {
//if file does not exist, it's a core, we do not need do the job again, so return.
if (!FSO.FileExists(file)) return;
//open file
var translatefile=FSO.GetFile(file).OpenAsTextStream(ForReading, TristateUseDefault);
//read file into var
var translatefiletext=translatefile.ReadAll();
//"find" - RegularExpression, first string have to start with [ and end with]. Next string - translation
var find=/(^\[.+?\])\r\n(.+?)(?=\r)/mg;
//While our "find" RegExp return a results, push strings into array.
while ((string = find.exec(translatefiletext)) != null) {
    //first, init empty var
    var string;
    //now, push first match as original string [....], second match is a translation
    array.push([string[1],string[2]]);
    }
//close file 
translatefile.Close();
}

//Generate array with stirngs from translation template, adding founded translation, if exist.
function TranslateTemplateFile(Template_file,array) {
 //init Arrays:
 //1) PluginTranslate from plugins translate file
 PluginTranslateDB=new Array;
 //2) Global translate DB
 GlobalPluginTranslateDB=new Array;
 //Generate PluginTranslate DB
 GenerateTransalteArray(FSO.BuildPath(plugins,FSO.GetFileName(Template_file)),PluginTranslateDB);
 //Concatenate three translate DB in order: Plugin transalte, than Dupes and ending with Core translate DB
 GlobalPluginTranslateDB=PluginTranslateDB.concat(DupesTranslateDB).concat(CoreTranslateDB);
 //If file zero size, return;
 if (FSO.GetFile(Template_file).Size==0) return;  
 //access file
 template_file_stream=FSO.GetFile(Template_file).OpenAsTextStream(ForReading, TristateUseDefault);
 //Reading line-by-line
 while (!template_file_stream.AtEndOfStream) {
     //clear up variable
     englishstring="";
     //read on line into rcline
     line=template_file_stream.ReadLine();
     //Push line int array, we need all lines from template
     array.push(line);
     //find string covered by[] using regexp
     englishstring=line.match(/\[.+\]/);
     //If current line is english string covered by [], try to find translation in global db
     if (englishstring) {
            //uncomment next string for more verbose log output
            //if (log=="yes") WScript.Echo("lookin for "+englishstring);
            
            //add string  to array, firstly find our string in global translate DB
            //if translation not found (thus, FindTranslation return nothing), empty line will be added.
            array.push(FindTranslation(englishstring,GlobalPluginTranslateDB));
            }
    }  
 //closing file
 template_file_stream.Close();
};

//Cycle trough array and find translation for given string in translate_array
function FindTranslation (string, translate_array) {
//cycle trough array length
for (i=0;i<=translate_array.length-1;i++) {
    //check if the string match our first [0] colomn of array with english strings
    if (translate_array[i][0]==string) {
        //yes, we we have this string, so return second [1] colomn of array
        return translate_array[i][1]
        }
    };
}

//Recourse find all files in "path" with file RegExp mask "name" and return file list into filelistarray
function FindFiles (path,name,filelistarray) {
 //Init vars
 var Folder, Folders, Files, file, filename;
 // second param "name" is our case insensive RegExp
 var filemask=new RegExp(name,"i");
 //Put path into var Folder
 Folder=FSO.GetFolder(path);
 //put subFolders into var
 Folders=new Enumerator(Folder.SubFolders);
 //Create Enumerator with Folder files inside
 Files=new Enumerator(Folder.Files);
 //Cycle by files in Folder
 while (!Files.atEnd()) {
     //file is a next file
     file=Files.item();
     //put file name into filename
     filename=FSO.GetFileName(file);
     //if filename is our name mask, do the job.
     if (filemask.test(filename)) filelistarray.push(file);
     //move to next file
     Files.moveNext();
    };
 //Cycle by subfolders
 while (!Folders.atEnd()) {
    FindFiles(Folders.item().Path,name,filelistarray);
    //WScript.Echo(Folders.item().Path);
    Folders.moveNext();
    };
};

//Put array of strings into file
function WriteToFile (array,file) {
 //Create file
 outfile=FSO.CreateTextFile(file, overwritefile , unicode)
 //Finally, write strings from array to file
 for (i=0;i<=array.length-1;i++) outfile.WriteLine(array[i]);
 //Close file
 outfile.Close();
};