forked from telegramdesktop/tdesktop
-
Notifications
You must be signed in to change notification settings - Fork 0
/
lang.cpp
168 lines (151 loc) · 5.7 KB
/
lang.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
/*
This file is part of Telegram Desktop,
the official desktop version of Telegram messaging app, see https://telegram.org
Telegram Desktop is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
It is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
In addition, as a special exception, the copyright holders give permission
to link the code of portions of this program with the OpenSSL library.
Full license: https://github.com/telegramdesktop/tdesktop/blob/master/LICENSE
Copyright (c) 2014-2017 John Preston, https://desktop.telegram.org
*/
#include "lang.h"
#include "langloaderplain.h"
LangString LangString::tag(ushort tag, const QString &replacement) {
for (const QChar *s = constData(), *ch = s, *e = ch + size(); ch != e;) {
if (*ch == TextCommand) {
if (ch + 3 < e && (ch + 1)->unicode() == TextCommandLangTag && *(ch + 3) == TextCommand) {
if ((ch + 2)->unicode() == 0x0020 + tag) {
LangString result;
result.reserve(size() + replacement.size() - 4);
if (ch > s) result.append(midRef(0, ch - s));
result.append(replacement);
if (ch + 4 < e) result.append(midRef(ch - s + 4));
return result;
} else {
ch += 4;
}
} else {
const QChar *next = textSkipCommand(ch, e);
if (next == ch) {
++ch;
} else {
ch = next;
}
}
} else {
++ch;
}
}
return *this;
}
LangString langCounted(ushort key0, ushort tag, float64 value) { // current lang dependent
int v = qFloor(value);
QString sv;
ushort key = key0;
if (v != qCeil(value)) {
key += 2;
sv = QString::number(value);
} else {
if (v == 1) {
key += 1;
} else if (v) {
key += 2;
}
sv = QString::number(v);
}
while (key > key0) {
LangString v = lang(LangKey(key));
if (!v.isEmpty()) return v.tag(tag, sv);
--key;
}
return lang(LangKey(key0)).tag(tag, sv);
}
//#define NEW_VER_TAG lt_link
//#define NEW_VER_TAG_VALUE "https://telegram.org/blog/desktop-1-0"
QString langNewVersionText() {
#ifdef NEW_VER_TAG
return lng_new_version_text(NEW_VER_TAG, QString::fromUtf8(NEW_VER_TAG_VALUE));
#else // NEW_VER_TAG
return lang(lng_new_version_text);
#endif // NEW_VER_TAG
}
#ifdef NEW_VER_TAG
#define NEW_VER_KEY lng_new_version_text__tagged
#define NEW_VER_POSTFIX .tag(NEW_VER_TAG, QString::fromUtf8(NEW_VER_TAG_VALUE))
#else // NEW_VER_TAG
#define NEW_VER_KEY lng_new_version_text
#define NEW_VER_POSTFIX
#endif // NEW_VER_TAG
QString langNewVersionTextForLang(int langId) {
LangLoaderResult result;
if (langId) {
LangLoaderPlain loader(qsl(":/langs/lang_") + LanguageCodes[langId].c_str() + qsl(".strings"), langLoaderRequest(lng_language_name, NEW_VER_KEY));
result = loader.found();
} else {
result.insert(lng_language_name, langOriginal(lng_language_name));
result.insert(NEW_VER_KEY, langOriginal(NEW_VER_KEY));
}
return result.value(lng_language_name, LanguageCodes[langId].c_str() + qsl(" language")) + qsl(":\n\n") + LangString(result.value(NEW_VER_KEY, qsl("--none--")))NEW_VER_POSTFIX;
}
#undef NEW_VER_POSTFIX
#undef NEW_VER_KEY
#undef NEW_VER_TAG_VALUE
#undef NEW_VER_TAG
const QString &LangLoader::errors() const {
if (_errors.isEmpty() && !_err.isEmpty()) {
_errors = _err.join('\n');
}
return _errors;
}
const QString &LangLoader::warnings() const {
if (!_checked) {
for (int32 i = 0; i < lngkeys_cnt; ++i) {
if (!_found[i]) {
_warn.push_back(qsl("No value found for key '%1'").arg(langKeyName(LangKey(i))));
}
}
_checked = true;
}
if (_warnings.isEmpty() && !_warn.isEmpty()) {
_warnings = _warn.join('\n');
}
return _warnings;
}
void LangLoader::foundKeyValue(LangKey key) {
if (key < lngkeys_cnt) {
_found[key] = true;
}
}
QString Translator::translate(const char *context, const char *sourceText, const char *disambiguation, int n) const {
if (qstr("QMenuBar") == context) {
if (qstr("Services") == sourceText) return lang(lng_mac_menu_services);
if (qstr("Hide %1") == sourceText) return lng_mac_menu_hide_telegram(lt_telegram, qsl("%1"));
if (qstr("Hide Others") == sourceText) return lang(lng_mac_menu_hide_others);
if (qstr("Show All") == sourceText) return lang(lng_mac_menu_show_all);
if (qstr("Preferences...") == sourceText) return lang(lng_mac_menu_preferences);
if (qstr("Quit %1") == sourceText) return lng_mac_menu_quit_telegram(lt_telegram, qsl("%1"));
if (qstr("About %1") == sourceText) return lng_mac_menu_about_telegram(lt_telegram, qsl("%1"));
return QString();
}
if (qstr("QWidgetTextControl") == context || qstr("QLineEdit") == context) {
if (qstr("&Undo") == sourceText) return lang((cPlatform() == dbipWindows) ? lng_wnd_menu_undo : ((cPlatform() == dbipMac || cPlatform() == dbipMacOld) ? lng_mac_menu_undo : lng_linux_menu_undo));
if (qstr("&Redo") == sourceText) return lang((cPlatform() == dbipWindows) ? lng_wnd_menu_redo : ((cPlatform() == dbipMac || cPlatform() == dbipMacOld) ? lng_mac_menu_redo : lng_linux_menu_redo));
if (qstr("Cu&t") == sourceText) return lang(lng_mac_menu_cut);
if (qstr("&Copy") == sourceText) return lang(lng_mac_menu_copy);
if (qstr("&Paste") == sourceText) return lang(lng_mac_menu_paste);
if (qstr("Delete") == sourceText) return lang(lng_mac_menu_delete);
if (qstr("Select All") == sourceText) return lang(lng_mac_menu_select_all);
return QString();
}
if (qstr("QUnicodeControlCharacterMenu") == context) {
if (qstr("Insert Unicode control character") == sourceText) return lang(lng_menu_insert_unicode);
return QString();
}
return QString();
}