]> git.lizzy.rs Git - dragonfireclient.git/blob - src/translation.cpp
Fix a -Wcatch-value warning reported by GCC 8.1
[dragonfireclient.git] / src / translation.cpp
1 /*
2 Minetest
3 Copyright (C) 2017 Nore, NathanaĆ«l Courant <nore@mesecons.net>
4
5 This program is free software; you can redistribute it and/or modify
6 it under the terms of the GNU Lesser General Public License as published by
7 the Free Software Foundation; either version 2.1 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 Lesser General Public License for more details.
14
15 You should have received a copy of the GNU Lesser General Public License along
16 with this program; if not, write to the Free Software Foundation, Inc.,
17 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
18 */
19
20 #include "translation.h"
21 #include "log.h"
22 #include "util/string.h"
23
24 static Translations main_translations;
25 Translations *g_translations = &main_translations;
26
27 Translations::~Translations()
28 {
29         clear();
30 }
31
32 void Translations::clear()
33 {
34         m_translations.clear();
35 }
36
37 const std::wstring &Translations::getTranslation(
38                 const std::wstring &textdomain, const std::wstring &s)
39 {
40         std::wstring key = textdomain + L"|" + s;
41         try {
42                 return m_translations.at(key);
43         } catch (const std::out_of_range &) {
44                 warningstream << "Translations: can't find translation for string \""
45                               << wide_to_utf8(s) << "\" in textdomain \""
46                               << wide_to_utf8(textdomain) << "\"" << std::endl;
47                 // Silence that warning in the future
48                 m_translations[key] = s;
49                 return s;
50         }
51 }
52
53 void Translations::loadTranslation(const std::string &data)
54 {
55         std::istringstream is(data);
56         std::wstring textdomain;
57         std::string line;
58
59         while (is.good()) {
60                 std::getline(is, line);
61                 if (str_starts_with(line, "# textdomain:")) {
62                         textdomain = utf8_to_wide(trim(str_split(line, ':')[1]));
63                 }
64                 if (line.empty() || line[0] == '#')
65                         continue;
66
67                 std::wstring wline = utf8_to_wide(line);
68                 if (wline.empty())
69                         continue;
70
71                 // Read line
72                 // '=' marks the key-value pair, but may be escaped by an '@'.
73                 // '\n' may also be escaped by '@'.
74                 // All other escapes are preserved.
75
76                 size_t i = 0;
77                 std::wostringstream word1, word2;
78                 while (i < wline.length() && wline[i] != L'=') {
79                         if (wline[i] == L'@') {
80                                 if (i + 1 < wline.length()) {
81                                         if (wline[i + 1] == L'=') {
82                                                 word1.put(L'=');
83                                         } else if (wline[i + 1] == L'n') {
84                                                 word1.put(L'\n');
85                                         } else {
86                                                 word1.put(L'@');
87                                                 word1.put(wline[i + 1]);
88                                         }
89                                         i += 2;
90                                 } else {
91                                         // End of line, go to the next one.
92                                         word1.put(L'\n');
93                                         if (!is.good()) {
94                                                 break;
95                                         }
96                                         i = 0;
97                                         std::getline(is, line);
98                                         wline = utf8_to_wide(line);
99                                 }
100                         } else {
101                                 word1.put(wline[i]);
102                                 i++;
103                         }
104                 }
105
106                 if (i == wline.length()) {
107                         errorstream << "Malformed translation line \"" << line << "\""
108                                     << std::endl;
109                         continue;
110                 }
111                 i++;
112
113                 while (i < wline.length()) {
114                         if (wline[i] == L'@') {
115                                 if (i + 1 < wline.length()) {
116                                         if (wline[i + 1] == L'=') {
117                                                 word2.put(L'=');
118                                         } else if (wline[i + 1] == L'n') {
119                                                 word2.put(L'\n');
120                                         } else {
121                                                 word2.put(L'@');
122                                                 word2.put(wline[i + 1]);
123                                         }
124                                         i += 2;
125                                 } else {
126                                         // End of line, go to the next one.
127                                         word2.put(L'\n');
128                                         if (!is.good()) {
129                                                 break;
130                                         }
131                                         i = 0;
132                                         std::getline(is, line);
133                                         wline = utf8_to_wide(line);
134                                 }
135                         } else {
136                                 word2.put(wline[i]);
137                                 i++;
138                         }
139                 }
140
141                 std::wstring oword1 = word1.str(), oword2 = word2.str();
142                 if (oword2.empty()) {
143                         oword2 = oword1;
144                         errorstream << "Ignoring empty translation for \""
145                                     << wide_to_utf8(oword1) << "\"" << std::endl;
146                 }
147
148                 m_translations[textdomain + L"|" + oword1] = oword2;
149         }
150 }