Pin Chrome's shortcut to the Win10 Start menu on install and OS upgrade.
[chromium-blink-merge.git] / chrome / installer / util / language_selector.cc
bloba29ec1ff62dbfe437ba0f5dbeca677d12c03801d
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
4 //
5 // This file defines a helper class for selecting a supported language from a
6 // set of candidates.
8 #include "chrome/installer/util/language_selector.h"
10 #include <algorithm>
11 #include <functional>
13 #include "base/logging.h"
14 #include "base/strings/string16.h"
15 #include "base/strings/string_util.h"
16 #include "base/win/i18n.h"
17 #include "chrome/installer/util/google_update_settings.h"
18 #include "chrome/installer/util/installer_util_strings.h"
20 namespace {
22 struct LangToOffset {
23 const wchar_t* language;
24 int offset;
27 // The language we fall back upon when all else fails.
28 const wchar_t kFallbackLanguage[] = L"en-us";
29 const int kFallbackLanguageOffset = IDS_L10N_OFFSET_EN_US;
31 // A sorted array of language identifiers (and their offsets) for which
32 // translations are available. The contents of the array are generated by
33 // create_string_rc.py.
34 const LangToOffset kLanguageOffsetPairs[] = {
35 #define HANDLE_LANGUAGE(l_, o_) { L ## #l_, o_ },
36 DO_LANGUAGES
37 #undef HANDLE_LANGUAGE
40 // A sorted array of language identifiers that are aliases to other languages
41 // for which translations are available.
42 const LangToOffset kLanguageToOffsetExceptions[] = {
43 // Alias some English variants to British English (all others wildcard to US).
44 { L"en-au", IDS_L10N_OFFSET_EN_GB },
45 { L"en-ca", IDS_L10N_OFFSET_EN_GB },
46 { L"en-nz", IDS_L10N_OFFSET_EN_GB },
47 { L"en-za", IDS_L10N_OFFSET_EN_GB },
48 // Alias es-es to es (all others wildcard to es-419).
49 { L"es-es", IDS_L10N_OFFSET_ES },
50 // Google web properties use iw for he. Handle both just to be safe.
51 { L"he", IDS_L10N_OFFSET_IW },
52 // Google web properties use no for nb. Handle both just to be safe.
53 { L"nb", IDS_L10N_OFFSET_NO },
54 // Some Google web properties use tl for fil. Handle both just to be safe.
55 // They're not completely identical, but alias it here.
56 { L"tl", IDS_L10N_OFFSET_FIL },
57 // Pre-Vista aliases for Chinese w/ script subtag.
58 { L"zh-chs", IDS_L10N_OFFSET_ZH_CN },
59 { L"zh-cht", IDS_L10N_OFFSET_ZH_TW },
60 // Vista+ aliases for Chinese w/ script subtag.
61 { L"zh-hans", IDS_L10N_OFFSET_ZH_CN },
62 { L"zh-hant", IDS_L10N_OFFSET_ZH_TW },
63 // Alias Hong Kong and Macau to Taiwan.
64 { L"zh-hk", IDS_L10N_OFFSET_ZH_TW },
65 { L"zh-mo", IDS_L10N_OFFSET_ZH_TW },
66 // Although the wildcard entry for zh would result in this, alias zh-sg so
67 // that it will win if it precedes another valid tag in a list of candidates.
68 { L"zh-sg", IDS_L10N_OFFSET_ZH_CN }
71 // A sorted array of neutral language identifiers that are wildcard aliases to
72 // other languages for which translations are available.
73 const LangToOffset kLanguageToOffsetWildcards[] = {
74 // Use the U.S. region for anything English.
75 { L"en", IDS_L10N_OFFSET_EN_US },
76 // Use the Latin American region for anything Spanish.
77 { L"es", IDS_L10N_OFFSET_ES_419 },
78 // Use the Brazil region for anything Portugese.
79 { L"pt", IDS_L10N_OFFSET_PT_BR },
80 // Use the P.R.C. region for anything Chinese.
81 { L"zh", IDS_L10N_OFFSET_ZH_CN }
84 #if !defined(NDEBUG)
85 // Returns true if the items in the given range are sorted. If
86 // |byNameAndOffset| is true, the items must be sorted by both name and offset.
87 bool IsArraySorted(const LangToOffset* first, const LangToOffset* last,
88 bool byNameAndOffset) {
89 if (last - first > 1) {
90 for (--last; first != last; ++first) {
91 if (!(std::wstring(first->language) < (first + 1)->language) ||
92 (byNameAndOffset && !(first->offset < (first + 1)->offset))) {
93 return false;
97 return true;
100 // Validates that the static read-only mappings are properly sorted.
101 void ValidateMappings() {
102 // Ensure that kLanguageOffsetPairs is sorted.
103 DCHECK(IsArraySorted(&kLanguageOffsetPairs[0],
104 &kLanguageOffsetPairs[arraysize(kLanguageOffsetPairs)],
105 true)) << "kOffsetToLanguageId is not sorted";
107 // Ensure that kLanguageToOffsetExceptions is sorted.
108 DCHECK(IsArraySorted(
109 &kLanguageToOffsetExceptions[0],
110 &kLanguageToOffsetExceptions[arraysize(kLanguageToOffsetExceptions)],
111 false)) << "kLanguageToOffsetExceptions is not sorted";
113 // Ensure that kLanguageToOffsetWildcards is sorted.
114 DCHECK(IsArraySorted(
115 &kLanguageToOffsetWildcards[0],
116 &kLanguageToOffsetWildcards[arraysize(kLanguageToOffsetWildcards)],
117 false)) << "kLanguageToOffsetWildcards is not sorted";
119 #endif // !defined(NDEBUG)
121 // A less-than overload to do slightly more efficient searches in the
122 // sorted arrays.
123 bool operator<(const LangToOffset& left, const std::wstring& right) {
124 return left.language < right;
127 // A less-than overload to do slightly more efficient searches in the
128 // sorted arrays.
129 bool operator<(const std::wstring& left, const LangToOffset& right) {
130 return left < right.language;
133 // A not-so-efficient less-than overload for the same uses as above.
134 bool operator<(const LangToOffset& left, const LangToOffset& right) {
135 return std::wstring(left.language) < right.language;
138 // A compare function for searching in a sorted array by offset.
139 bool IsOffsetLessThan(const LangToOffset& left, const LangToOffset& right) {
140 return left.offset < right.offset;
143 // Binary search in one of the sorted arrays to find the offset corresponding to
144 // a given language |name|.
145 bool TryFindOffset(const LangToOffset* first, const LangToOffset* last,
146 const std::wstring& name, int* offset) {
147 const LangToOffset* search_result = std::lower_bound(first, last, name);
148 if (last != search_result && search_result->language == name) {
149 *offset = search_result->offset;
150 return true;
152 return false;
155 // A predicate function for LanguageSelector::SelectIf that searches for the
156 // offset of a translated language. The search first tries to find an exact
157 // match. Failing that, an exact match with an alias is attempted.
158 bool GetLanguageOffset(const std::wstring& language, int* offset) {
159 // Note: always perform the exact match first so that an alias is never
160 // selected in place of a future translation.
161 return
162 TryFindOffset(
163 &kLanguageOffsetPairs[0],
164 &kLanguageOffsetPairs[arraysize(kLanguageOffsetPairs)],
165 language, offset) ||
166 TryFindOffset(
167 &kLanguageToOffsetExceptions[0],
168 &kLanguageToOffsetExceptions[arraysize(kLanguageToOffsetExceptions)],
169 language, offset);
172 // A predicate function for LanguageSelector::SelectIf that searches for a
173 // wildcard match with |language|'s primary language subtag.
174 bool MatchLanguageOffset(const std::wstring& language, int* offset) {
175 std::wstring primary_language = language.substr(0, language.find(L'-'));
177 // Now check for wildcards.
178 return
179 TryFindOffset(
180 &kLanguageToOffsetWildcards[0],
181 &kLanguageToOffsetWildcards[arraysize(kLanguageToOffsetWildcards)],
182 primary_language, offset);
185 // Adds to |candidates| the eligible languages on the system. Any language
186 // setting specified by Omaha takes precedence over the operating system's
187 // configured languages.
188 void GetCandidatesFromSystem(std::vector<std::wstring>* candidates) {
189 DCHECK(candidates);
190 base::string16 language;
192 // Omaha gets first pick.
193 GoogleUpdateSettings::GetLanguage(&language);
194 if (!language.empty()) {
195 candidates->push_back(language);
198 // Now try the Windows UI languages. Use the thread preferred since that will
199 // kindly return us a list of all kinds of fallbacks.
200 base::win::i18n::GetThreadPreferredUILanguageList(candidates);
203 } // namespace
205 namespace installer {
207 LanguageSelector::LanguageSelector()
208 : offset_(arraysize(kLanguageOffsetPairs)) {
209 #if !defined(NDEBUG)
210 ValidateMappings();
211 #endif // !defined(NDEBUG)
212 std::vector<std::wstring> candidates;
214 GetCandidatesFromSystem(&candidates);
215 DoSelect(candidates);
218 LanguageSelector::LanguageSelector(const std::vector<std::wstring>& candidates)
219 : offset_(arraysize(kLanguageOffsetPairs)) {
220 #if !defined(NDEBUG)
221 ValidateMappings();
222 #endif // !defined(NDEBUG)
223 DoSelect(candidates);
226 LanguageSelector::~LanguageSelector() {
229 // static
230 std::wstring LanguageSelector::GetLanguageName(int offset) {
231 DCHECK_GE(offset, 0);
232 DCHECK_LT(static_cast<size_t>(offset), arraysize(kLanguageOffsetPairs));
234 LangToOffset value = { NULL, offset };
235 const LangToOffset* search_result =
236 std::lower_bound(&kLanguageOffsetPairs[0],
237 &kLanguageOffsetPairs[arraysize(kLanguageOffsetPairs)],
238 value, IsOffsetLessThan);
239 if (&kLanguageOffsetPairs[arraysize(kLanguageOffsetPairs)] != search_result &&
240 search_result->offset == offset) {
241 return search_result->language;
243 NOTREACHED() << "Unknown language offset.";
244 return std::wstring(&kFallbackLanguage[0], arraysize(kFallbackLanguage) - 1);
247 // Runs through the set of candidates, sending their downcased representation
248 // through |select_predicate|. Returns true if the predicate selects a
249 // candidate, in which case |matched_name| is assigned the value of the
250 // candidate and |matched_offset| is assigned the language offset of the
251 // selected translation.
252 // static
253 bool LanguageSelector::SelectIf(const std::vector<std::wstring>& candidates,
254 SelectPred_Fn select_predicate,
255 std::wstring* matched_name,
256 int* matched_offset) {
257 std::wstring candidate;
258 for (std::vector<std::wstring>::const_iterator scan = candidates.begin(),
259 end = candidates.end(); scan != end; ++scan) {
260 candidate.assign(*scan);
261 base::StringToLowerASCII(&candidate);
262 if (select_predicate(candidate, matched_offset)) {
263 matched_name->assign(*scan);
264 return true;
268 return false;
271 // Select the best-fit translation from the ordered list |candidates|.
272 // At the conclusion, this instance's |matched_candidate_| and |offset_| members
273 // are set to the name of the selected candidate and the offset of the matched
274 // translation. If no translation is selected, the fallback's name and offset
275 // are selected.
276 void LanguageSelector::DoSelect(const std::vector<std::wstring>& candidates) {
277 // Make a pass through the candidates looking for an exact or alias match.
278 // Failing that, make another pass looking for a wildcard match.
279 if (!SelectIf(candidates, &GetLanguageOffset, &matched_candidate_,
280 &offset_) &&
281 !SelectIf(candidates, &MatchLanguageOffset, &matched_candidate_,
282 &offset_)) {
283 VLOG(1) << "No suitable language found for any candidates.";
285 // Our fallback is "en-us"
286 matched_candidate_.assign(&kFallbackLanguage[0],
287 arraysize(kFallbackLanguage) - 1);
288 offset_ = kFallbackLanguageOffset;
292 } // namespace installer