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