]> git.lizzy.rs Git - dragonfireclient.git/blob - src/translation.cpp
Force-update shadows when the world is changed (#12364)
[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 #include <unordered_map>
24
25
26 #ifndef SERVER
27 // Client translations
28 Translations client_translations;
29 Translations *g_client_translations = &client_translations;
30 #endif
31
32
33 void Translations::clear()
34 {
35         m_translations.clear();
36 }
37
38 const std::wstring &Translations::getTranslation(
39                 const std::wstring &textdomain, const std::wstring &s)
40 {
41         std::wstring key = textdomain + L"|" + s;
42         try {
43                 return m_translations.at(key);
44         } catch (const std::out_of_range &) {
45                 verbosestream << "Translations: can't find translation for string \""
46                               << wide_to_utf8(s) << "\" in textdomain \""
47                               << wide_to_utf8(textdomain) << "\"" << std::endl;
48                 // Silence that warning in the future
49                 m_translations[key] = s;
50                 return s;
51         }
52 }
53
54 void Translations::loadTranslation(const std::string &data)
55 {
56         std::istringstream is(data);
57         std::wstring textdomain;
58         std::string line;
59
60         while (is.good()) {
61                 std::getline(is, line);
62                 // Trim last character if file was using a \r\n line ending
63                 if (line.length () > 0 && line[line.length() - 1] == '\r')
64                         line.resize(line.length() - 1);
65
66                 if (str_starts_with(line, "# textdomain:")) {
67                         auto parts = str_split(line, ':');
68                         if (parts.size() < 2) {
69                                 errorstream << "Invalid textdomain translation line \"" << line
70                                                 << "\"" << std::endl;
71                                 continue;
72                         }
73                         textdomain = utf8_to_wide(trim(parts[1]));
74                 }
75                 if (line.empty() || line[0] == '#')
76                         continue;
77
78                 std::wstring wline = utf8_to_wide(line);
79                 if (wline.empty())
80                         continue;
81
82                 // Read line
83                 // '=' marks the key-value pair, but may be escaped by an '@'.
84                 // '\n' may also be escaped by '@'.
85                 // All other escapes are preserved.
86
87                 size_t i = 0;
88                 std::wostringstream word1, word2;
89                 while (i < wline.length() && wline[i] != L'=') {
90                         if (wline[i] == L'@') {
91                                 if (i + 1 < wline.length()) {
92                                         if (wline[i + 1] == L'=') {
93                                                 word1.put(L'=');
94                                         } else if (wline[i + 1] == L'n') {
95                                                 word1.put(L'\n');
96                                         } else {
97                                                 word1.put(L'@');
98                                                 word1.put(wline[i + 1]);
99                                         }
100                                         i += 2;
101                                 } else {
102                                         // End of line, go to the next one.
103                                         word1.put(L'\n');
104                                         if (!is.good()) {
105                                                 break;
106                                         }
107                                         i = 0;
108                                         std::getline(is, line);
109                                         wline = utf8_to_wide(line);
110                                 }
111                         } else {
112                                 word1.put(wline[i]);
113                                 i++;
114                         }
115                 }
116
117                 if (i == wline.length()) {
118                         errorstream << "Malformed translation line \"" << line << "\""
119                                     << std::endl;
120                         continue;
121                 }
122                 i++;
123
124                 while (i < wline.length()) {
125                         if (wline[i] == L'@') {
126                                 if (i + 1 < wline.length()) {
127                                         if (wline[i + 1] == L'=') {
128                                                 word2.put(L'=');
129                                         } else if (wline[i + 1] == L'n') {
130                                                 word2.put(L'\n');
131                                         } else {
132                                                 word2.put(L'@');
133                                                 word2.put(wline[i + 1]);
134                                         }
135                                         i += 2;
136                                 } else {
137                                         // End of line, go to the next one.
138                                         word2.put(L'\n');
139                                         if (!is.good()) {
140                                                 break;
141                                         }
142                                         i = 0;
143                                         std::getline(is, line);
144                                         wline = utf8_to_wide(line);
145                                 }
146                         } else {
147                                 word2.put(wline[i]);
148                                 i++;
149                         }
150                 }
151
152                 std::wstring oword1 = word1.str(), oword2 = word2.str();
153                 if (!oword2.empty()) {
154                         std::wstring translation_index = textdomain + L"|";
155                         translation_index.append(oword1);
156                         m_translations[translation_index] = oword2;
157                 } else {
158                         infostream << "Ignoring empty translation for \""
159                                 << wide_to_utf8(oword1) << "\"" << std::endl;
160                 }
161         }
162 }