GCC Code Coverage Report
Directory: src/ Exec Total Coverage
File: src/utils/translation/translationmanager.cpp Lines: 9 59 15.3 %
Date: 2017-11-29 Branches: 5 94 5.3 %

Line Branch Exec Source
1
/*
2
 *  The ManaPlus Client
3
 *  Copyright (C) 2012-2017  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
152
void TranslationManager::init()
38
{
39
152
    delete translator;
40
152
    translator = PoParser::getEmptyDict();
41
152
}
42
43
void TranslationManager::loadCurrentLang()
44
{
45
    delete translator;
46
    translator = loadLang(getLang(), "");
47
    translator = loadLang(getLang(), "help/", translator);
48
}
49
50
void TranslationManager::loadDictionaryLang()
51
{
52
    delete dictionary;
53
    delete reverseDictionary;
54
    dictionary = loadLang(getServerLang(), "dict/");
55
    reverseDictionary = reverseLang(dictionary);
56
}
57
58
#ifdef ENABLE_CUSTOMNLS
59
void TranslationManager::loadGettextLang()
60
{
61
    delete mainTranslator;
62
    mainTranslator = loadLang(getLang(), "manaplus/");
63
}
64
#endif  // ENABLE_CUSTOMNLS
65
66
384
void TranslationManager::close()
67
{
68
384
    delete2(translator);
69
384
    delete2(dictionary);
70
384
    delete2(reverseDictionary);
71
384
}
72
73
PoDict *TranslationManager::loadLang(const LangVect &lang,
74
                                     const std::string &subName,
75
                                     PoDict *const dict)
76
{
77
    std::string name;
78
    PoParser parser;
79
80
    FOR_EACH (LangIter, it, lang)
81
    {
82
        if (*it == "C")
83
            continue;
84
85
//        logger->log("check file: " + subName + *it);
86
        if (PoParser::checkLang(subName + *it))
87
        {
88
            name = *it;
89
            break;
90
        }
91
    }
92
    if (!name.empty())
93
        return parser.load(name, subName + name, dict);
94
    logger->log("can't find client data translation");
95
    if (dict != nullptr)
96
        return dict;
97
    return PoParser::getEmptyDict();
98
}
99
100
bool TranslationManager::translateFile(const std::string &fileName,
101
                                       PoDict *const dict,
102
                                       StringVect &lines)
103
{
104
    if ((dict == nullptr) || fileName.empty())
105
        return false;
106
107
    int contentsLength;
108
    const char *fileContents = VirtFs::loadFile(fileName,
109
        contentsLength);
110
111
    if (fileContents == nullptr)
112
    {
113
        logger->log("Couldn't load file: %s", fileName.c_str());
114
        return false;
115
    }
116
    std::string str = std::string(fileContents, contentsLength);
117
118
    size_t oldPos1 = std::string::npos;
119
    size_t pos1;
120
121
    while ((pos1 = str.find("<<")) != std::string::npos)
122
    {
123
        if (pos1 == oldPos1)
124
            break;  // detected infinite loop
125
        const size_t pos2 = str.find(">>", pos1 + 2);
126
        if (pos2 == std::string::npos)
127
            break;
128
        const std::string key(str.substr(pos1 + 2, pos2 - pos1 - 2));
129
        const std::string key2("<<" + str.substr(
130
            pos1 + 2, pos2 - pos1 - 2) + ">>");
131
        const std::string val(dict->getStr(key));
132
        replaceAll(str, key2, val);
133
        oldPos1 = pos1;
134
    }
135
136
    std::istringstream iss(str);
137
    std::string line;
138
139
    while (getline(iss, line))
140
        lines.push_back(line);
141
142
    delete [] fileContents;
143
    return true;
144
}
145
146
PoDict *TranslationManager::reverseLang(const PoDict *const dict)
147
{
148
    PoDict *const revDict = new PoDict(dict->mLang);
149
    FOR_EACH (PoMap::const_iterator, it, dict->mPoLines)
150
    {
151
        revDict->set((*it).second, (*it).first);
152
    }
153
    return revDict;
154
}