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 180 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
191 | 191 |
192 // Checks if CLD can complement a sub code when the page language doesn't know | 192 // Checks if CLD can complement a sub code when the page language doesn't know |
193 // the sub code. | 193 // the sub code. |
194 bool CanCLDComplementSubCode( | 194 bool CanCLDComplementSubCode( |
195 const std::string& page_language, const std::string& cld_language) { | 195 const std::string& page_language, const std::string& cld_language) { |
196 // Translate server cannot treat general Chinese. If Content-Language and | 196 // Translate server cannot treat general Chinese. If Content-Language and |
197 // CLD agree that the language is Chinese and Content-Language doesn't know | 197 // CLD agree that the language is Chinese and Content-Language doesn't know |
198 // which dialect is used, CLD language has priority. | 198 // which dialect is used, CLD language has priority. |
199 // TODO(hajimehoshi): How about the other dialects like zh-MO? | 199 // TODO(hajimehoshi): How about the other dialects like zh-MO? |
200 return page_language == "zh" && | 200 return page_language == "zh" && |
201 base::StartsWithASCII(cld_language, "zh-", false); | 201 base::StartsWith(cld_language, "zh-", |
| 202 base::CompareCase::INSENSITIVE_ASCII); |
202 } | 203 } |
203 | 204 |
204 } // namespace | 205 } // namespace |
205 | 206 |
206 namespace translate { | 207 namespace translate { |
207 | 208 |
208 std::string DeterminePageLanguage(const std::string& code, | 209 std::string DeterminePageLanguage(const std::string& code, |
209 const std::string& html_lang, | 210 const std::string& html_lang, |
210 const base::string16& contents, | 211 const base::string16& contents, |
211 std::string* cld_language_p, | 212 std::string* cld_language_p, |
(...skipping 162 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
374 bool match = page_code != 0 && | 375 bool match = page_code != 0 && |
375 page_code == GetSimilarLanguageGroupCode(cld_language); | 376 page_code == GetSimilarLanguageGroupCode(cld_language); |
376 | 377 |
377 translate::ReportSimilarLanguageMatch(match); | 378 translate::ReportSimilarLanguageMatch(match); |
378 return match; | 379 return match; |
379 } | 380 } |
380 | 381 |
381 bool MaybeServerWrongConfiguration(const std::string& page_language, | 382 bool MaybeServerWrongConfiguration(const std::string& page_language, |
382 const std::string& cld_language) { | 383 const std::string& cld_language) { |
383 // If |page_language| is not "en-*", respect it and just return false here. | 384 // If |page_language| is not "en-*", respect it and just return false here. |
384 if (!base::StartsWithASCII(page_language, "en", false)) | 385 if (!base::StartsWith(page_language, "en", |
| 386 base::CompareCase::INSENSITIVE_ASCII)) |
385 return false; | 387 return false; |
386 | 388 |
387 // A server provides a language meta information representing "en-*". But it | 389 // A server provides a language meta information representing "en-*". But it |
388 // might be just a default value due to missing user configuration. | 390 // might be just a default value due to missing user configuration. |
389 // Let's trust |cld_language| if the determined language is not difficult to | 391 // Let's trust |cld_language| if the determined language is not difficult to |
390 // distinguish from English, and the language is one of well-known languages | 392 // distinguish from English, and the language is one of well-known languages |
391 // which often provide "en-*" meta information mistakenly. | 393 // which often provide "en-*" meta information mistakenly. |
392 for (size_t i = 0; i < arraysize(kWellKnownCodesOnWrongConfiguration); ++i) { | 394 for (size_t i = 0; i < arraysize(kWellKnownCodesOnWrongConfiguration); ++i) { |
393 if (cld_language == kWellKnownCodesOnWrongConfiguration[i]) | 395 if (cld_language == kWellKnownCodesOnWrongConfiguration[i]) |
394 return true; | 396 return true; |
395 } | 397 } |
396 return false; | 398 return false; |
397 } | 399 } |
398 | 400 |
399 } // namespace translate | 401 } // namespace translate |
OLD | NEW |