Chromium Code Reviews| 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 #include "chrome/browser/extensions/api/i18n/i18n_api.h" | 5 #include "chrome/browser/extensions/api/i18n/i18n_api.h" |
| 6 | 6 |
| 7 #include <algorithm> | 7 #include <algorithm> |
| 8 #include <string> | |
| 9 #include <vector> | 8 #include <vector> |
| 10 | 9 |
| 11 #include "base/lazy_instance.h" | 10 #include "base/lazy_instance.h" |
| 12 #include "base/prefs/pref_service.h" | 11 #include "base/prefs/pref_service.h" |
| 13 #include "base/strings/string_piece.h" | 12 #include "base/strings/string_piece.h" |
| 14 #include "base/strings/string_split.h" | 13 #include "base/strings/string_split.h" |
| 15 #include "chrome/browser/profiles/profile.h" | 14 #include "chrome/browser/profiles/profile.h" |
| 16 #include "chrome/common/extensions/api/i18n.h" | 15 #include "chrome/common/extensions/api/i18n.h" |
| 17 #include "chrome/common/pref_names.h" | 16 #include "chrome/common/pref_names.h" |
| 18 | 17 |
| (...skipping 33 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 52 | 51 |
| 53 if (languages.empty()) { | 52 if (languages.empty()) { |
| 54 error_ = kEmptyAcceptLanguagesError; | 53 error_ = kEmptyAcceptLanguagesError; |
| 55 return false; | 54 return false; |
| 56 } | 55 } |
| 57 | 56 |
| 58 results_ = GetAcceptLanguages::Results::Create(languages); | 57 results_ = GetAcceptLanguages::Results::Create(languages); |
| 59 return true; | 58 return true; |
| 60 } | 59 } |
| 61 | 60 |
| 61 bool I18nDetectLanguageFunction::RunAsync() { | |
| 62 scoped_ptr<api::i18n::DetectLanguage::Params> params( | |
| 63 api::i18n::DetectLanguage::Params::Create(*args_)); | |
| 64 EXTENSION_FUNCTION_VALIDATE(params.get()); | |
| 65 | |
| 66 if (!params->text.get()) { | |
| 67 // text was not provided so return | |
| 68 return false; | |
| 69 } | |
| 70 | |
| 71 GetLanguage(*params->text); | |
| 72 return true; | |
| 73 } | |
| 74 | |
| 75 void I18nDetectLanguageFunction::GetLanguage(const std::string& text) { | |
| 76 // Sets up the input String | |
| 77 const std::string utf8_text(text); | |
| 78 const int num_utf8_bytes = static_cast<int>(utf8_text.size()); | |
| 79 const char* raw_utf8_bytes = utf8_text.c_str(); | |
| 80 | |
| 81 // TODO(mcindy): improve this by providing better CLD hints | |
| 82 const char tld_hint[] = ""; // asummed no cld hints provided | |
| 83 int encoding_hint = CLD2::UNKNOWN_ENCODING; | |
| 84 CLD2::Language language_hint = CLD2::UNKNOWN_LANGUAGE; | |
| 85 CLD2::CLDHints cldhints = {NULL, tld_hint, encoding_hint, language_hint}; | |
| 86 | |
| 87 const bool is_plain_text = true; | |
| 88 CLD2::Language language3[3]; | |
| 89 int percent3[3]; | |
| 90 int text_bytes; // amount of non-tag/letters-only text (assumed 0) | |
| 91 bool is_reliable = false; | |
| 92 | |
| 93 int cld_language = 0; | |
| 94 int flags = 0; | |
| 95 int valid_prefix_bytes = false; | |
| 96 double normalized_score3[3]; | |
| 97 | |
| 98 cld_language = CLD2::ExtDetectLanguageSummaryCheckUTF8( | |
| 99 raw_utf8_bytes, | |
| 100 num_utf8_bytes, | |
| 101 is_plain_text, | |
| 102 &cldhints, | |
| 103 flags, | |
| 104 language3, | |
| 105 percent3, | |
| 106 normalized_score3, | |
| 107 NULL, // assumed no ResultChunkVector is used | |
| 108 &text_bytes, | |
| 109 &is_reliable, | |
| 110 &valid_prefix_bytes); | |
| 111 | |
| 112 DetectedLanguage detected_languages[3]; | |
| 113 InitDetectedLanguages(language3, percent3, detected_languages); | |
| 114 | |
| 115 SendLanguagesResult(detected_languages, is_reliable); | |
| 116 } | |
| 117 | |
| 118 void I18nDetectLanguageFunction::SendLanguagesResult( | |
| 119 DetectedLanguage* detected_languages, | |
| 120 const bool is_reliable) { | |
| 121 base::DictionaryValue* result = new base::DictionaryValue(); | |
| 122 result->Set("is_reliable", new base::FundamentalValue(is_reliable)); | |
| 123 | |
| 124 base::ListValue* languages = new base::ListValue(); | |
| 125 // Set up three languages returned by CLD | |
| 126 for (int i = 0; i <3; i++) { | |
| 127 base::DictionaryValue* language = new base::DictionaryValue(); | |
| 128 language->Set("language", | |
| 129 new base::StringValue(detected_languages[i].language)); | |
| 130 language->Set("percentage", | |
| 131 new base::FundamentalValue(detected_languages[i].percentage)); | |
| 132 languages->Set(i, language); | |
| 133 } | |
|
not at google - send to devlin
2015/07/08 19:34:53
The i18n API is compiled to a typed struct, see ou
amalika
2015/07/09 23:00:02
Done.
| |
| 134 | |
| 135 result->Set("languages", languages); | |
| 136 | |
| 137 SetResult(result); | |
| 138 SendResponse(true); | |
| 139 } | |
| 140 | |
| 141 void I18nDetectLanguageFunction::InitDetectedLanguages( | |
| 142 CLD2::Language* languages, | |
| 143 int* percent3, | |
| 144 DetectedLanguage detected_languages[3]) { | |
|
not at google - send to devlin
2015/07/08 19:34:53
Is there a way to implement this stuff without pas
amalika
2015/07/08 21:58:03
Done.
| |
| 145 for (int i = 0; i< 3; i++) { | |
| 146 std::string language_code = ""; | |
| 147 | |
| 148 // Convert LanguageCode 'zh' to 'zh-CN' and 'zh-Hant' to 'zh-TW' for | |
| 149 // Translate server usage. see DetermineTextLanguage in | |
| 150 // components/translate/core/language_detection/language_detection_util.cc | |
| 151 if (languages[i] == CLD2::CHINESE) { | |
| 152 language_code = "zh-CN"; | |
| 153 } else if (languages[i] == CLD2::CHINESE_T) { | |
| 154 language_code = "zh-TW"; | |
| 155 } else { | |
| 156 language_code = | |
| 157 CLD2::LanguageCode(static_cast<CLD2::Language>(languages[i])); | |
| 158 } | |
| 159 | |
| 160 detected_languages[i].language = language_code; | |
| 161 detected_languages[i].percentage = percent3[i]; | |
| 162 } | |
| 163 } | |
| 164 | |
| 62 } // namespace extensions | 165 } // namespace extensions |
| OLD | NEW |