nlist: make selected list accessible globally
[waspsaliva.git] / src / translation.cpp
blob82e813a5d59b0f8f3813a828c566baa7260fcf18
1 /*
2 Minetest
3 Copyright (C) 2017 Nore, Nathanaƫl Courant <nore@mesecons.net>
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.
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.
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.
20 #include "translation.h"
21 #include "log.h"
22 #include "util/string.h"
23 #include <unordered_map>
26 #ifndef SERVER
27 // Client translations
28 Translations client_translations;
29 Translations *g_client_translations = &client_translations;
30 #endif
33 void Translations::clear()
35 m_translations.clear();
38 const std::wstring &Translations::getTranslation(
39 const std::wstring &textdomain, const std::wstring &s)
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;
54 void Translations::loadTranslation(const std::string &data)
56 std::istringstream is(data);
57 std::wstring textdomain;
58 std::string line;
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);
66 if (str_starts_with(line, "# textdomain:")) {
67 textdomain = utf8_to_wide(trim(str_split(line, ':')[1]));
69 if (line.empty() || line[0] == '#')
70 continue;
72 std::wstring wline = utf8_to_wide(line);
73 if (wline.empty())
74 continue;
76 // Read line
77 // '=' marks the key-value pair, but may be escaped by an '@'.
78 // '\n' may also be escaped by '@'.
79 // All other escapes are preserved.
81 size_t i = 0;
82 std::wostringstream word1, word2;
83 while (i < wline.length() && wline[i] != L'=') {
84 if (wline[i] == L'@') {
85 if (i + 1 < wline.length()) {
86 if (wline[i + 1] == L'=') {
87 word1.put(L'=');
88 } else if (wline[i + 1] == L'n') {
89 word1.put(L'\n');
90 } else {
91 word1.put(L'@');
92 word1.put(wline[i + 1]);
94 i += 2;
95 } else {
96 // End of line, go to the next one.
97 word1.put(L'\n');
98 if (!is.good()) {
99 break;
101 i = 0;
102 std::getline(is, line);
103 wline = utf8_to_wide(line);
105 } else {
106 word1.put(wline[i]);
107 i++;
111 if (i == wline.length()) {
112 errorstream << "Malformed translation line \"" << line << "\""
113 << std::endl;
114 continue;
116 i++;
118 while (i < wline.length()) {
119 if (wline[i] == L'@') {
120 if (i + 1 < wline.length()) {
121 if (wline[i + 1] == L'=') {
122 word2.put(L'=');
123 } else if (wline[i + 1] == L'n') {
124 word2.put(L'\n');
125 } else {
126 word2.put(L'@');
127 word2.put(wline[i + 1]);
129 i += 2;
130 } else {
131 // End of line, go to the next one.
132 word2.put(L'\n');
133 if (!is.good()) {
134 break;
136 i = 0;
137 std::getline(is, line);
138 wline = utf8_to_wide(line);
140 } else {
141 word2.put(wline[i]);
142 i++;
146 std::wstring oword1 = word1.str(), oword2 = word2.str();
147 if (oword2.empty()) {
148 oword2 = oword1;
149 errorstream << "Ignoring empty translation for \""
150 << wide_to_utf8(oword1) << "\"" << std::endl;
153 std::wstring translation_index = textdomain + L"|";
154 translation_index.append(oword1);
155 m_translations[translation_index] = oword2;