2010-02-04 13:33:47 +02:00
|
|
|
/***************************************************************************
|
|
|
|
* Copyright (C) 2006 by Massimiliano Torromeo *
|
|
|
|
* massimiliano.torromeo@gmail.com *
|
|
|
|
* *
|
|
|
|
* This program is free software; you can redistribute it and/or modify *
|
|
|
|
* it under the terms of the GNU General Public License as published by *
|
|
|
|
* the Free Software Foundation; either version 2 of the License, or *
|
|
|
|
* (at your option) any later version. *
|
|
|
|
* *
|
|
|
|
* This program is distributed in the hope that it will be useful, *
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of *
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
|
|
|
|
* GNU General Public License for more details. *
|
|
|
|
* *
|
|
|
|
* You should have received a copy of the GNU General Public License *
|
|
|
|
* along with this program; if not, write to the *
|
|
|
|
* Free Software Foundation, Inc., *
|
|
|
|
* 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. *
|
|
|
|
***************************************************************************/
|
|
|
|
|
|
|
|
#include <iostream>
|
|
|
|
#include <sstream>
|
|
|
|
#include <stdarg.h>
|
|
|
|
|
|
|
|
#include "translator.h"
|
|
|
|
|
|
|
|
using namespace std;
|
|
|
|
|
2010-05-02 15:29:09 +03:00
|
|
|
Translator::Translator(const string &lang) {
|
2010-02-04 13:33:47 +02:00
|
|
|
_lang = "";
|
|
|
|
if (!lang.empty())
|
|
|
|
setLang(lang);
|
|
|
|
}
|
|
|
|
|
|
|
|
Translator::~Translator() {}
|
|
|
|
|
2010-05-02 15:29:09 +03:00
|
|
|
bool Translator::exists(const string &term) {
|
2010-02-04 13:33:47 +02:00
|
|
|
return translations.find(term) != translations.end();
|
|
|
|
}
|
|
|
|
|
2010-05-02 15:29:09 +03:00
|
|
|
void Translator::setLang(const string &lang) {
|
2010-02-04 13:33:47 +02:00
|
|
|
translations.clear();
|
|
|
|
|
|
|
|
string line;
|
|
|
|
ifstream infile (string("translations/"+lang).c_str(), ios_base::in);
|
|
|
|
if (infile.is_open()) {
|
|
|
|
while (getline(infile, line, '\n')) {
|
|
|
|
line = trim(line);
|
|
|
|
if (line=="") continue;
|
|
|
|
if (line[0]=='#') continue;
|
|
|
|
|
|
|
|
string::size_type position = line.find("=");
|
|
|
|
translations[ trim(line.substr(0,position)) ] = trim(line.substr(position+1));
|
|
|
|
}
|
|
|
|
infile.close();
|
|
|
|
_lang = lang;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2010-05-02 15:29:09 +03:00
|
|
|
string Translator::translate(const string &term,const char *replacestr,...) {
|
2010-02-04 13:33:47 +02:00
|
|
|
string result = term;
|
|
|
|
|
|
|
|
if (!_lang.empty()) {
|
2010-06-18 01:45:16 +03:00
|
|
|
unordered_map<string, string>::iterator i = translations.find(term);
|
2010-02-04 13:33:47 +02:00
|
|
|
if (i != translations.end()) {
|
|
|
|
result = i->second;
|
|
|
|
}
|
|
|
|
#ifdef DEBUG
|
|
|
|
else cout << "Untranslated string: " << term << endl;
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
|
|
|
va_list arglist;
|
|
|
|
va_start(arglist, replacestr);
|
|
|
|
const char *param = replacestr;
|
|
|
|
int argnum = 1;
|
|
|
|
while (param!=NULL) {
|
|
|
|
string id = "";
|
|
|
|
stringstream ss; ss << argnum; ss >> id;
|
|
|
|
result = strreplace(result,"$"+id,param);
|
|
|
|
|
|
|
|
param = va_arg(arglist,const char*);
|
|
|
|
argnum++;
|
|
|
|
}
|
|
|
|
va_end(arglist);
|
|
|
|
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
2010-05-02 15:29:09 +03:00
|
|
|
string Translator::operator[](const string &term) {
|
2010-02-04 13:33:47 +02:00
|
|
|
return translate(term);
|
|
|
|
}
|
|
|
|
|
|
|
|
string Translator::lang() {
|
|
|
|
return _lang;
|
|
|
|
}
|