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..f0e553d05f2ea60a0f6fe0394bbede121cefdb1d 100644 |
--- a/chrome/browser/extensions/api/i18n/i18n_api.cc |
+++ b/chrome/browser/extensions/api/i18n/i18n_api.cc |
@@ -5,7 +5,6 @@ |
#include "chrome/browser/extensions/api/i18n/i18n_api.h" |
#include <algorithm> |
-#include <string> |
#include <vector> |
#include "base/lazy_instance.h" |
@@ -59,4 +58,108 @@ 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()); |
+ |
+ if (!params->text.get()) { |
+ // text was not provided so return |
+ return false; |
+ } |
+ |
+ GetLanguage(*params->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 |
+ 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); |
+} |
+ |
+void I18nDetectLanguageFunction::SendLanguagesResult( |
+ DetectedLanguage* detected_languages, |
+ const bool is_reliable) { |
+ 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); |
+ } |
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.
|
+ |
+ result->Set("languages", languages); |
+ |
+ SetResult(result); |
+ SendResponse(true); |
+} |
+ |
+void I18nDetectLanguageFunction::InitDetectedLanguages( |
+ CLD2::Language* languages, |
+ int* percent3, |
+ 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.
|
+ for (int i = 0; i< 3; i++) { |
+ std::string language_code = ""; |
+ |
+ // Convert LanguageCode 'zh' to 'zh-CN' and 'zh-Hant' to 'zh-TW' for |
+ // Translate 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]; |
+ } |
+} |
+ |
} // namespace extensions |