Logo
  • Main Page
  • Related Pages
  • Modules
  • Classes
  • Files

mmstranslator.cpp

Go to the documentation of this file.
00001 /***************************************************************************
00002  *   Copyright (C) 2005-2007 Stefan Schwarzer, Jens Schneider,             *
00003  *                           Matthias Hardt, Guido Madaus                  *
00004  *                                                                         *
00005  *   Copyright (C) 2007-2008 BerLinux Solutions GbR                        *
00006  *                           Stefan Schwarzer & Guido Madaus               *
00007  *                                                                         *
00008  *   Copyright (C) 2009-2013 BerLinux Solutions GmbH                       *
00009  *                                                                         *
00010  *   Authors:                                                              *
00011  *      Stefan Schwarzer   <stefan.schwarzer@diskohq.org>,                 *
00012  *      Matthias Hardt     <matthias.hardt@diskohq.org>,                   *
00013  *      Jens Schneider     <jens.schneider@diskohq.org>,                   *
00014  *      Guido Madaus       <guido.madaus@diskohq.org>,                     *
00015  *      Patrick Helterhoff <patrick.helterhoff@diskohq.org>,               *
00016  *      René Bählkow       <rene.baehlkow@diskohq.org>                     *
00017  *                                                                         *
00018  *   This library is free software; you can redistribute it and/or         *
00019  *   modify it under the terms of the GNU Lesser General Public            *
00020  *   License version 2.1 as published by the Free Software Foundation.     *
00021  *                                                                         *
00022  *   This library is distributed in the hope that it will be useful,       *
00023  *   but WITHOUT ANY WARRANTY; without even the implied warranty of        *
00024  *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU     *
00025  *   Lesser General Public License for more details.                       *
00026  *                                                                         *
00027  *   You should have received a copy of the GNU Lesser General Public      *
00028  *   License along with this library; if not, write to the                 *
00029  *   Free Software Foundation, Inc.,                                       *
00030  *   51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA            *
00031  **************************************************************************/
00032 
00033 #include "mmscore/mmstranslator.h"
00034 #include "mmsconfig/mmsconfigdata.h"
00035 #include "mmsconfig/mmspluginservice.h"
00036 #include "mmstools/tools.h"
00037 #include "mmstools/mmsfile.h"
00038 #include "mmstools/mmsfilesearch.h"
00039 
00040 #include <string.h>
00041 #include <stdexcept>
00042 
00043 bool MMSTranslator::firsttime = true;
00044 MMSLanguage MMSTranslator::sourcelang = MMSLANG_NONE;
00045 MMSLanguage MMSTranslator::targetlang = MMSLANG_NONE;
00046 int MMSTranslator::sourceIdx = -1;
00047 int MMSTranslator::targetIdx = -1;
00048 MMSTranslator::MMSTRANSLATION_INDEX MMSTranslator::transIdx;
00049 MMSTranslator::MMSTRANSLATION_MAP MMSTranslator::transmap;
00050 MMSTranslator::MMSTRANSLATION_FILES MMSTranslator::files;
00051 
00052 sigc::signal<void, MMSLanguage>  MMSTranslator::onTargetLangChanged;
00053 bool MMSTranslator::addtranslations;
00054 
00055 MMSTranslator::MMSTranslator() {
00056     if (this->firsttime) {
00057         MMSConfigData config;
00058         this->sourcelang = config.getSourceLang();
00059         this->targetlang = config.getDefaultTargetLang();
00060         this->addtranslations = config.getAddTranslations();
00061         this->firsttime = false;
00062     }
00063 
00064 /*
00065 QUESTION: sourcelang not used? why???
00066     if (this->sourcelang != MMSLANG_NONE && this->targetlang != MMSLANG_NONE)
00067         loadTranslations();
00068 */
00069 
00070     if (this->targetlang != MMSLANG_NONE)
00071         loadTranslations();
00072 }
00073 
00074 MMSTranslator::~MMSTranslator() {
00075 
00076 }
00077 
00078 void MMSTranslator::loadTranslations() {
00079     MMSConfigData config;
00080     DataSource source(config.getConfigDBDBMS(),
00081                       config.getConfigDBDatabase(),
00082                       config.getConfigDBAddress(),
00083                       config.getConfigDBPort(),
00084                       config.getConfigDBUser(),
00085                       config.getConfigDBPassword());
00086 
00087     try {
00088         MMSPluginService service(&source);
00089         vector<MMSPluginData *> data = service.getAllPlugins();
00090 
00091         for(vector<MMSPluginData *>::iterator it = data.begin();it!=data.end();it++) {
00092             MMSFileSearch search((*it)->getPath(), "translation.??", true);
00093             MMSFILEENTRY_LIST ret =  search.execute();
00094             for(MMSFILEENTRY_LIST::iterator it2 = ret.begin(); it2 != ret.end();it2++) {
00095                 processFile((*it2)->name);
00096                 // clean
00097                 delete (*it2);
00098             }
00099             ret.clear();
00100         }
00101         // clean
00102         for(vector<MMSPluginData *>::iterator it = data.begin();it!=data.end();it++) {
00103             (*it)->clear();
00104             delete (*it);
00105         }
00106         data.clear();
00107 
00108     } catch (MMSError &err) {
00109         DEBUGMSG("MMSTranslator", "No plugins database found for translation.");
00110     }
00111 
00112     MMSFileSearch search(config.getLanguagefileDir(), "translation.??", false);
00113     MMSFILEENTRY_LIST ret =  search.execute();
00114     for(MMSFILEENTRY_LIST::iterator it2 = ret.begin(); it2 != ret.end();it2++) {
00115         processFile((*it2)->name);
00116         // clean
00117         delete (*it2);
00118     }
00119     ret.clear();
00120 
00121 //  this->sourceIdx = this->transIdx.find(this->sourcelang)->second;
00122     this->targetIdx = this->transIdx.find(this->targetlang)->second;
00123 }
00124 
00125 void MMSTranslator::addMissing(const string &phrase, const bool completemiss) {
00126     if(phrase.empty()) {
00127         return;
00128     }
00129 
00130     size_t size = this->files.size();
00131 
00132     if(completemiss) {
00133         //add to all language files;
00134         for(unsigned int idx = 0; idx < size; ++idx) {
00135             MMSFile file(this->files.at(idx), MMSFM_APPEND, false);
00136             char line[1024];
00137             snprintf(line, sizeof(line) - 4, "%s===\n", phrase.c_str());
00138             file.writeBuffer(line, NULL, strlen(line), 1);
00139 
00140             MMSTRANSLATION_MAP::iterator transit = this->transmap.find(phrase);
00141             if(transit != this->transmap.end()) {
00142                 transit->second.at(idx) = phrase;
00143             } else {
00144                 vector<string> trans(this->files.size());
00145                 trans.at(idx) = phrase;
00146                 transmap.insert(make_pair(phrase, trans));
00147             }
00148         }
00149     } else {
00150         //check the single languages...
00151         MMSTRANSLATION_MAP::iterator transit = this->transmap.find(phrase);
00152         for(unsigned int idx = 0; idx < size; ++idx) {
00153             if(transit->second.at(idx).empty()) {
00154                 MMSFile file(this->files.at(idx), MMSFM_APPEND, false);
00155                 char line[1024];
00156                 snprintf(line, sizeof(line) - 4, "%s===\n", phrase.c_str());
00157                 file.writeBuffer(line, NULL, strlen(line), 1);
00158                 transit->second.at(idx) = phrase;
00159             }
00160         }
00161     }
00162 }
00163 
00164 
00165 void MMSTranslator::translate(const string &source, string &dest) {
00166     if((this->targetIdx == -1) || source.empty()) {
00167         dest = source;
00168         return;
00169     }
00170 
00171     MMSTRANSLATION_MAP::iterator it = this->transmap.find(source);
00172     if(it == this->transmap.end()) {
00173         dest = source;
00174         if(this->addtranslations) {
00175             addMissing(source, true);
00176         }
00177     } else {
00178         if(it->second.size() > this->targetIdx) {
00179             dest = it->second.at(this->targetIdx);
00180             if(dest.empty()) {
00181                 dest = source;
00182             }
00183         } else {
00184             dest = source;
00185         }
00186     }
00187 
00188     size_t lookHere = 0;
00189     size_t foundHere;
00190     string from = "\\n";
00191     string to = "\n";
00192     while((foundHere = dest.find(from, lookHere)) != string::npos) {
00193         dest.replace(foundHere, from.size(), to);
00194         lookHere = foundHere + to.size();
00195     }
00196 
00197 }
00198 
00199 bool MMSTranslator::setTargetLang(MMSLanguage lang) {
00200     MMSTRANSLATION_INDEX::iterator it = this->transIdx.find(lang);
00201     if (it == this->transIdx.end())
00202         return false;
00203 
00204     this->targetlang = lang;
00205     this->targetIdx = it->second;
00206     this->onTargetLangChanged.emit(this->targetlang);
00207 
00208     return true;
00209 }
00210 
00211 MMSLanguage MMSTranslator::getTargetLang() {
00212     return this->targetlang;
00213 }
00214 
00215 void MMSTranslator::processFile(const string &file) {
00216     MMSFile transfile(file,MMSFM_READ,false);
00217     string line;
00218     string from, to;
00219     size_t idx;
00220     string countryCode = file.substr(file.find("translation")+12);
00221     MMSLanguage lang = getMMSLanguageFromString(strToUpr(countryCode));
00222 
00223     MMSTRANSLATION_INDEX::iterator it = this->transIdx.find(lang);
00224     if(it == this->transIdx.end()) {
00225         idx = this->files.size();
00226         this->transIdx[lang] = idx;
00227         this->files.push_back(file);
00228         for(MMSTRANSLATION_MAP::iterator it = this->transmap.begin(); it != this->transmap.end(); ++it) {
00229             it->second.resize(idx + 1, "");
00230         }
00231     } else {
00232         idx = it->second;
00233     }
00234 
00235     while(transfile.getLine(line)) {
00236         size_t pos = line.find("===");
00237         if(pos != string::npos) {
00238             from = line.substr(0, pos);
00239             to = line.substr(pos+3, string::npos);
00240 
00241             MMSTRANSLATION_MAP::iterator f = this->transmap.find(from);
00242             if(f != this->transmap.end()) {
00243                 //already have the source
00244                 DEBUGMSG("MMSTranslator", "insert: '%s'", from.c_str());
00245                 try {
00246                     f->second.at(idx) = to;
00247                 } catch(std::exception &ex) {
00248                     f->second.resize(idx + 1, "");
00249                     f->second.at(idx) = to;
00250                 }
00251             } else {
00252                 DEBUGMSG("MMSTranslator", "fresh insert: '%s'", from.c_str());
00253                 vector<string> trans(idx + 1);
00254                 trans.at(idx) = to;
00255                 transmap.insert(make_pair(from, trans));
00256             }
00257         }
00258     }
00259 }

Generated by doxygen