GCC Code Coverage Report
Directory: src/ Exec Total Coverage
File: src/utils/translation/translationmanager.cpp Lines: 9 59 15.3 %
Date: 2018-09-20 Branches: 6 90 6.7 %

Line Branch Exec Source
1
/*
2
 *  The ManaPlus Client
3
 *  Copyright (C) 2012-2018  The ManaPlus Developers
4
 *
5
 *  This file is part of The ManaPlus Client.
6
 *
7
 *  This program is free software; you can redistribute it and/or modify
8
 *  it under the terms of the GNU General Public License as published by
9
 *  the Free Software Foundation; either version 2 of the License, or
10
 *  any later version.
11
 *
12
 *  This program is distributed in the hope that it will be useful,
13
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
14
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15
 *  GNU General Public License for more details.
16
 *
17
 *  You should have received a copy of the GNU General Public License
18
 *  along with this program.  If not, see <http://www.gnu.org/licenses/>.
19
 */
20
21
#include "utils/translation/translationmanager.h"
22
23
#include "fs/virtfs/fs.h"
24
25
#include "utils/delete2.h"
26
#include "utils/stringutils.h"
27
28
#include "utils/foreach.h"
29
30
#include "utils/translation/podict.h"
31
#include "utils/translation/poparser.h"
32
33
#include "logger.h"
34
35
#include "debug.h"
36
37
102
void TranslationManager::init()
38
{
39
102
    delete translator;
40
102
    translator = PoParser::getEmptyDict();
41
102
}
42
43
void TranslationManager::loadCurrentLang()
44
{
45
    delete translator;
46
    const StringVect lang = getLang();
47
    translator = loadLang(lang, "", nullptr);
48
    translator = loadLang(lang, "help/", translator);
49
}
50
51
void TranslationManager::loadDictionaryLang()
52
{
53
    delete dictionary;
54
    delete reverseDictionary;
55
    dictionary = loadLang(getServerLang(), "dict/", nullptr);
56
    reverseDictionary = reverseLang(dictionary);
57
}
58
59
#ifdef ENABLE_CUSTOMNLS
60
void TranslationManager::loadGettextLang()
61
{
62
    delete mainTranslator;
63
    mainTranslator = loadLang(getLang(), "manaplus/", nullptr);
64
}
65
#endif  // ENABLE_CUSTOMNLS
66
67
215
void TranslationManager::close()
68
{
69
215
    delete2(translator);
70
215
    delete2(dictionary);
71
215
    delete2(reverseDictionary);
72
215
}
73
74
PoDict *TranslationManager::loadLang(const LangVect &lang,
75
                                     const std::string &subName,
76
                                     PoDict *const dict)
77
{
78
    std::string name;
79
    PoParser parser;
80
81
    FOR_EACH (LangIter, it, lang)
82
    {
83
        if (*it == "C")
84
            continue;
85
86
//        logger->log("check file: " + subName + *it);
87
        if (PoParser::checkLang(subName + *it))
88
        {
89
            name = *it;
90
            break;
91
        }
92
    }
93
    if (!name.empty())
94
        return parser.load(name, subName + name, dict);
95
    logger->log("can't find client data translation");
96
    if (dict != nullptr)
97
        return dict;
98
    return PoParser::getEmptyDict();
99
}
100
101
bool TranslationManager::translateFile(const std::string &fileName,
102
                                       PoDict *const dict,
103
                                       StringVect &lines)
104
{
105
    if ((dict == nullptr) || fileName.empty())
106
        return false;
107
108
    int contentsLength;
109
    const char *fileContents = VirtFs::loadFile(fileName,
110
        contentsLength);
111
112
    if (fileContents == nullptr)
113
    {
114
        logger->log("Couldn't load file: %s", fileName.c_str());
115
        return false;
116
    }
117
    std::string str = std::string(fileContents, contentsLength);
118
119
    size_t oldPos1 = std::string::npos;
120
    size_t pos1;
121
122
    while ((pos1 = str.find("<<")) != std::string::npos)
123
    {
124
        if (pos1 == oldPos1)
125
            break;  // detected infinite loop
126
        const size_t pos2 = str.find(">>", pos1 + 2);
127
        if (pos2 == std::string::npos)
128
            break;
129
        const std::string key(str.substr(pos1 + 2, pos2 - pos1 - 2));
130
        const std::string key2("<<" + str.substr(
131
            pos1 + 2, pos2 - pos1 - 2) + ">>");
132
        const std::string val(dict->getStr(key));
133
        replaceAll(str, key2, val);
134
        oldPos1 = pos1;
135
    }
136
137
    std::istringstream iss(str);
138
    std::string line;
139
140
    while (getline(iss, line))
141
        lines.push_back(line);
142
143
    delete [] fileContents;
144
    return true;
145
}
146
147
PoDict *TranslationManager::reverseLang(const PoDict *const dict)
148
{
149
    PoDict *const revDict = new PoDict(dict->mLang);
150
    FOR_EACH (PoMap::const_iterator, it, dict->mPoLines)
151
    {
152
        revDict->set((*it).second, (*it).first);
153
    }
154
    return revDict;
155
}