Index: chrome/browser/extensions/api/i18n/i18n_api.cc |
diff --git a/chrome/browser/extensions/api/i18n/i18n_api.cc b/chrome/browser/extensions/api/i18n/i18n_api.cc |
index ca40229ba1a03de856fde71207b23ff3386653e6..3083bb9c78a716c3446e06afded5266f6128dabc 100644 |
--- a/chrome/browser/extensions/api/i18n/i18n_api.cc |
+++ b/chrome/browser/extensions/api/i18n/i18n_api.cc |
@@ -16,6 +16,7 @@ |
#include "chrome/common/extensions/api/i18n.h" |
#include "chrome/common/pref_names.h" |
+ |
Takashi Toyoshima
2015/07/08 03:19:41
no empty line?
amalika
2015/07/08 21:58:03
Done.
|
namespace GetAcceptLanguages = extensions::api::i18n::GetAcceptLanguages; |
namespace extensions { |
@@ -59,4 +60,122 @@ bool I18nGetAcceptLanguagesFunction::RunSync() { |
return true; |
} |
+bool I18nDetectLanguageFunction::RunAsync() { |
+ scoped_ptr<api::i18n::DetectLanguage::Params> params( |
+ api::i18n::DetectLanguage::Params::Create(*args_)); |
+ EXTENSION_FUNCTION_VALIDATE(params.get()); |
+ |
+ std::string text = ""; |
+ |
+ if (!params->text.get()) { |
+ // text was not provided so return |
+ return false; |
+ } |
+ |
+ 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.
|
+ GetLanguage(text); |
+ |
+ return true; |
+} |
+ |
+void I18nDetectLanguageFunction::GetLanguage(const std::string& text) { |
+ // Sets up the input String |
+ const std::string utf8_text(text); |
+ const int num_utf8_bytes = static_cast<int>(utf8_text.size()); |
+ const char* raw_utf8_bytes = utf8_text.c_str(); |
+ |
+ // TODO(mcindy): improve this by providing better CLD hints |
+ 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.
|
+ int encoding_hint = CLD2::UNKNOWN_ENCODING; |
+ CLD2::Language language_hint = CLD2::UNKNOWN_LANGUAGE; |
+ CLD2::CLDHints cldhints = {NULL, tld_hint, encoding_hint, language_hint}; |
+ |
+ const bool is_plain_text = true; |
+ CLD2::Language language3[3]; |
+ int percent3[3]; |
+ int text_bytes; // amount of non-tag/letters-only text (assumed 0) |
+ bool is_reliable = false; |
+ |
+ int cld_language = 0; |
+ int flags = 0; |
+ int valid_prefix_bytes = false; |
+ double normalized_score3[3]; |
+ |
+ cld_language = CLD2::ExtDetectLanguageSummaryCheckUTF8( |
+ raw_utf8_bytes, |
+ num_utf8_bytes, |
+ is_plain_text, |
+ &cldhints, |
+ flags, |
+ language3, |
+ percent3, |
+ normalized_score3, |
+ NULL, // assumed no ResultChunkVector is used |
+ &text_bytes, |
+ &is_reliable, |
+ &valid_prefix_bytes); |
+ |
+ DetectedLanguage detected_languages[3]; |
+ InitDetectedLanguages(language3, percent3, detected_languages); |
+ |
+ SendLanguagesResult(detected_languages, is_reliable); |
+ |
+ 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.
|
+} |
+ |
+void I18nDetectLanguageFunction::SendLanguagesResult( |
+ DetectedLanguage *detected_languages, |
+ const bool is_reliable) { |
+ std::string ver = std::to_string(CLD_VERSION); |
+ |
+ base::DictionaryValue* result = new base::DictionaryValue(); |
+ result->Set("is_reliable", new base::FundamentalValue(is_reliable)); |
+ |
+ base::ListValue* languages = new base::ListValue(); |
+ // Set up three languages returned by CLD |
+ for (int i = 0; i <3; i++) { |
+ base::DictionaryValue* language = new base::DictionaryValue(); |
+ language->Set("language", |
+ new base::StringValue(detected_languages[i].language)); |
+ language->Set("percentage", |
+ new base::FundamentalValue(detected_languages[i].percentage)); |
+ languages->Set(i, language); |
+ } |
+ |
+ result->Set("languages", languages); |
+ |
+ |
+ SetResult(result); |
+ SendResponse(true); |
+ |
+ return; |
Takashi Toyoshima
2015/07/08 03:19:41
ditto
amalika
2015/07/08 21:58:02
Done.
|
+} |
+ |
+void I18nDetectLanguageFunction::InitDetectedLanguages( |
+ CLD2::Language *languages, |
+ int *percent3, |
+ DetectedLanguage detected_languages[3]) { |
+ for (int i = 0; i< 3; i++) { |
+ std::string language_code = ""; |
+ |
+ // Convert LanguageCode 'zh' to 'zh-CN' and 'zh-Hant' to 'zh-TW' for |
+ // Translare server usage. see DetermineTextLanguage in |
+ // components/translate/core/language_detection/language_detection_util.cc |
+ if (languages[i] == CLD2::CHINESE) { |
+ language_code = "zh-CN"; |
+ } else if (languages[i] == CLD2::CHINESE_T) { |
+ language_code = "zh-TW"; |
+ } else { |
+ language_code = |
+ CLD2::LanguageCode(static_cast<CLD2::Language>(languages[i])); |
+ } |
+ |
+ detected_languages[i].language = language_code; |
+ detected_languages[i].percentage = percent3[i]; |
+ } |
+ |
+ 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.
|
+} |
+ |
+ |
Takashi Toyoshima
2015/07/08 03:19:41
remove one of two empty lines?
amalika
2015/07/08 21:58:03
Done.
|
} // namespace extensions |