OLD | NEW |
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 // | 4 // |
5 // This file defines a helper class for selecting a supported language from a | 5 // This file defines a helper class for selecting a supported language from a |
6 // set of candidates. | 6 // set of candidates. |
7 | 7 |
8 #include "chrome/installer/util/language_selector.h" | 8 #include "chrome/installer/util/language_selector.h" |
9 | 9 |
10 #include <algorithm> | 10 #include <algorithm> |
(...skipping 225 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
236 // Runs through the set of candidates, sending their downcased representation | 236 // Runs through the set of candidates, sending their downcased representation |
237 // through |select_predicate|. Returns true if the predicate selects a | 237 // through |select_predicate|. Returns true if the predicate selects a |
238 // candidate, in which case |matched_name| is assigned the value of the | 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 | 239 // candidate and |matched_offset| is assigned the language offset of the |
240 // selected translation. | 240 // selected translation. |
241 // static | 241 // static |
242 bool LanguageSelector::SelectIf(const std::vector<std::wstring>& candidates, | 242 bool LanguageSelector::SelectIf(const std::vector<std::wstring>& candidates, |
243 SelectPred_Fn select_predicate, | 243 SelectPred_Fn select_predicate, |
244 std::wstring* matched_name, | 244 std::wstring* matched_name, |
245 int* matched_offset) { | 245 int* matched_offset) { |
246 std::wstring candidate; | 246 for (const std::wstring& scan : candidates) { |
247 for (std::vector<std::wstring>::const_iterator scan = candidates.begin(), | 247 if (select_predicate(base::ToLowerASCII(scan), matched_offset)) { |
248 end = candidates.end(); scan != end; ++scan) { | 248 matched_name->assign(scan); |
249 candidate.assign(*scan); | |
250 base::StringToLowerASCII(&candidate); | |
251 if (select_predicate(candidate, matched_offset)) { | |
252 matched_name->assign(*scan); | |
253 return true; | 249 return true; |
254 } | 250 } |
255 } | 251 } |
256 | 252 |
257 return false; | 253 return false; |
258 } | 254 } |
259 | 255 |
260 // Select the best-fit translation from the ordered list |candidates|. | 256 // Select the best-fit translation from the ordered list |candidates|. |
261 // At the conclusion, this instance's |matched_candidate_| and |offset_| members | 257 // At the conclusion, this instance's |matched_candidate_| and |offset_| members |
262 // are set to the name of the selected candidate and the offset of the matched | 258 // are set to the name of the selected candidate and the offset of the matched |
263 // translation. If no translation is selected, the fallback's name and offset | 259 // translation. If no translation is selected, the fallback's name and offset |
264 // are selected. | 260 // are selected. |
265 void LanguageSelector::DoSelect(const std::vector<std::wstring>& candidates) { | 261 void LanguageSelector::DoSelect(const std::vector<std::wstring>& candidates) { |
266 // Make a pass through the candidates looking for an exact or alias match. | 262 // Make a pass through the candidates looking for an exact or alias match. |
267 // Failing that, make another pass looking for a wildcard match. | 263 // Failing that, make another pass looking for a wildcard match. |
268 if (!SelectIf(candidates, &GetLanguageOffset, &matched_candidate_, | 264 if (!SelectIf(candidates, &GetLanguageOffset, &matched_candidate_, |
269 &offset_) && | 265 &offset_) && |
270 !SelectIf(candidates, &MatchLanguageOffset, &matched_candidate_, | 266 !SelectIf(candidates, &MatchLanguageOffset, &matched_candidate_, |
271 &offset_)) { | 267 &offset_)) { |
272 VLOG(1) << "No suitable language found for any candidates."; | 268 VLOG(1) << "No suitable language found for any candidates."; |
273 | 269 |
274 // Our fallback is "en-us" | 270 // Our fallback is "en-us" |
275 matched_candidate_.assign(&kFallbackLanguage[0], | 271 matched_candidate_.assign(&kFallbackLanguage[0], |
276 arraysize(kFallbackLanguage) - 1); | 272 arraysize(kFallbackLanguage) - 1); |
277 offset_ = kFallbackLanguageOffset; | 273 offset_ = kFallbackLanguageOffset; |
278 } | 274 } |
279 } | 275 } |
280 | 276 |
281 } // namespace installer | 277 } // namespace installer |
OLD | NEW |