Formspecs: Add starting frame to `animated_image` (#9411)
[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
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                 verbosestream << "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                 // Trim last character if file was using a \r\n line ending
62                 if (line.length () > 0 && line[line.length() - 1] == '\r')
63                         line.resize(line.length() - 1);
64
65                 if (str_starts_with(line, "# textdomain:")) {
66                         textdomain = utf8_to_wide(trim(str_split(line, ':')[1]));
67                 }
68                 if (line.empty() || line[0] == '#')
69                         continue;
70
71                 std::wstring wline = utf8_to_wide(line);
72                 if (wline.empty())
73                         continue;
74
75                 // Read line
76                 // '=' marks the key-value pair, but may be escaped by an '@'.
77                 // '\n' may also be escaped by '@'.
78                 // All other escapes are preserved.
79
80                 size_t i = 0;
81                 std::wostringstream word1, word2;
82                 while (i < wline.length() && wline[i] != L'=') {
83                         if (wline[i] == L'@') {
84                                 if (i + 1 < wline.length()) {
85                                         if (wline[i + 1] == L'=') {
86                                                 word1.put(L'=');
87                                         } else if (wline[i + 1] == L'n') {
88                                                 word1.put(L'\n');
89                                         } else {
90                                                 word1.put(L'@');
91                                                 word1.put(wline[i + 1]);
92                                         }
93                                         i += 2;
94                                 } else {
95                                         // End of line, go to the next one.
96                                         word1.put(L'\n');
97                                         if (!is.good()) {
98                                                 break;
99                                         }
100                                         i = 0;
101                                         std::getline(is, line);
102                                         wline = utf8_to_wide(line);
103                                 }
104                         } else {
105                                 word1.put(wline[i]);
106                                 i++;
107                         }
108                 }
109
110                 if (i == wline.length()) {
111                         errorstream << "Malformed translation line \"" << line << "\""
112                                     << std::endl;
113                         continue;
114                 }
115                 i++;
116
117                 while (i < wline.length()) {
118                         if (wline[i] == L'@') {
119                                 if (i + 1 < wline.length()) {
120                                         if (wline[i + 1] == L'=') {
121                                                 word2.put(L'=');
122                                         } else if (wline[i + 1] == L'n') {
123                                                 word2.put(L'\n');
124                                         } else {
125                                                 word2.put(L'@');
126                                                 word2.put(wline[i + 1]);
127                                         }
128                                         i += 2;
129                                 } else {
130                                         // End of line, go to the next one.
131                                         word2.put(L'\n');
132                                         if (!is.good()) {
133                                                 break;
134                                         }
135                                         i = 0;
136                                         std::getline(is, line);
137                                         wline = utf8_to_wide(line);
138                                 }
139                         } else {
140                                 word2.put(wline[i]);
141                                 i++;
142                         }
143                 }
144
145                 std::wstring oword1 = word1.str(), oword2 = word2.str();
146                 if (oword2.empty()) {
147                         oword2 = oword1;
148                         errorstream << "Ignoring empty translation for \""
149                                     << wide_to_utf8(oword1) << "\"" << std::endl;
150                 }
151
152                 std::wstring translation_index = textdomain + L"|";
153                 translation_index.append(oword1);
154                 m_translations[translation_index] = oword2;
155         }
156 }