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 // 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);
65 if (str_starts_with(line, "# textdomain:")) {
66 textdomain = utf8_to_wide(trim(str_split(line, ':')[1]));
68 if (line.empty() || line[0] == '#')
71 std::wstring wline = utf8_to_wide(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.
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'=') {
87 } else if (wline[i + 1] == L'n') {
91 word1.put(wline[i + 1]);
95 // End of line, go to the next one.
101 std::getline(is, line);
102 wline = utf8_to_wide(line);
110 if (i == wline.length()) {
111 errorstream << "Malformed translation line \"" << line << "\""
117 while (i < wline.length()) {
118 if (wline[i] == L'@') {
119 if (i + 1 < wline.length()) {
120 if (wline[i + 1] == L'=') {
122 } else if (wline[i + 1] == L'n') {
126 word2.put(wline[i + 1]);
130 // End of line, go to the next one.
136 std::getline(is, line);
137 wline = utf8_to_wide(line);
145 std::wstring oword1 = word1.str(), oword2 = word2.str();
146 if (oword2.empty()) {
148 errorstream << "Ignoring empty translation for \""
149 << wide_to_utf8(oword1) << "\"" << std::endl;
152 m_translations[textdomain + L"|" + oword1] = oword2;