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.
5 // This file defines a helper class for selecting a supported language from a
8 #include "chrome/installer/util/language_selector.h"
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"
23 const wchar_t* language
;
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_ },
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
}
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
))) {
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
123 bool operator<(const LangToOffset
& left
, const std::wstring
& right
) {
124 return left
.language
< right
;
127 // A compare function for searching in a sorted array by offset.
128 bool IsOffsetLessThan(const LangToOffset
& left
, const LangToOffset
& right
) {
129 return left
.offset
< right
.offset
;
132 // Binary search in one of the sorted arrays to find the offset corresponding to
133 // a given language |name|.
134 bool TryFindOffset(const LangToOffset
* first
, const LangToOffset
* last
,
135 const std::wstring
& name
, int* offset
) {
136 const LangToOffset
* search_result
= std::lower_bound(first
, last
, name
);
137 if (last
!= search_result
&& search_result
->language
== name
) {
138 *offset
= search_result
->offset
;
144 // A predicate function for LanguageSelector::SelectIf that searches for the
145 // offset of a translated language. The search first tries to find an exact
146 // match. Failing that, an exact match with an alias is attempted.
147 bool GetLanguageOffset(const std::wstring
& language
, int* offset
) {
148 // Note: always perform the exact match first so that an alias is never
149 // selected in place of a future translation.
152 &kLanguageOffsetPairs
[0],
153 &kLanguageOffsetPairs
[arraysize(kLanguageOffsetPairs
)],
156 &kLanguageToOffsetExceptions
[0],
157 &kLanguageToOffsetExceptions
[arraysize(kLanguageToOffsetExceptions
)],
161 // A predicate function for LanguageSelector::SelectIf that searches for a
162 // wildcard match with |language|'s primary language subtag.
163 bool MatchLanguageOffset(const std::wstring
& language
, int* offset
) {
164 std::wstring primary_language
= language
.substr(0, language
.find(L
'-'));
166 // Now check for wildcards.
169 &kLanguageToOffsetWildcards
[0],
170 &kLanguageToOffsetWildcards
[arraysize(kLanguageToOffsetWildcards
)],
171 primary_language
, offset
);
174 // Adds to |candidates| the eligible languages on the system. Any language
175 // setting specified by Omaha takes precedence over the operating system's
176 // configured languages.
177 void GetCandidatesFromSystem(std::vector
<std::wstring
>* candidates
) {
179 base::string16 language
;
181 // Omaha gets first pick.
182 GoogleUpdateSettings::GetLanguage(&language
);
183 if (!language
.empty()) {
184 candidates
->push_back(language
);
187 // Now try the Windows UI languages. Use the thread preferred since that will
188 // kindly return us a list of all kinds of fallbacks.
189 base::win::i18n::GetThreadPreferredUILanguageList(candidates
);
194 namespace installer
{
196 LanguageSelector::LanguageSelector()
197 : offset_(arraysize(kLanguageOffsetPairs
)) {
200 #endif // !defined(NDEBUG)
201 std::vector
<std::wstring
> candidates
;
203 GetCandidatesFromSystem(&candidates
);
204 DoSelect(candidates
);
207 LanguageSelector::LanguageSelector(const std::vector
<std::wstring
>& candidates
)
208 : offset_(arraysize(kLanguageOffsetPairs
)) {
211 #endif // !defined(NDEBUG)
212 DoSelect(candidates
);
215 LanguageSelector::~LanguageSelector() {
219 std::wstring
LanguageSelector::GetLanguageName(int offset
) {
220 DCHECK_GE(offset
, 0);
221 DCHECK_LT(static_cast<size_t>(offset
), arraysize(kLanguageOffsetPairs
));
223 LangToOffset value
= { NULL
, offset
};
224 const LangToOffset
* search_result
=
225 std::lower_bound(&kLanguageOffsetPairs
[0],
226 &kLanguageOffsetPairs
[arraysize(kLanguageOffsetPairs
)],
227 value
, IsOffsetLessThan
);
228 if (&kLanguageOffsetPairs
[arraysize(kLanguageOffsetPairs
)] != search_result
&&
229 search_result
->offset
== offset
) {
230 return search_result
->language
;
232 NOTREACHED() << "Unknown language offset.";
233 return std::wstring(&kFallbackLanguage
[0], arraysize(kFallbackLanguage
) - 1);
236 // Runs through the set of candidates, sending their downcased representation
237 // through |select_predicate|. Returns true if the predicate selects a
238 // candidate, in which case |matched_name| is assigned the value of the
239 // candidate and |matched_offset| is assigned the language offset of the
240 // selected translation.
242 bool LanguageSelector::SelectIf(const std::vector
<std::wstring
>& candidates
,
243 SelectPred_Fn select_predicate
,
244 std::wstring
* matched_name
,
245 int* matched_offset
) {
246 for (const std::wstring
& scan
: candidates
) {
247 if (select_predicate(base::ToLowerASCII(scan
), matched_offset
)) {
248 matched_name
->assign(scan
);
256 // Select the best-fit translation from the ordered list |candidates|.
257 // At the conclusion, this instance's |matched_candidate_| and |offset_| members
258 // are set to the name of the selected candidate and the offset of the matched
259 // translation. If no translation is selected, the fallback's name and offset
261 void LanguageSelector::DoSelect(const std::vector
<std::wstring
>& candidates
) {
262 // Make a pass through the candidates looking for an exact or alias match.
263 // Failing that, make another pass looking for a wildcard match.
264 if (!SelectIf(candidates
, &GetLanguageOffset
, &matched_candidate_
,
266 !SelectIf(candidates
, &MatchLanguageOffset
, &matched_candidate_
,
268 VLOG(1) << "No suitable language found for any candidates.";
270 // Our fallback is "en-us"
271 matched_candidate_
.assign(&kFallbackLanguage
[0],
272 arraysize(kFallbackLanguage
) - 1);
273 offset_
= kFallbackLanguageOffset
;
277 } // namespace installer