summaryrefslogtreecommitdiff
path: root/l10ntools/source/gLang.cxx
blob: af031a4f2e8df95c4012e57fea6ff69300fdb509 (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
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
/* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
/*
 * This file is part of the LibreOffice project.
 *
 * This Source Code Form is subject to the terms of the Mozilla Public
 * License, v. 2.0. If a copy of the MPL was not distributed with this
 * file, You can obtain one at http://mozilla.org/MPL/2.0/.
 *
 * This file incorporates work covered by the following license notice:
 *
 *   Licensed to the Apache Software Foundation (ASF) under one or more
 *   contributor license agreements. See the NOTICE file distributed
 *   with this work for additional information regarding copyright
 *   ownership. The ASF licenses this file to you under the Apache
 *   License, Version 2.0 (the "License"); you may not use this file
 *   except in compliance with the License. You may obtain a copy of
 *   the License at http://www.apache.org/licenses/LICENSE-2.0 .
 */
#include <iostream>
#include <string>
#include <vector>
using namespace std;

#include "gL10nMem.hxx"
#include "gConv.hxx"



class handler
{
    public:
        handler()  {};
        ~handler() {};

        static void showRunTimeError(string sErr);
        static void showUsage(string        sErr);
        void checkCommandLine(int argc, char *argv[]);
        void run();

    private:
        bool mbForceSave;
        enum {DO_CONVERT, DO_EXTRACT, DO_MERGE} meWorkMode;
        string         msTargetDir;
        string         msPoDir;
        vector<string> mvSourceFiles;
        l10nMem        mcMemory;

        static void showManual();
        void loadL10MEM(bool onlyTemplates);
        void runConvert();
        void runExtract();
        void runMerge();
};



void handler::showRunTimeError(string sErr)
{
    cerr << "runtime error: "
         << (sErr.size() ? sErr : "No description")
         << endl;
    exit(-1);
}



void handler::showUsage(string sErr)
{
    if (sErr.size())
        cerr << "commandline error: " << sErr << endl;

    cout << "syntax oveview, use \"genLang help\" for full description\n"
        "   genLang <cmd> <options>\n"
        "   <cmd> is one of\n"
        "      convert   convert old pot/po files to new format\n"
        "      extract   extract pot templates from sources\n"
        "      help      show manual\n"
        "      merge     merge po files back to sources\n"
        "   <options> is a combination of\n"
        "      -d        show debug information\n"
        "      -s        save unconditionally\n"
        "      -v        show progress information\n"
        "\n"
        "      --files     <files>       input file list\n"
        "      --target    <directory>   target root directory\n"
        "      --base      <directory>   base directory for files\n"
        "      --po        <directory>   po root directory\n";

    if (sErr.size())
        exit(-1);
}



void handler::showManual()
{
    // give the correct usage
    cout <<
        "genLang(c) 2016 by Document Foundation\n"
        "=============================================\n"
        "As part of the L10N framework for LibreOffice,\n"
        "genLang extracts en-US texts sources of the following types:\n"
        "  .xrm, .xhp, .xcu, .xcs, .ulf, .tree, .src, .prop\n"
        "to generate .pot files used for translation\n"
        "genLang extract localized texts from\n"
        "  .po\n"
        "to generate sources containing all translated languages\n"
        "\n"
        "genLang can also convert old .po and .pot files\n"
        "the conversion makes tool changes transparent to translators\n"
        "\n"
        "Syntax:\n\n";

    showUsage("");

    cout <<
        "\n"
        "  genLang extract [-v] [-d] [-s]\n"
        "                  --base <directory> --files <files> --target <directory>\n"
        "    extract text from source (.ui etc) <files>, result is .pot\n"
        "    templates files written to <directory> with a structure\n"
        "    files are expanded with --base, to shorten command line"
        "\n\n";

    cout <<
        "  genLang merge [-v] [-d] [-s]\n"
        "                --base  <directory>"
        "                --files <files>\n"
        "                --target <directory>\n"
        "                --po     <directory>\n"
        "  merges translations (--po) with source files (--files)\n"
        "  and write the result to --target\n"
        "  files are expanded with --base, to shorten command line"
        "\n\n";

    cout <<
        "  genLang convert [-v] [-d] [-s]\n"
        "                  --base  <directory>"
        "                  --files <files>\n"
        "                  --po     <directory>\n"
        "                  --target <directory>\n"
        "  read old po (--po) and new po (--files) files and\n"
        "  write po files (--target), ready to be loaded\n"
        "  files are expanded with --base, to shorten command line"
        "  in Pootle\n"
        "\n\n";

    cout <<
        "  genLang help\n"
        "    this text\n"
        "\n\n";
    exit(0);
}



void handler::checkCommandLine(int argc, char *argv[])
{
    string sWorkText, sBaseDir("");
    int    i;
    bool   bSourceFiles, bTargetDir, bPoDir;


    // Set default
    mbForceSave = false;

    // check for fixed parameter: genLang <cmd>
    if (argc < 2)
      throw "Not enough parameters";

    // check for working mode
    sWorkText = argv[1];
    if      (sWorkText == "convert")  meWorkMode = DO_CONVERT;
    else if (sWorkText == "extract")  meWorkMode = DO_EXTRACT;
    else if (sWorkText == "merge")    meWorkMode = DO_MERGE;
    else if (sWorkText == "help")     showManual();
    else                              throw "<command> is mandatory";

    // loop through all variable arguments
    for (i = 2; i < argc; ++i) {
        sWorkText = argv[i];
        if (sWorkText == "-d") {
            // show debug information
            mcMemory.setDebug(true);
        }
        else if (sWorkText == "-v") {
            // show progress information
            mcMemory.setVerbose(true);
        }
        else if (sWorkText == "-s") {
            // forced save
            mbForceSave = true;
        }
        else {
            // These arguments, all need and extra argument
            if (i == argc - 1)
                throw sWorkText + " missing filename arguments";

            if (sWorkText == "--files") {
                // Loop through filenames
                for (++i; i < argc && argv[i][0] != '-'; ++i)
                    mvSourceFiles.push_back(sBaseDir + argv[i]);
                --i;
            }
            else if (sWorkText == "--base") {
                sBaseDir = argv[++i];
                if (sBaseDir[sBaseDir.length() - 1] != '/')
                    sBaseDir += "/";
            }
            else if (sWorkText == "--target") {
                msTargetDir = argv[++i];
                if (msTargetDir[msTargetDir.length() - 1] != '/')
                    msTargetDir += "/";
            }
            else if (sWorkText == "--po") {
                msPoDir = argv[++i];
                if (msPoDir[msPoDir.length() - 1] != '/')
                    msPoDir += "/";
            }
            else
                throw "unknown argument";
        }
    }

    // Check all the correct parameters are supplied
    bSourceFiles = bTargetDir = bPoDir = false;
    switch (meWorkMode) {
        case DO_CONVERT:
             bSourceFiles = bPoDir = bTargetDir = true;
             break;
        case DO_EXTRACT:
             bSourceFiles = bTargetDir = true;
             break;
        case DO_MERGE:
             bPoDir = bTargetDir = true;
             break;
    }

    if ( (mvSourceFiles.size() > 0) != bSourceFiles)
        throw bSourceFiles ? "--files missing" : "--files used, but not permitted";
    if ( (msPoDir.size() > 0) != bPoDir)
        throw bPoDir ? "--po missing" : "--po used, but not permitted";
}



void handler::run()
{
    // use workMode to start correct control part
    switch (meWorkMode)
    {
        case DO_EXTRACT:     runExtract(); break;
        case DO_MERGE:       runMerge();   break;
        case DO_CONVERT:     runConvert(); break;
    }
}



void handler::loadL10MEM(bool onlyTemplates)
{
    string sLoad = msPoDir + "templates/";
//FIX    vector<string>::iterator siLang;

    // no convert
    mcMemory.setConvert(false, false);

    // load texts from en-US po file (master)
    // tell system
    l10nMem::showDebug("genLang loading master text from file " + sLoad);

    // and load file
    mcMemory.setLanguage("", true);
    convert_gen::createInstance(mcMemory, sLoad, msTargetDir, "").execute(false);

    if (onlyTemplates)
      return;

    // loop through all languages and load text
//FIX    for (siLang = mvLanguages.begin(); siLang != mvLanguages.end(); ++siLang)
//FIX    {
//FIX        sLoad = msPoDir + *siLang + "/";

        // get converter and extract files
//FIX        mcMemory.setLanguage(*siLang, true);

        // tell system
//FIX        l10nMem::showDebug("genLang loading text from language file " + sLoad);

//FIX        convert_gen::createInstance(mcMemory, sLoad, msTargetDir, "").execute(false, false);
//FIX    }
}



void handler::runConvert()
{
    vector<string>::iterator siSource;
//FIX    vector<string>::iterator siLang;


    // convert
    loadL10MEM(true);
    mcMemory.setConvert(true, false);

    // loop through all source files, and extract messages from each file
    for (siSource = mvSourceFiles.begin(); siSource != mvSourceFiles.end(); ++siSource) {
        // tell system
        l10nMem::showDebug("genLang compare template " + *siSource);

        // get converter and extract files
        convert_gen& convertObj = convert_gen::createInstance(mcMemory, "./", msTargetDir, *siSource);
        convertObj.execute(false);

        mcMemory.showNOconvert();

//FIX        for (siLang = mvLanguages.begin(); siLang != mvLanguages.end(); ++siLang) {
//FIX            string sFilePath = *siLang + "/";

            // get converter and extract files
//FIX            mcMemory.setLanguage(*siLang, false);

            // tell system
//FIX            l10nMem::showDebug("genLang convert text from file " +
//FIX                               sFilePath + *siSource + " language " + *siLang);

            // get converter and extract files
//FIX            convert_gen& convertObj = convert_gen::createInstance(mcMemory, sFilePath, msTargetDir, *siSource);
//FIX            convertObj.execute(true, false);
//FIX        }
    }

    // and generate language file
    //mcMemory.saveLanguages(msPoOutDir, mbForceSave);
}



void handler::runExtract()
{
    vector<string>::iterator siSource;
    int newPos;

    // no convert
    mcMemory.setConvert(false, false);

    // loop through all source files, and extract messages from each file
    for (siSource = mvSourceFiles.begin(); siSource != mvSourceFiles.end(); ++siSource) {
        // tell system
        l10nMem::showDebug("genLang extracting text from file " + *siSource);

        // set module name
        newPos = (*siSource).find_last_of("/\\", (*siSource).length());
        mcMemory.setModuleName((*siSource).substr(0, newPos));

        // get converter and extract file
        convert_gen& convertObj = convert_gen::createInstance(mcMemory, "", msTargetDir, *siSource);
        convertObj.execute(false);
    }

    // and generate language file
    mcMemory.saveTemplates(msTargetDir, mbForceSave);
}



void handler::runMerge()
{
    vector<string>::iterator siSource;

    // no convert
    mcMemory.setConvert(false, false);

    // loop through all source files, and extract messages from each file
    for (siSource = mvSourceFiles.begin(); siSource != mvSourceFiles.end(); ++siSource) {
        // tell system
        l10nMem::showDebug("genLang merging translated text to file " + *siSource);

        // get converter and extract file
        convert_gen& convertObj = convert_gen::createInstance(mcMemory, "", msTargetDir, *siSource);
        convertObj.execute(true);
    }
}



int main(int argc, char *argv[])
{
    handler cHandler;

    // check command line (exit if problems)
    try {
        cHandler.checkCommandLine(argc, argv);
    }
    catch(const char *sErr) {
        string myErr(sErr);
        handler::showUsage(myErr);
        exit(-1);
    }
    catch(const string& sErr) {
        handler::showUsage(sErr);
        exit(-1);
    }

    // command line is ok, so execute it
    try {
        cHandler.run();
    }
    catch(const char *sErr) {
        string myErr(sErr);
        handler::showRunTimeError(myErr);
        exit(-1);
    }
    catch(const string& sErr) {
        handler::showRunTimeError(sErr);
        exit(-1);
    }
}