Root/src/translator.cpp

1/***************************************************************************
2 * Copyright (C) 2006 by Massimiliano Torromeo *
3 * massimiliano.torromeo@gmail.com *
4 * *
5 * This program is free software; you can redistribute it and/or modify *
6 * it under the terms of the GNU General Public License as published by *
7 * the Free Software Foundation; either version 2 of the License, or *
8 * (at your option) any later version. *
9 * *
10 * This program is distributed in the hope that it will be useful, *
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of *
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
13 * GNU General Public License for more details. *
14 * *
15 * You should have received a copy of the GNU General Public License *
16 * along with this program; if not, write to the *
17 * Free Software Foundation, Inc., *
18 * 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. *
19 ***************************************************************************/
20
21#include <fstream>
22#include <iostream>
23#include <sstream>
24#include <stdarg.h>
25
26#include "translator.h"
27#include "debug.h"
28#include "gmenu2x.h"
29
30using namespace std;
31
32Translator::Translator(const string &lang) {
33    _lang = "";
34    if (!lang.empty())
35        setLang(lang);
36}
37
38Translator::~Translator() {}
39
40bool Translator::exists(const string &term) {
41    return translations.find(term) != translations.end();
42}
43
44void Translator::setLang(const string &lang) {
45    translations.clear();
46
47    string line;
48    ifstream infile ((GMenu2X::getHome() + "/translations/" + lang).c_str(), ios_base::in);
49    if (!infile.is_open())
50      infile.open((string(GMENU2X_SYSTEM_DIR "/translations/") + lang).c_str(), ios_base::in);
51
52    if (infile.is_open()) {
53        while (getline(infile, line, '\n')) {
54            line = trim(line);
55            if (line=="") continue;
56            if (line[0]=='#') continue;
57
58            string::size_type position = line.find("=");
59            translations[ trim(line.substr(0,position)) ] = trim(line.substr(position+1));
60        }
61        infile.close();
62        _lang = lang;
63    }
64}
65
66string Translator::translate(const string &term,const char *replacestr,...) {
67    string result = term;
68
69    if (!_lang.empty()) {
70        unordered_map<string, string>::iterator i = translations.find(term);
71        if (i != translations.end()) {
72            result = i->second;
73        }
74
75        else WARNING("Untranslated string: '%s'\n", term.c_str());
76    }
77
78    va_list arglist;
79    va_start(arglist, replacestr);
80    const char *param = replacestr;
81    int argnum = 1;
82    while (param!=NULL) {
83        string id = "";
84        stringstream ss; ss << argnum; ss >> id;
85        result = strreplace(result,"$"+id,param);
86
87        param = va_arg(arglist,const char*);
88        argnum++;
89    }
90    va_end(arglist);
91
92    return result;
93}
94
95string Translator::operator[](const string &term) {
96    return translate(term);
97}
98
99string Translator::lang() {
100    return _lang;
101}
102

Archive Download this file



interactive