Chromium Code Reviews| 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..3a5b8dd39cca0a3b1070e62a26f414ad75fd5c1d 100644 |
| --- a/chrome/browser/extensions/api/i18n/i18n_api.cc |
| +++ b/chrome/browser/extensions/api/i18n/i18n_api.cc |
| @@ -5,16 +5,17 @@ |
| #include "chrome/browser/extensions/api/i18n/i18n_api.h" |
| #include <algorithm> |
| -#include <string> |
| #include <vector> |
| -#include "base/lazy_instance.h" |
| #include "base/prefs/pref_service.h" |
| #include "base/strings/string_piece.h" |
| #include "base/strings/string_split.h" |
| #include "chrome/browser/profiles/profile.h" |
| #include "chrome/common/extensions/api/i18n.h" |
| #include "chrome/common/pref_names.h" |
| +#include "tools/json_schema_compiler/util.h" |
| + |
| +#define CLD_NUM_OF_LANGS 3 |
|
not at google - send to devlin
2015/07/10 16:21:09
use "const int kCldNumLands = 3" not #define
amalika
2015/07/13 23:57:00
Done.
|
| namespace GetAcceptLanguages = extensions::api::i18n::GetAcceptLanguages; |
|
not at google - send to devlin
2015/07/10 16:21:09
It makes sense to move this namespace alias inside
amalika
2015/07/13 23:57:01
Done.
|
| @@ -59,4 +60,151 @@ bool I18nGetAcceptLanguagesFunction::RunSync() { |
| return true; |
| } |
| + |
| +I18nDetectLanguageFunction::DetectedLanguage::DetectedLanguage() |
| +: percentage(0) {} |
| + |
| +I18nDetectLanguageFunction::DetectedLanguage::DetectedLanguage( |
| + std::string language, |
| + int percentage) |
| +: language(language), percentage(percentage) {} |
| + |
| +I18nDetectLanguageFunction::DetectedLanguage::~DetectedLanguage() {} |
| + |
| +scoped_ptr<base::DictionaryValue> |
| + I18nDetectLanguageFunction::DetectedLanguage::ToValue() const { |
| + scoped_ptr<base::DictionaryValue> value(new base::DictionaryValue()); |
| + |
| + value->SetWithoutPathExpansion("language", |
| + new base::StringValue(this->language)); |
| + |
| + value->SetWithoutPathExpansion("percentage", |
| + new base::FundamentalValue(this->percentage)); |
| + |
| + |
| + return value.Pass(); |
| +} |
| + |
| +I18nDetectLanguageFunction::LanguageDetectionResult::LanguageDetectionResult() |
| +: is_reliable(false) {} |
| + |
| +I18nDetectLanguageFunction::LanguageDetectionResult::LanguageDetectionResult( |
| + bool is_reliable, |
| + std::vector<linked_ptr<DetectedLanguage>> detected_languages) |
| +: is_reliable(is_reliable), detected_langs(detected_languages) {} |
| + |
| +I18nDetectLanguageFunction::LanguageDetectionResult::~LanguageDetectionResult() |
| +{} |
| + |
| +scoped_ptr<base::DictionaryValue> |
| + I18nDetectLanguageFunction::LanguageDetectionResult::ToValue() const { |
| + scoped_ptr<base::DictionaryValue> value(new base::DictionaryValue()); |
| + |
| + value->SetWithoutPathExpansion("isReliable", |
| + new base::FundamentalValue(this->is_reliable)); |
| + |
| + if (this->detected_langs.empty()) { |
| + value->SetWithoutPathExpansion("languages", new base::ListValue()); |
| + } else { |
| + value->SetWithoutPathExpansion("languages", |
| + json_schema_compiler::util::CreateValueFromArray( |
| + this->detected_langs).release()); |
| + } |
| + |
| + return value.Pass(); |
| +} |
| + |
| + |
| +ExtensionFunction::ResponseAction I18nDetectLanguageFunction::Run() { |
| + scoped_ptr<api::i18n::DetectLanguage::Params> params( |
| + api::i18n::DetectLanguage::Params::Create(*args_)); |
| + |
| + EXTENSION_FUNCTION_VALIDATE(params.get()); |
| + |
| + GetLanguage(params->text); |
| + |
| + return RespondLater(); |
| +} |
| + |
| +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[CLD_NUM_OF_LANGS]; |
| + int percent3[CLD_NUM_OF_LANGS]; |
| + 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[CLD_NUM_OF_LANGS]; |
|
not at google - send to devlin
2015/07/10 16:21:09
just |normalized_scores|, take out all references
amalika
2015/07/13 23:57:00
Done.
|
| + |
| + cld_language = CLD2::ExtDetectLanguageSummaryCheckUTF8( |
|
not at google - send to devlin
2015/07/10 16:21:09
Declare and initialize |cld_language| here.
amalika
2015/07/13 23:57:00
Done.
|
| + 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); |
| + |
| + std::vector<linked_ptr<DetectedLanguage> > detected_languages; |
| + InitDetectedLanguages(language3, percent3, &detected_languages); |
| + |
| + SendLanguagesResult(detected_languages, is_reliable); |
| +} |
| + |
| +void I18nDetectLanguageFunction::SendLanguagesResult( |
| + std::vector<linked_ptr<DetectedLanguage> > detected_languages, |
| + bool is_reliable) { |
| + LanguageDetectionResult* result = new LanguageDetectionResult( |
|
not at google - send to devlin
2015/07/10 16:21:09
I don't see where this result is destroyed; but in
amalika
2015/07/13 23:57:01
Done.
|
| + is_reliable, |
| + detected_languages); |
| + SetResult(result->ToValue()); |
| + SendResponse(true); |
| +} |
| + |
| +void I18nDetectLanguageFunction::InitDetectedLanguages( |
| + CLD2::Language* languages, int* percent3, |
| + std::vector<linked_ptr<DetectedLanguage>> *detected_languages) { |
| + for (int i = 0; i< CLD_NUM_OF_LANGS; i++) { |
|
not at google - send to devlin
2015/07/10 16:21:09
"i <" not "i<"
amalika
2015/07/13 23:57:00
Done.
|
| + 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])); |
| + // "un" stands for undefined language |
| + // no need to save in detected_languages |
| + if (language_code == "un") { |
| + break; |
|
not at google - send to devlin
2015/07/10 16:21:09
Didn't we establish not to use "un"?
amalika
2015/07/13 23:57:01
Done.
|
| + } |
| + } |
| + |
| + detected_languages->push_back(linked_ptr<DetectedLanguage>( |
| + new DetectedLanguage(language_code, percent3[i]))); |
| + } |
| +} |
| + |
| } // namespace extensions |