-
Notifications
You must be signed in to change notification settings - Fork 872
/
language_metrics.cc
262 lines (247 loc) · 7.88 KB
/
language_metrics.cc
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
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
// Copyright (c) 2024 The Brave Authors. All rights reserved.
// This Source Code Form is subject to the terms of the Mozilla Public
// License, v. 2.0. If a copy of the MPL was not distributed with this file,
// You can obtain one at https://mozilla.org/MPL/2.0/.
#include "brave/components/misc_metrics/language_metrics.h"
#include "base/containers/fixed_flat_set.h"
#include "base/functional/bind.h"
#include "base/metrics/histogram_macros.h"
#include "base/strings/string_split.h"
#include "components/language/core/browser/pref_names.h"
#include "components/language/core/common/locale_util.h"
#include "components/prefs/pref_service.h"
namespace misc_metrics {
struct LanguageSynonymPair {
const char* deprecated_code;
const char* active_code;
};
constexpr auto kOrderedLangCodes = base::MakeFixedFlatSet<std::string_view>(
base::sorted_unique,
{
"aa", // Afar
"ab", // Abkhazian
"ae", // Avestan
"af", // Afrikaans
"ak", // Akan
"am", // Amharic
"an", // Aragonese
"ar", // Arabic
"as", // Assamese
"av", // Avaric
"ay", // Aymara
"az", // Azerbaijani
"ba", // Bashkir
"be", // Belarusian
"bg", // Bulgarian
"bh", // Bihari languages
"bi", // Bislama
"bm", // Bambara
"bn", // Bengali
"bo", // Tibetan
"br", // Breton
"bs", // Bosnian
"ca", // Catalan; Valencian
"ce", // Chechen
"ch", // Chamorro
"co", // Corsican
"cr", // Cree
"cs", // Czech
"cu", // Church Slavic; Old Slavonic; Church Slavonic; Old Bulgarian;
// Old Church Slavonic
"cv", // Chuvash
"cy", // Welsh
"da", // Danish
"de", // German
"dv", // Divehi; Dhivehi; Maldivian
"dz", // Dzongkha
"ee", // Ewe
"el", // "Greek, Modern (1453-)"
"en", // English
"eo", // Esperanto
"es", // Spanish; Castilian
"et", // Estonian
"eu", // Basque
"fa", // Persian
"ff", // Fulah
"fi", // Finnish
"fj", // Fijian
"fo", // Faroese
"fr", // French
"fy", // Western Frisian
"ga", // Irish
"gd", // Gaelic; Scottish Gaelic
"gl", // Galician
"gn", // Guarani
"gu", // Gujarati
"gv", // Manx
"ha", // Hausa
"he", // Hebrew
"hi", // Hindi
"ho", // Hiri Motu
"hr", // Croatian
"ht", // Haitian; Haitian Creole
"hu", // Hungarian
"hy", // Armenian
"hz", // Herero
"ia", // Interlingua (International Auxiliary Language Association)
"id", // Indonesian
"ie", // Interlingue; Occidental
"ig", // Igbo
"ii", // Sichuan Yi; Nuosu
"ik", // Inupiaq
"io", // Ido
"is", // Icelandic
"it", // Italian
"iu", // Inuktitut
"ja", // Japanese
"jv", // Javanese
"ka", // Georgian
"kg", // Kongo
"ki", // Kikuyu; Gikuyu
"kj", // Kuanyama; Kwanyama
"kk", // Kazakh
"kl", // Kalaallisut; Greenlandic
"km", // Central Khmer
"kn", // Kannada
"ko", // Korean
"kr", // Kanuri
"ks", // Kashmiri
"ku", // Kurdish
"kv", // Komi
"kw", // Cornish
"ky", // Kirghiz; Kyrgyz
"la", // Latin
"lb", // Luxembourgish; Letzeburgesch
"lg", // Ganda
"li", // Limburgan; Limburger; Limburgish
"ln", // Lingala
"lo", // Lao
"lt", // Lithuanian
"lu", // Luba-Katanga
"lv", // Latvian
"mg", // Malagasy
"mh", // Marshallese
"mi", // Maori
"mk", // Macedonian
"ml", // Malayalam
"mn", // Mongolian
"mr", // Marathi
"ms", // Malay
"mt", // Maltese
"my", // Burmese
"na", // Nauru
"nb", // "Bokmål, Norwegian; Norwegian Bokmål"
"nd", // "Ndebele, North; North Ndebele"
"ne", // Nepali
"ng", // Ndonga
"nl", // Dutch; Flemish
"nn", // "Norwegian Nynorsk; Nynorsk, Norwegian"
"no", // Norwegian
"nr", // "Ndebele, South; South Ndebele"
"nv", // Navajo; Navaho
"ny", // Chichewa; Chewa; Nyanja
"oc", // Occitan (post 1500)
"oj", // Ojibwa
"om", // Oromo
"or", // Oriya
"os", // Ossetian; Ossetic
"pa", // Panjabi; Punjabi
"pi", // Pali
"pl", // Polish
"ps", // Pushto; Pashto
"pt", // Portuguese
"qu", // Quechua
"rm", // Romansh
"rn", // Rundi
"ro", // Romanian; Moldavian; Moldovan
"ru", // Russian
"rw", // Kinyarwanda
"sa", // Sanskrit
"sc", // Sardinian
"sd", // Sindhi
"se", // Northern Sami
"sg", // Sango
"si", // Sinhala; Sinhalese
"sk", // Slovak
"sl", // Slovenian
"sm", // Samoan
"sn", // Shona
"so", // Somali
"sq", // Albanian
"sr", // Serbian
"ss", // Swati
"st", // "Sotho, Southern"
"su", // Sundanese
"sv", // Swedish
"sw", // Swahili
"ta", // Tamil
"te", // Telugu
"tg", // Tajik
"th", // Thai
"ti", // Tigrinya
"tk", // Turkmen
"tl", // Tagalog
"tn", // Tswana
"to", // Tonga (Tonga Islands)
"tr", // Turkish
"ts", // Tsonga
"tt", // Tatar
"tw", // Twi
"ty", // Tahitian
"ug", // Uighur; Uyghur
"uk", // Ukrainian
"ur", // Urdu
"uz", // Uzbek
"ve", // Venda
"vi", // Vietnamese
"vo", // Volapük
"wa", // Walloon
"wo", // Wolof
"xh", // Xhosa
"yi", // Yiddish
"yo", // Yoruba
"za", // Zhuang; Chuang
"zh", // Chinese
"zu", // Zulu
});
constexpr LanguageSynonymPair kLanguageSynonyms[] = {
{"in", "id"}, {"iw", "he"}, {"ji", "yi"},
{"jw", "jv"}, {"mo", "ro"}, {"gsw", "de"}};
LanguageMetrics::LanguageMetrics(PrefService* profile_prefs)
: profile_prefs_(profile_prefs) {
pref_change_registrar_.Init(profile_prefs);
pref_change_registrar_.Add(
language::prefs::kAcceptLanguages,
base::BindRepeating(&LanguageMetrics::RecordLanguageMetric,
base::Unretained(this)));
RecordLanguageMetric();
}
LanguageMetrics::~LanguageMetrics() = default;
void LanguageMetrics::RecordLanguageMetric() {
auto languages = profile_prefs_->GetString(language::prefs::kAcceptLanguages);
auto languages_split = base::SplitString(
languages, ",", base::WhitespaceHandling::TRIM_WHITESPACE,
base::SplitResult::SPLIT_WANT_NONEMPTY);
if (languages_split.empty()) {
// Suspend metric if primary language not detected
UMA_HISTOGRAM_EXACT_LINEAR(kPrimaryLanguageHistogramName, INT_MAX - 1,
kOrderedLangCodes.size());
return;
}
auto primary_language = language::ExtractBaseLanguage(languages_split[0]);
for (const auto& synonym_pair : kLanguageSynonyms) {
if (primary_language == synonym_pair.deprecated_code) {
primary_language = synonym_pair.active_code;
}
}
// set answer to fallback which will suspend the metric
// if the language is not found in the list
int answer = INT_MAX - 1;
const auto language_it = kOrderedLangCodes.find(primary_language);
if (language_it != kOrderedLangCodes.end()) {
answer = std::distance(kOrderedLangCodes.begin(), language_it);
}
UMA_HISTOGRAM_EXACT_LINEAR(kPrimaryLanguageHistogramName, answer,
kOrderedLangCodes.size());
}
} // namespace misc_metrics