Root/src/translator.cpp

Source at commit 73ceffa51d3450d2d20ce933e7fc6da187e09fc8 created 7 years 5 months ago.
By Maarten ter Huurne, Fix bug in section directory creation method
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 "translator.h"
22
23#include "debug.h"
24#include "gmenu2x.h"
25#include "utilities.h"
26
27#include <fstream>
28#include <iostream>
29#include <sstream>
30#include <stdarg.h>
31
32using namespace std;
33
34Translator::Translator(const string &lang) {
35    _lang = "";
36    if (!lang.empty())
37        setLang(lang);
38}
39
40Translator::~Translator() {}
41
42bool Translator::exists(const string &term) {
43    return translations.find(term) != translations.end();
44}
45
46void Translator::setLang(const string &lang) {
47    translations.clear();
48
49    string line;
50    ifstream infile ((GMenu2X::getHome() + "/translations/" + lang).c_str(), ios_base::in);
51    if (!infile.is_open())
52      infile.open((string(GMENU2X_SYSTEM_DIR "/translations/") + lang).c_str(), ios_base::in);
53
54    if (infile.is_open()) {
55        while (getline(infile, line, '\n')) {
56            line = trim(line);
57            if (line.empty()) continue;
58            if (line[0]=='#') continue;
59
60            string::size_type position = line.find("=");
61            translations[ trim(line.substr(0,position)) ] = trim(line.substr(position+1));
62        }
63        infile.close();
64        _lang = lang;
65    }
66}
67
68string Translator::translate(const string &term,const char *replacestr,...) {
69    string result = term;
70
71    if (!_lang.empty()) {
72        unordered_map<string, string>::iterator i = translations.find(term);
73        if (i != translations.end()) {
74            result = i->second;
75        }
76
77        else WARNING("Untranslated string: '%s'\n", term.c_str());
78    }
79
80    va_list arglist;
81    va_start(arglist, replacestr);
82    const char *param = replacestr;
83    int argnum = 1;
84    while (param!=NULL) {
85        string id = "";
86        stringstream ss; ss << argnum; ss >> id;
87        result = strreplace(result,"$"+id,param);
88
89        param = va_arg(arglist,const char*);
90        argnum++;
91    }
92    va_end(arglist);
93
94    return result;
95}
96
97string Translator::operator[](const string &term) {
98    return translate(term);
99}
100
101string Translator::lang() {
102    return _lang;
103}
104

Archive Download this file



interactive