OLD | NEW |
1 // Copyright 2014 The Chromium Authors. All rights reserved. | 1 // Copyright 2014 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 #include "components/translate/core/language_detection/language_detection_util.h
" | 5 #include "components/translate/core/language_detection/language_detection_util.h
" |
6 | 6 |
7 #include "base/logging.h" | 7 #include "base/logging.h" |
8 #include "base/metrics/field_trial.h" | 8 #include "base/metrics/field_trial.h" |
9 #include "base/strings/string_split.h" | 9 #include "base/strings/string_split.h" |
10 #include "base/strings/string_util.h" | 10 #include "base/strings/string_util.h" |
(...skipping 274 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
285 | 285 |
286 // An underscore instead of a dash is a frequent mistake. | 286 // An underscore instead of a dash is a frequent mistake. |
287 size_t underscore_index = code->find('_'); | 287 size_t underscore_index = code->find('_'); |
288 if (underscore_index != std::string::npos) | 288 if (underscore_index != std::string::npos) |
289 (*code)[underscore_index] = '-'; | 289 (*code)[underscore_index] = '-'; |
290 | 290 |
291 // Change everything up to a dash to lower-case and everything after to upper. | 291 // Change everything up to a dash to lower-case and everything after to upper. |
292 size_t dash_index = code->find('-'); | 292 size_t dash_index = code->find('-'); |
293 if (dash_index != std::string::npos) { | 293 if (dash_index != std::string::npos) { |
294 *code = base::StringToLowerASCII(code->substr(0, dash_index)) + | 294 *code = base::StringToLowerASCII(code->substr(0, dash_index)) + |
295 StringToUpperASCII(code->substr(dash_index)); | 295 base::StringToUpperASCII(code->substr(dash_index)); |
296 } else { | 296 } else { |
297 *code = base::StringToLowerASCII(*code); | 297 *code = base::StringToLowerASCII(*code); |
298 } | 298 } |
299 } | 299 } |
300 | 300 |
301 bool IsValidLanguageCode(const std::string& code) { | 301 bool IsValidLanguageCode(const std::string& code) { |
302 // Roughly check if the language code follows /[a-zA-Z]{2,3}(-[a-zA-Z]{2})?/. | 302 // Roughly check if the language code follows /[a-zA-Z]{2,3}(-[a-zA-Z]{2})?/. |
303 // TODO(hajimehoshi): How about es-419, which is used as an Accept language? | 303 // TODO(hajimehoshi): How about es-419, which is used as an Accept language? |
304 std::vector<std::string> chunks; | 304 std::vector<std::string> chunks; |
305 base::SplitString(code, '-', &chunks); | 305 base::SplitString(code, '-', &chunks); |
(...skipping 74 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
380 // distinguish from English, and the language is one of well-known languages | 380 // distinguish from English, and the language is one of well-known languages |
381 // which often provide "en-*" meta information mistakenly. | 381 // which often provide "en-*" meta information mistakenly. |
382 for (size_t i = 0; i < arraysize(kWellKnownCodesOnWrongConfiguration); ++i) { | 382 for (size_t i = 0; i < arraysize(kWellKnownCodesOnWrongConfiguration); ++i) { |
383 if (cld_language == kWellKnownCodesOnWrongConfiguration[i]) | 383 if (cld_language == kWellKnownCodesOnWrongConfiguration[i]) |
384 return true; | 384 return true; |
385 } | 385 } |
386 return false; | 386 return false; |
387 } | 387 } |
388 | 388 |
389 } // namespace translate | 389 } // namespace translate |
OLD | NEW |