forked from telegramdesktop/tdesktop
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlang.h
204 lines (172 loc) · 7.02 KB
/
lang.h
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
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
/*
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-2016 John Preston, https://desktop.telegram.org
*/
#pragma once
constexpr const str_const LanguageCodes[] = {
"en",
"it",
"es",
"de",
"nl",
"pt_BR",
"ko",
};
constexpr const int languageTest = -1, languageDefault = 0, languageCount = arraysize(LanguageCodes);
class LangString : public QString {
public:
LangString() {
}
LangString(const QString &str) : QString(str) {
}
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 &operator=(const QString &str) {
QString::operator=(str);
return (*this);
}
};
LangString langCounted(ushort key0, ushort tag, float64 value);
#include "lang_auto.h"
const char *langKeyName(LangKey key);
inline LangString langDayOfMonth(const QDate &date) {
QDate c(QDate::currentDate());
int32 month = date.month(), day = date.day(), year = date.year(), cyear = c.year(), cmonth = c.month();
if (year != cyear) {
if (year > cyear + 1 || cyear > year + 1 || (year == cyear + 1 && month + 12 > cmonth + 3) || (cyear == year + 1 && cmonth + 12 > month + 3)) {
return (month > 0 && month <= 12) ? lng_month_day_year(lt_month, lang(LangKey(lng_month1_small + month - 1)), lt_day, QString::number(day), lt_year, QString::number(year)) : qsl("MONTH_ERR");
}
}
return (month > 0 && month <= 12) ? lng_month_day(lt_month, lang(LangKey(lng_month1_small + month - 1)), lt_day, QString::number(day)) : qsl("MONTH_ERR");
}
inline LangString langDayOfMonthFull(const QDate &date) {
QDate c(QDate::currentDate());
int32 month = date.month(), day = date.day(), year = date.year(), cyear = c.year(), cmonth = c.month();
if (year != cyear) {
if (year > cyear + 1 || cyear > year + 1 || (year == cyear + 1 && month + 12 > cmonth + 3) || (cyear == year + 1 && cmonth + 12 > month + 3)) {
return (month > 0 && month <= 12) ? lng_month_day_year(lt_month, lang(LangKey(lng_month1 + month - 1)), lt_day, QString::number(day), lt_year, QString::number(year)) : qsl("MONTH_ERR");
}
}
return (month > 0 && month <= 12) ? lng_month_day(lt_month, lang(LangKey(lng_month1 + month - 1)), lt_day, QString::number(day)) : qsl("MONTH_ERR");
}
inline LangString langMonth(const QDate &date) {
QDate c(QDate::currentDate());
int32 month = date.month(), day = date.day(), year = date.year(), cyear = c.year(), cmonth = c.month();
if (year != cyear) {
if (year > cyear + 1 || cyear > year + 1 || (year == cyear + 1 && month + 12 > cmonth + 3) || (cyear == year + 1 && cmonth + 12 > month + 3)) {
return (month > 0 && month <= 12) ? lng_month_year(lt_month, lang(LangKey(lng_month1_small + month - 1)), lt_year, QString::number(year)) : qsl("MONTH_ERR");
}
}
return (month > 0 && month <= 12) ? lang(LangKey(lng_month1_small + month - 1)) : qsl("MONTH_ERR");
}
inline LangString langMonthFull(const QDate &date) {
QDate c(QDate::currentDate());
int32 month = date.month(), day = date.day(), year = date.year(), cyear = c.year(), cmonth = c.month();
if (year != cyear) {
if (year > cyear + 1 || cyear > year + 1 || (year == cyear + 1 && month + 12 > cmonth + 3) || (cyear == year + 1 && cmonth + 12 > month + 3)) {
return (month > 0 && month <= 12) ? lng_month_year(lt_month, lang(LangKey(lng_month1 + month - 1)), lt_year, QString::number(year)) : qsl("MONTH_ERR");
}
}
return (month > 0 && month <= 12) ? lang(LangKey(lng_month1 + month - 1)) : qsl("MONTH_ERR");
}
inline LangString langDayOfWeek(const QDate &date) {
int32 day = date.dayOfWeek();
return (day > 0 && day <= 7) ? lang(LangKey(lng_weekday1 + day - 1)) : qsl("DAY_ERR");
}
inline LangString langDayOfWeekFull(const QDate &date) {
int32 day = date.dayOfWeek();
return (day > 0 && day <= 7) ? lang(LangKey(lng_weekday1_full + day - 1)) : qsl("DAY_ERR");
}
inline LangString langDateTime(const QDateTime &date) {
return lng_mediaview_date_time(lt_date, langDayOfMonth(date.date()), lt_time, date.time().toString(cTimeFormat()));
}
inline LangString langDateTimeFull(const QDateTime &date) {
return lng_mediaview_date_time(lt_date, langDayOfMonthFull(date.date()), lt_time, date.time().toString(cTimeFormat()));
}
QString langNewVersionText();
QString langNewVersionTextForLang(int langId);
class LangLoader {
public:
const QString &errors() const;
const QString &warnings() const;
protected:
LangLoader() : _checked(false) {
memset(_found, 0, sizeof(_found));
}
ushort tagIndex(const QByteArray &tag) const;
LangKey keyIndex(const QByteArray &key) const;
bool tagReplaced(LangKey key, ushort tag) const;
LangKey subkeyIndex(LangKey key, ushort tag, ushort index) const;
bool feedKeyValue(LangKey key, const QString &value);
void foundKeyValue(LangKey key);
void error(const QString &text) {
_err.push_back(text);
}
void warning(const QString &text) {
_warn.push_back(text);
}
private:
mutable QStringList _err, _warn;
mutable QString _errors, _warnings;
mutable bool _checked;
bool _found[lngkeys_cnt];
LangLoader(const LangLoader &);
LangLoader &operator=(const LangLoader &);
};
class Translator : public QTranslator {
public:
QString translate(const char *context, const char *sourceText, const char *disambiguation = 0, int n = -1) const override;
};
inline bool langFirstNameGoesSecond() {
QString fullname = lang(lng_full_name__tagged);
for (const QChar *s = fullname.constData(), *ch = s, *e = ch + fullname.size(); ch != e;) {
if (*ch == TextCommand) {
if (ch + 3 < e && (ch + 1)->unicode() == TextCommandLangTag && *(ch + 3) == TextCommand) {
if ((ch + 2)->unicode() == 0x0020 + lt_last_name) {
return true;
} else if ((ch + 2)->unicode() == 0x0020 + lt_first_name) {
break;
}
}
}
}
return false;
}