]> git.lizzy.rs Git - minetest.git/blob - src/translation.cpp
e8582f3286e3481ede2df829726488b5a92be73b
[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
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 (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 {
84                                                 word1.put(L'@');
85                                                 word1.put(wline[i + 1]);
86                                         }
87                                         i += 2;
88                                 } else {
89                                         // End of line, go to the next one.
90                                         word1.put(L'\n');
91                                         if (!is.good()) {
92                                                 break;
93                                         }
94                                         i = 0;
95                                         std::getline(is, line);
96                                         wline = utf8_to_wide(line);
97                                 }
98                         } else {
99                                 word1.put(wline[i]);
100                                 i++;
101                         }
102                 }
103
104                 if (i == wline.length()) {
105                         errorstream << "Malformed translation line \"" << line << "\""
106                                     << std::endl;
107                         continue;
108                 }
109                 i++;
110
111                 while (i < wline.length()) {
112                         if (wline[i] == L'@') {
113                                 if (i + 1 < wline.length()) {
114                                         if (wline[i + 1] == L'=') {
115                                                 word2.put(L'=');
116                                         } else {
117                                                 word2.put(L'@');
118                                                 word2.put(wline[i + 1]);
119                                         }
120                                         i += 2;
121                                 } else {
122                                         // End of line, go to the next one.
123                                         word2.put(L'\n');
124                                         if (!is.good()) {
125                                                 break;
126                                         }
127                                         i = 0;
128                                         std::getline(is, line);
129                                         wline = utf8_to_wide(line);
130                                 }
131                         } else {
132                                 word2.put(wline[i]);
133                                 i++;
134                         }
135                 }
136
137                 std::wstring oword1 = word1.str(), oword2 = word2.str();
138                 if (oword2.empty()) {
139                         oword2 = oword1;
140                         errorstream << "Ignoring empty translation for \""
141                                     << wide_to_utf8(oword1) << "\"" << std::endl;
142                 }
143
144                 m_translations[textdomain + L"|" + oword1] = oword2;
145         }
146 }