| 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/strings/string_split.h" | 8 #include "base/strings/string_split.h" |
| 9 #include "base/strings/string_util.h" | 9 #include "base/strings/string_util.h" |
| 10 #include "base/strings/utf_string_conversions.h" | 10 #include "base/strings/utf_string_conversions.h" |
| (...skipping 252 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 263 base::TrimWhitespaceASCII(*code, base::TRIM_ALL, code); | 263 base::TrimWhitespaceASCII(*code, base::TRIM_ALL, code); |
| 264 | 264 |
| 265 // An underscore instead of a dash is a frequent mistake. | 265 // An underscore instead of a dash is a frequent mistake. |
| 266 size_t underscore_index = code->find('_'); | 266 size_t underscore_index = code->find('_'); |
| 267 if (underscore_index != std::string::npos) | 267 if (underscore_index != std::string::npos) |
| 268 (*code)[underscore_index] = '-'; | 268 (*code)[underscore_index] = '-'; |
| 269 | 269 |
| 270 // Change everything up to a dash to lower-case and everything after to upper. | 270 // Change everything up to a dash to lower-case and everything after to upper. |
| 271 size_t dash_index = code->find('-'); | 271 size_t dash_index = code->find('-'); |
| 272 if (dash_index != std::string::npos) { | 272 if (dash_index != std::string::npos) { |
| 273 *code = base::StringToLowerASCII(code->substr(0, dash_index)) + | 273 *code = base::ToLowerASCII(code->substr(0, dash_index)) + |
| 274 base::StringToUpperASCII(code->substr(dash_index)); | 274 base::ToUpperASCII(code->substr(dash_index)); |
| 275 } else { | 275 } else { |
| 276 *code = base::StringToLowerASCII(*code); | 276 *code = base::ToLowerASCII(*code); |
| 277 } | 277 } |
| 278 } | 278 } |
| 279 | 279 |
| 280 bool IsValidLanguageCode(const std::string& code) { | 280 bool IsValidLanguageCode(const std::string& code) { |
| 281 // Roughly check if the language code follows /[a-zA-Z]{2,3}(-[a-zA-Z]{2})?/. | 281 // Roughly check if the language code follows /[a-zA-Z]{2,3}(-[a-zA-Z]{2})?/. |
| 282 // TODO(hajimehoshi): How about es-419, which is used as an Accept language? | 282 // TODO(hajimehoshi): How about es-419, which is used as an Accept language? |
| 283 std::vector<base::StringPiece> chunks = base::SplitStringPiece( | 283 std::vector<base::StringPiece> chunks = base::SplitStringPiece( |
| 284 code, "-", base::TRIM_WHITESPACE, base::SPLIT_WANT_ALL); | 284 code, "-", base::TRIM_WHITESPACE, base::SPLIT_WANT_ALL); |
| 285 | 285 |
| 286 if (chunks.size() < 1 || 2 < chunks.size()) | 286 if (chunks.size() < 1 || 2 < chunks.size()) |
| (...skipping 71 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 358 // distinguish from English, and the language is one of well-known languages | 358 // distinguish from English, and the language is one of well-known languages |
| 359 // which often provide "en-*" meta information mistakenly. | 359 // which often provide "en-*" meta information mistakenly. |
| 360 for (size_t i = 0; i < arraysize(kWellKnownCodesOnWrongConfiguration); ++i) { | 360 for (size_t i = 0; i < arraysize(kWellKnownCodesOnWrongConfiguration); ++i) { |
| 361 if (cld_language == kWellKnownCodesOnWrongConfiguration[i]) | 361 if (cld_language == kWellKnownCodesOnWrongConfiguration[i]) |
| 362 return true; | 362 return true; |
| 363 } | 363 } |
| 364 return false; | 364 return false; |
| 365 } | 365 } |
| 366 | 366 |
| 367 } // namespace translate | 367 } // namespace translate |
| OLD | NEW |