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"
22 #include "util/string.h"
24 static Translations main_translations;
25 Translations *g_translations = &main_translations;
27 Translations::~Translations()
32 void Translations::clear()
34 m_translations.clear();
37 const std::wstring &Translations::getTranslation(
38 const std::wstring &textdomain, const std::wstring &s)
40 std::wstring key = textdomain + L"|" + s;
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;
53 void Translations::loadTranslation(const std::string &data)
55 std::istringstream is(data);
56 std::wstring textdomain;
60 std::getline(is, line);
61 if (str_starts_with(line, "# textdomain:")) {
62 textdomain = utf8_to_wide(trim(str_split(line, ':')[1]));
64 if (line.empty() || line[0] == '#')
67 std::wstring wline = utf8_to_wide(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.
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'=') {
83 } else if (wline[i + 1] == L'n') {
87 word1.put(wline[i + 1]);
91 // End of line, go to the next one.
97 std::getline(is, line);
98 wline = utf8_to_wide(line);
106 if (i == wline.length()) {
107 errorstream << "Malformed translation line \"" << line << "\""
113 while (i < wline.length()) {
114 if (wline[i] == L'@') {
115 if (i + 1 < wline.length()) {
116 if (wline[i + 1] == L'=') {
118 } else if (wline[i + 1] == L'n') {
122 word2.put(wline[i + 1]);
126 // End of line, go to the next one.
132 std::getline(is, line);
133 wline = utf8_to_wide(line);
141 std::wstring oword1 = word1.str(), oword2 = word2.str();
142 if (oword2.empty()) {
144 errorstream << "Ignoring empty translation for \""
145 << wide_to_utf8(oword1) << "\"" << std::endl;
148 m_translations[textdomain + L"|" + oword1] = oword2;