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