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
|
#include "coding/multilang_utf8_string.hpp"
#include "defines.hpp"
namespace
{
// TODO(AlexZ): Review and replace invalid languages which does not map correctly to
// iOS/Android locales/UI by valid and more popular ones.
// Languages below were choosen after sorting name:<lang> tags in 2011.
// Note, that it's not feasible to increase languages number here due to
// our current encoding (6 bit to store language code).
StringUtf8Multilang::Languages const g_languages = {{ {"default", "Native for each country"},
{"en", "English"}, {"ja", "日本語"}, {"fr", "Français"}, {"ko_rm", "Korean (Romanized)"},
{"ar", "العربية"}, {"de", "Deutsch"}, {"int_name", "International (Latin)"}, {"ru", "Русский"},
{"sv", "Svenska"}, {"zh", "中文"}, {"fi", "Suomi"}, {"be", "Беларуская"}, {"ka", "ქართული"},
{"ko", "한국어"}, {"he", "עברית"}, {"nl", "Nederlands"}, {"ga", "Gaeilge"},
{"ja_rm", "Japanese (Romanized)"}, {"el", "Ελληνικά"}, {"it", "Italiano"}, {"es", "Español"},
{"zh_pinyin", "Chinese (Pinyin)"}, {"th", "ไทย"}, {"cy", "Cymraeg"}, {"sr", "Српски"},
{"uk", "Українська"}, {"ca", "Català"}, {"hu", "Magyar"}, {"hsb", "Hornjoserbšćina"}, {"eu", "Euskara"},
{"fa", "فارسی"}, {"br", "Breton"}, {"pl", "Polski"}, {"hy", "Հայերէն"}, {"kn", "ಕನ್ನಡ"},
{"sl", "Slovenščina"}, {"ro", "Română"}, {"sq", "Shqipe"}, {"am", "አማርኛ"}, {"fy", "Frysk"},
{"cs", "Čeština"}, {"gd", "Gàidhlig"}, {"sk", "Slovenčina"}, {"af", "Afrikaans"},
{"ja_kana", "日本語(カタカナ)"}, {"lb", "Luxembourgish"}, {"pt", "Português"}, {"hr", "Hrvatski"},
{"fur", "Friulian"}, {"vi", "Tiếng Việt"}, {"tr", "Türkçe"}, {"bg", "Български"},
{"eo", "Esperanto"}, {"lt", "Lietuvių"}, {"la", "Latin"}, {"kk", "Қазақ"},
{"gsw", "Schwiizertüütsch"}, {"et", "Eesti"}, {"ku", "Kurdish"}, {"mn", "Mongolian"},
{"mk", "Македонски"}, {"lv", "Latviešu"}, {"hi", "हिन्दी"}
}};
static_assert(g_languages.size() == StringUtf8Multilang::kMaxSupportedLanguages,
"With current encoding we are limited to 64 languages max.");
} // namespace
int8_t constexpr StringUtf8Multilang::kUnsupportedLanguageCode;
int8_t constexpr StringUtf8Multilang::kDefaultCode;
int8_t constexpr StringUtf8Multilang::kEnglishCode;
int8_t constexpr StringUtf8Multilang::kInternationalCode;
// static
StringUtf8Multilang::Languages const & StringUtf8Multilang::GetSupportedLanguages()
{
// Asserts for generic class constants.
ASSERT_EQUAL(g_languages[kDefaultCode].m_code, string("default"), ());
ASSERT_EQUAL(g_languages[kInternationalCode].m_code, string("int_name"), ());
return g_languages;
}
// static
int8_t StringUtf8Multilang::GetLangIndex(string const & lang)
{
for (size_t i = 0; i < g_languages.size(); ++i)
if (lang == g_languages[i].m_code)
return static_cast<int8_t>(i);
return kUnsupportedLanguageCode;
}
// static
char const * StringUtf8Multilang::GetLangByCode(int8_t langCode)
{
if (langCode < 0 || langCode >= static_cast<int8_t>(g_languages.size()))
return "";
return g_languages[langCode].m_code;
}
// static
char const * StringUtf8Multilang::GetLangNameByCode(int8_t langCode)
{
if (langCode < 0 || langCode >= static_cast<int8_t>(g_languages.size()))
return "";
return g_languages[langCode].m_name;
}
size_t StringUtf8Multilang::GetNextIndex(size_t i) const
{
++i;
size_t const sz = m_s.size();
while (i < sz && (m_s[i] & 0xC0) != 0x80)
{
if ((m_s[i] & 0x80) == 0)
i += 1;
else if ((m_s[i] & 0xFE) == 0xFE)
i += 7;
else if ((m_s[i] & 0xFC) == 0xFC)
i += 6;
else if ((m_s[i] & 0xF8) == 0xF8)
i += 5;
else if ((m_s[i] & 0xF0) == 0xF0)
i += 4;
else if ((m_s[i] & 0xE0) == 0xE0)
i += 3;
else if ((m_s[i] & 0xC0) == 0xC0)
i += 2;
}
return i;
}
void StringUtf8Multilang::AddString(int8_t lang, string const & utf8s)
{
size_t i = 0;
size_t const sz = m_s.size();
while (i < sz)
{
size_t const next = GetNextIndex(i);
if ((m_s[i] & 0x3F) == lang)
{
++i;
m_s.replace(i, next - i, utf8s);
return;
}
i = next;
}
m_s.push_back(lang | 0x80);
m_s.insert(m_s.end(), utf8s.begin(), utf8s.end());
}
bool StringUtf8Multilang::GetString(int8_t lang, string & utf8s) const
{
size_t i = 0;
size_t const sz = m_s.size();
while (i < sz)
{
size_t const next = GetNextIndex(i);
if ((m_s[i] & 0x3F) == lang)
{
++i;
utf8s.assign(m_s.c_str() + i, next - i);
return true;
}
i = next;
}
return false;
}
bool StringUtf8Multilang::HasString(int8_t lang) const
{
for(size_t i = 0; i < m_s.size(); i = GetNextIndex(i))
{
if ((m_s[i] & 0x3F) == lang)
return true;
}
return false;
}
namespace
{
struct Printer
{
string & m_out;
Printer(string & out) : m_out(out) {}
bool operator()(int8_t code, string const & name) const
{
m_out += string(StringUtf8Multilang::GetLangByCode(code)) + string(":") + name + " ";
return true;
}
};
struct Finder
{
string const & m_s;
int8_t m_res;
Finder(string const & s) : m_s(s), m_res(-1) {}
bool operator()(int8_t code, string const & name)
{
if (name == m_s)
{
m_res = code;
return false;
}
return true;
}
};
} // namespace
int8_t StringUtf8Multilang::FindString(string const & utf8s) const
{
Finder finder(utf8s);
ForEach(finder);
return finder.m_res;
}
string DebugPrint(StringUtf8Multilang const & s)
{
string out;
s.ForEach(Printer(out));
return out;
}
|