update genLang (l10ntools)

Upgrade gLang to LO standard
Compilation of genLang module is disabled for now
activate manually by adding to Module_l10ntools.mk

Change-Id: Ib82cae6a013d10d158ec5faa81ace512c0096a39
This commit is contained in:
Jan Iversen 2016-03-11 10:39:47 +01:00
parent 66fee51115
commit 00badb6fb5
11 changed files with 473 additions and 145 deletions

View File

@ -113,7 +113,7 @@ class convert_gen
~convert_gen();
// do extract/merge
bool execute(const bool bMerge);
bool execute(const bool bMerge, const bool bKid);
// ONLY po should implement these functions
void startSave(const std::string& sLanguage,
@ -127,38 +127,4 @@ class convert_gen
static bool checkAccess(std::string& sFile);
static bool createDir(std::string& sDir, std::string& sFile);
};
/******************** C L A S S D E F I N I T I O N ********************/
class handler
{
public:
handler();
~handler();
void checkCommandLine(int argc, char *argv[]);
void run();
private:
enum {DO_CONVERT, DO_CONVERT_POT, DO_EXTRACT, DO_EXTRACT_KID, DO_MERGE} meWorkMode;
l10nMem mcMemory;
std::string msModuleName;
std::string msPoOutDir;
std::string msPoDir;
std::string msSourceDir;
std::string msTargetDir;
bool mbForceSave;
std::vector<std::string> mvSourceFiles;
std::vector<std::string> mvLanguages;
void runConvert(bool bPot);
void runExtract(bool bKid);
void runMerge();
void showUsage(std::string& sErr);
void showManual();
void loadL10MEM(bool onlyTemplates);
void readFileWithSources();
};
#endif

View File

@ -103,10 +103,13 @@ convert_gen::~convert_gen()
/********************** I M P L E M E N T A T I O N **********************/
bool convert_gen::execute(const bool bMerge)
bool convert_gen::execute(const bool bMerge, const bool bKid)
{
convert_gen_impl::mcImpl->mbMergeMode = bMerge;
if (bKid)
throw l10nMem::showError("not implemented");
// and load file
if (!convert_gen_impl::mcImpl->prepareFile())
return false;

View File

@ -16,33 +16,463 @@
* except in compliance with the License. You may obtain a copy of
* the License at http://www.apache.org/licenses/LICENSE-2.0 .
*/
#include "gLang.hxx"
#include <iostream>
/*****************************************************************************
*************************** G L A N G . C X X ***************************
*****************************************************************************
* This is the main of the l10n localizer program, it is C based and call
* down to classes for handling.
*****************************************************************************/
class handler
{
public:
handler() {};
~handler() {};
void showRunTimeError(std::string sErr);
void showUsage(std::string sErr);
void checkCommandLine(int argc, char *argv[]);
void run();
private:
bool mbForceSave;
bool mbDebug;
bool mbVerbose;
bool mbSave;
enum {DO_CONVERT, DO_EXTRACT, DO_MERGE_KID, DO_MERGE} meWorkMode;
std::string msTargetDir;
std::string msPoDir;
std::string msPotDir;
std::vector<std::string> mvSourceFiles;
std::vector<std::string> mvLanguages;
l10nMem mcMemory;
void showManual();
void loadL10MEM(bool onlyTemplates);
void runConvert();
void runExtract();
void runMerge(bool bKid);
};
void handler::showRunTimeError(std::string sErr)
{
std::cerr << "runtime error: "
<< (sErr.size() ? sErr : "No description")
<< std::endl;
exit(-1);
}
void handler::showUsage(std::string sErr)
{
if (sErr.size())
std::cerr << "commandline error: " << sErr << std::endl;
std::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"
" -k generate key identifier version\n"
" -s save unconditionally\n"
" -v show progress information\n"
"\n"
" --files <files> input file list\n"
" --languages <languages> language list (omitting is all)\n"
" --target <directory> target root directory\n"
" --po <directory> po root directory\n"
" --pot <directory> pot root directory\n";
if (sErr.size())
exit(-1);
}
void handler::showManual()
{
// give the correct usage
std::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("");
std::cout <<
"\n"
" genLang extract [-v] [-d] [-s]\n"
" --files <files> --pot <directory>\n"
" extract text from <files>, result is .pot template\n"
" files written to <directory> with a structure\n"
"\n\n";
std::cout <<
" genLang merge [-v] [-d] [-s] [-k]\n"
" --languages <languages>\n"
" --target <directory>\n"
" --po <directory>\n"
" merges translations (--po) with source files\n"
" and write the result to --target\n"
"\n\n";
std::cout <<
" genLang convert [-v] [-d] [-s]\n"
" --po <directory>\n"
" --pot <directory>\n"
" --target <directory>\n"
" read old po (--po) and pot (--pot) files and updates\n"
" target po and pot files (--target), ready to be loaded\n"
" in Pootle\n"
"\n\n";
std::cout <<
" genLang help\n"
" this text\n"
"\n\n";
std::cout <<
"Parameters:\n"
" -d show debug information\n"
" -k generate key identifier version\n"
" -v show progress information\n"
" -s save unconditionally\n"
"\n"
" --files <files> input file list\n"
" --languages <languages> language list (omitting is all)\n"
" --target <directory> target root directory\n"
" --po <directory> po root directory\n"
" --pot <directory> pot root directory\n";
exit(0);
}
void handler::checkCommandLine(int argc, char *argv[])
{
std::string sWorkText;
int i;
// Set default
mbForceSave = mbDebug = mbVerbose = mbSave = 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
mbDebug = true;
}
else if (sWorkText == "-k") {
// generate key identifier version
if (meWorkMode != DO_MERGE)
showUsage("-k requires \"merge\"");
meWorkMode = DO_MERGE_KID;
}
else if (sWorkText == "-v") {
// show progress information
mbVerbose = true;
}
else if (sWorkText == "-s") {
// forced save
mbSave = true;
}
else if (sWorkText == "--files") {
// list of input files
if (meWorkMode != DO_EXTRACT)
showUsage("--files not valid for command");
if (i == argc)
showUsage("--files missing filename arguments");
// Loop through filenames
for (; i < argc && argv[i][0] != '-'; ++i)
mvSourceFiles.push_back(argv[i]);
}
else if (sWorkText == "--languages") {
// list of languages
if (meWorkMode != DO_MERGE)
showUsage("--languages not valid for command");
if (i == argc)
showUsage("--languages missing arguments");
// Loop through filenames
for (; i < argc && argv[i][0] != '-'; ++i)
mvLanguages.push_back(argv[i]);
}
else if (sWorkText == "--target") {
// target root directory
if (meWorkMode != DO_MERGE && meWorkMode != DO_CONVERT)
showUsage("--target not valid for command");
if (i == argc)
showUsage("--target missing directory argument");
msTargetDir = argv[++i];
}
else if (sWorkText == "--po") {
// po file root directory
if (meWorkMode != DO_MERGE && meWorkMode != DO_CONVERT)
showUsage("--po not valid for command");
if (i == argc)
showUsage("--po missing directory argument");
msPoDir = argv[++i];
}
else if (sWorkText == "--pot") {
// pot file root directory
if (meWorkMode != DO_EXTRACT && meWorkMode != DO_CONVERT)
showUsage("--pot not valid for command");
if (i == argc)
showUsage("--pot missing directory argument");
msPotDir = argv[++i];
}
else {
// collect files
showUsage("unknown argument");
}
}
// Check all the correct parameters are suplied
{
bool bSourceFiles, bLanguages, bTargetDir, bPoDir, bPotDir;
bSourceFiles = bLanguages = bTargetDir = bPoDir = bPotDir = false;
switch (meWorkMode)
{
case DO_CONVERT:
bPoDir = bPotDir = bTargetDir = true;
break;
case DO_EXTRACT:
bPotDir = bSourceFiles = true;
break;
case DO_MERGE_KID:
case DO_MERGE:
bPoDir = bLanguages = bTargetDir = true;
break;
}
if ( (mvSourceFiles.size() > 0) != bSourceFiles)
throw bSourceFiles ? "--files missing" :
"--files used, but not permitted";
if ( (mvLanguages.size() > 0) != bLanguages)
throw bLanguages ? "--languages missing" :
"--languages used, but not permitted";
if ( (msPoDir.size() > 0) != bPoDir)
throw bPoDir ? "--po missing" :
"--po used, but not permitted";
if ( (msPotDir.size() > 0) != bPotDir)
throw bPotDir ? "--pot missing" :
"--pot used, but not permitted";
}
}
void handler::run()
{
// Start memory module
loadL10MEM( (meWorkMode == DO_EXTRACT) );
// use workMode to start correct control part
switch (meWorkMode)
{
case DO_EXTRACT: runExtract(); break;
case DO_MERGE: runMerge(false); break;
case DO_MERGE_KID: runMerge(true); break;
case DO_CONVERT: runConvert(); break;
}
}
void handler::loadL10MEM(bool onlyTemplates)
{
std::string sLoad = msPoDir + "templates/";
std::vector<std::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 (mcMemory, sLoad, msTargetDir, "").execute(false, false);
if (onlyTemplates)
return;
// loop through all languages and load text
for (siLang = mvLanguages.begin(); siLang != mvLanguages.end(); ++siLang)
{
sLoad = msPoDir + *siLang + "/";
// get converter and extract files
mcMemory.setLanguage(*siLang, true);
// tell system
l10nMem::showDebug("genLang loading text from language file " + sLoad);
convert_gen(mcMemory, sLoad, msTargetDir, "").execute(false, false);
}
}
void handler::runConvert()
{
std::vector<std::string>::iterator siSource;
std::vector<std::string>::iterator siLang;
throw("NOT CONTROLLED");
// convert
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(mcMemory, "./", msTargetDir, *siSource);
convertObj.execute(false, false);
mcMemory.showNOconvert();
for (siLang = mvLanguages.begin(); siLang != mvLanguages.end(); ++siLang)
{
std::string sFilePath = *siLang + "/";
// get converter and extract files
mcMemory.setLanguage(*siLang, false);
// tell system
l10nMem::showDebug("genLang convert text from file " +
sFilePath + *siSource + " language " + *siLang);
// get converter and extract files
//convert_gen convertObj(mcMemory, sFilePath, msTargetDir, *siSource);
convertObj.execute(true, false);
}
}
// and generate language file
//mcMemory.saveLanguages(msPoOutDir, mbForceSave);
}
void handler::runExtract()
{
std::vector<std::string>::iterator siSource;
// no convert
mcMemory.setConvert(false, false);
throw("NOT CONTROLLED");
// 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);
// get converter and extract file
convert_gen convertObj(mcMemory, "", msTargetDir, *siSource);
convertObj.execute(false, false);
}
// and generate language file
mcMemory.saveTemplates(msPoDir, false, mbForceSave);
}
void handler::runMerge(bool bKid)
{
std::vector<std::string>::iterator siSource;
// no convert
mcMemory.setConvert(false, false);
throw("NOT CONTROLLED");
// 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(mcMemory, "", msTargetDir, *siSource);
convertObj.execute(true, bKid);
}
}
/********************** I M P L E M E N T A T I O N **********************/
#if defined(UNX) || defined(OS2)
int main(int argc, char *argv[])
#else
int _cdecl main(int argc, char *argv[])
#endif
{
handler cHandler;
// check command line (exit if problems)
try {
cHandler.checkCommandLine(argc, argv);
}
catch(const char *sErr) {
std::string myErr(sErr);
cHandler.showUsage(myErr);
exit(-1);
}
catch(std::string sErr) {
cHandler.showUsage(sErr);
exit(-1);
}
// command line is ok, so execute it
try {
cHandler.run();
}
catch(const char *sErr) {
std::string myErr(sErr);
cHandler.showRunTimeError(myErr);
exit(-1);
}
catch(std::string sErr) {
cHandler.showRunTimeError(sErr);
exit(-1);
}
}

View File

@ -17,14 +17,6 @@
* the License at http://www.apache.org/licenses/LICENSE-2.0 .
*/
/*****************************************************************************
********************** L E X D E F I N I T I O N **********************
*****************************************************************************
* lex grammar for parsing ressource source files (*.po files)
* file is converted to gConPo_yy.cxx with "flex"
*****************************************************************************/
/*
white-space
# translator-comments
@ -167,8 +159,8 @@ SPACE [ \t]*
void dummyJustForCompiler()
{
// char *txt = NULL;
char *txt = NULL;
// yy_flex_strlen(txt);
// yyunput(0, txt);
yyunput(0, txt);
}

View File

@ -18,15 +18,6 @@
*/
/*****************************************************************************
********************** L E X D E F I N I T I O N **********************
*****************************************************************************
* lex grammar for parsing ressource source files (*.src)
* file is converted to gConSrc_yy.cxx with "flex"
*****************************************************************************/
/*************** O V E R W R I T I N G F U N C T I O N S ***************/
%top{
#include "gConvSrc.hxx"
@ -331,7 +322,7 @@ KEYID [a-zA-Z0-9_-]+
void src_dummyJustForCompiler()
{
// char *txt = NULL;
char *txt = NULL;
// yy_flex_strlen(txt);
// yyunput(0, txt);
yyunput(0, txt);
}

View File

@ -18,15 +18,6 @@
*/
/*****************************************************************************
********************** L E X D E F I N I T I O N **********************
*****************************************************************************
* lex grammar for parsing ressource source files (*.tree files)
* file is converted to gConTree_yy.cxx with "flex"
*****************************************************************************/
/*************** O V E R W R I T I N G F U N C T I O N S ***************/
%top{
#include "gConvTree.hxx"
@ -159,7 +150,7 @@ IDENT [\.a-zA-Z0-9_-]+
void tree_dummyJustForCompiler()
{
// char *txt = NULL;
char *txt = NULL;
// yy_flex_strlen(txt);
// yyunput(0, txt);
yyunput(0, txt);
}

View File

@ -18,15 +18,6 @@
*/
/*****************************************************************************
********************** L E X D E F I N I T I O N **********************
*****************************************************************************
* lex grammar for parsing ressource source files (*.ulf files)
* file is converted to gConUlf_yy.cxx with "flex"
*****************************************************************************/
/*************** O V E R W R I T I N G F U N C T I O N S ***************/
%top{
#include "gConvUlf.hxx"
@ -138,7 +129,7 @@ KEYID [a-zA-Z0-9_-]+
void ulf_dummyJustForCompiler()
{
// char *txt = NULL;
char *txt = NULL;
// yy_flex_strlen(txt);
// yyunput(0, txt);
yyunput(0, txt);
}

View File

@ -18,15 +18,6 @@
*/
/*****************************************************************************
********************** L E X D E F I N I T I O N **********************
*****************************************************************************
* lex grammar for parsing ressource source files (*.xrm files)
* file is converted to gConXcs_yy.cxx with "flex"
*****************************************************************************/
/*************** O V E R W R I T I N G F U N C T I O N S ***************/
%top{
#include "gConvXcs.hxx"
@ -105,7 +96,7 @@ static void yy_flex_strncpy (char* s1, yyconst char * s2, int n )
void xcs_dummyJustForCompiler()
{
// char *txt = NULL;
char *txt = NULL;
// yy_flex_strlen(txt);
// yyunput(0, txt);
yyunput(0, txt);
}

View File

@ -18,15 +18,6 @@
*/
/*****************************************************************************
********************** L E X D E F I N I T I O N **********************
*****************************************************************************
* lex grammar for parsing ressource source files (*.xrm files)
* file is converted to gConXcu_yy.cxx with "flex"
*****************************************************************************/
/*************** O V E R W R I T I N G F U N C T I O N S ***************/
%top{
#include "gConvXcu.hxx"
@ -155,7 +146,7 @@ FIN [^/>]*">"
void xcu_dummyJustForCompiler()
{
// char *txt = NULL;
char *txt = NULL;
// yy_flex_strlen(txt);
// yyunput(0, txt);
yyunput(0, txt);
}

View File

@ -18,15 +18,6 @@
*/
/*****************************************************************************
********************** L E X D E F I N I T I O N **********************
*****************************************************************************
* lex grammar for parsing ressource source files (*.xrm files)
* file is converted to gConXrm_yy.cxx with "flex"
*****************************************************************************/
/*************** O V E R W R I T I N G F U N C T I O N S ***************/
%top{
#include "gConvXhp.hxx"
@ -191,7 +182,7 @@ IDENT [\.a-zA-Z0-9_-]+
void xhp_dummyJustForCompiler()
{
// char *txt = NULL;
char *txt = NULL;
// yy_flex_strlen(txt);
// yyunput(0, txt);
yyunput(0, txt);
}

View File

@ -18,15 +18,6 @@
*/
/*****************************************************************************
********************** L E X D E F I N I T I O N **********************
*****************************************************************************
* lex grammar for parsing ressource source files (*.xrm files)
* file is converted to gConXrm_yy.cxx with "flex"
*****************************************************************************/
/*************** O V E R W R I T I N G F U N C T I O N S ***************/
%top{
#include "gConvXrm.hxx"
@ -126,7 +117,7 @@ SP [ \t]*
void xrm_dummyJustForCompiler()
{
// char *txt = NULL;
char *txt = NULL;
// yy_flex_strlen(txt);
// yyunput(0, txt);
yyunput(0, txt);
}