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

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