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" | |
12 #include "base/prefs/pref_service.h" | 10 #include "base/prefs/pref_service.h" |
13 #include "base/strings/string_piece.h" | 11 #include "base/strings/string_piece.h" |
14 #include "base/strings/string_split.h" | 12 #include "base/strings/string_split.h" |
15 #include "chrome/browser/profiles/profile.h" | 13 #include "chrome/browser/profiles/profile.h" |
16 #include "chrome/common/extensions/api/i18n.h" | |
17 #include "chrome/common/pref_names.h" | 14 #include "chrome/common/pref_names.h" |
18 | 15 #include "third_party/cld_2/src/internal/compact_lang_det_impl.h" |
19 namespace GetAcceptLanguages = extensions::api::i18n::GetAcceptLanguages; | |
20 | 16 |
21 namespace extensions { | 17 namespace extensions { |
22 | 18 |
19 namespace GetAcceptLanguages = api::i18n::GetAcceptLanguages; | |
20 using DetectedLanguage = | |
21 api::i18n::DetectLanguage::Results::Result::LanguagesType; | |
22 using LanguageDetectionResult = api::i18n::DetectLanguage::Results::Result; | |
23 | |
23 namespace { | 24 namespace { |
24 | 25 |
26 // max number of languages detected by CLD2 | |
not at google - send to devlin
2015/07/14 23:16:14
Use properly formatted sentences, so "Max" not "ma
amalika
2015/07/15 22:33:16
Done.
| |
27 const int kCldNumLangs = 3; | |
not at google - send to devlin
2015/07/14 23:16:14
Blank line below this.
amalika
2015/07/15 22:33:16
Done.
| |
25 // Errors. | 28 // Errors. |
26 static const char kEmptyAcceptLanguagesError[] = "accept-languages is empty."; | 29 static const char kEmptyAcceptLanguagesError[] = "accept-languages is empty."; |
27 | 30 |
28 } | 31 } |
29 | 32 |
30 bool I18nGetAcceptLanguagesFunction::RunSync() { | 33 bool I18nGetAcceptLanguagesFunction::RunSync() { |
31 std::string accept_languages = | 34 std::string accept_languages = |
32 GetProfile()->GetPrefs()->GetString(prefs::kAcceptLanguages); | 35 GetProfile()->GetPrefs()->GetString(prefs::kAcceptLanguages); |
33 // Currently, there are 2 ways to set browser's accept-languages: through UI | 36 // Currently, there are 2 ways to set browser's accept-languages: through UI |
34 // or directly modify the preference file. The accept-languages set through | 37 // or directly modify the preference file. The accept-languages set through |
(...skipping 17 matching lines...) Expand all Loading... | |
52 | 55 |
53 if (languages.empty()) { | 56 if (languages.empty()) { |
54 error_ = kEmptyAcceptLanguagesError; | 57 error_ = kEmptyAcceptLanguagesError; |
55 return false; | 58 return false; |
56 } | 59 } |
57 | 60 |
58 results_ = GetAcceptLanguages::Results::Create(languages); | 61 results_ = GetAcceptLanguages::Results::Create(languages); |
59 return true; | 62 return true; |
60 } | 63 } |
61 | 64 |
65 ExtensionFunction::ResponseAction I18nDetectLanguageFunction::Run() { | |
66 scoped_ptr<api::i18n::DetectLanguage::Params> params( | |
67 api::i18n::DetectLanguage::Params::Create(*args_)); | |
68 EXTENSION_FUNCTION_VALIDATE(params); | |
69 | |
70 return RespondNow(ArgumentList(GetLanguage(params->text))); | |
71 } | |
72 | |
73 scoped_ptr<base::ListValue> I18nDetectLanguageFunction::GetLanguage( | |
74 const std::string& text) { | |
75 // TODO(mcindy): improve this by providing better CLD hints | |
76 // asummed no cld hints is provided | |
77 CLD2::CLDHints cldhints = { | |
78 nullptr, "", CLD2::UNKNOWN_ENCODING, CLD2::UNKNOWN_LANGUAGE}; | |
79 | |
80 bool is_plain_text = true; // assumed the text is plain text | |
not at google - send to devlin
2015/07/14 23:16:14
"assume" not "assumed"
amalika
2015/07/15 22:33:16
Done.
| |
81 int flags = 0; // assumed 0 flags provided | |
not at google - send to devlin
2015/07/14 23:16:14
A comment like "assume 0 flags provided" is not ve
amalika
2015/07/15 22:33:16
Done.
| |
82 int text_bytes; // amount of non-tag/letters-only text (assumed 0) | |
83 int valid_prefix_bytes; // amount of valid UTF8 character in the string | |
84 double normalized_score[kCldNumLangs]; | |
85 | |
86 CLD2::Language languages[kCldNumLangs]; | |
87 int percents[kCldNumLangs]; | |
88 bool is_reliable = false; | |
89 | |
90 // populating languages and percents | |
91 int cld_language = CLD2::ExtDetectLanguageSummaryCheckUTF8( | |
92 text.c_str(), static_cast<int>(text.size()), is_plain_text, &cldhints, | |
93 flags, languages, percents, normalized_score, | |
94 nullptr, // assumed no ResultChunkVector is used | |
95 &text_bytes, &is_reliable, &valid_prefix_bytes); | |
96 | |
97 // Check if non-UTF8 character is encountered | |
98 // See bug http://crbug.com/444258. | |
99 if (valid_prefix_bytes < static_cast<int>(text.size()) && | |
100 cld_language == CLD2::UNKNOWN_LANGUAGE) { | |
101 // Detect Language upto before the first non-UTF8 character | |
102 CLD2::DetectLanguageSummaryV2( | |
103 text.c_str(), valid_prefix_bytes, is_plain_text, &cldhints, | |
104 true, // allow extended languages | |
105 flags, CLD2::UNKNOWN_LANGUAGE, languages, percents, normalized_score, | |
106 nullptr, // assumed no ResultChunkVector is used | |
107 &text_bytes, &is_reliable); | |
108 } | |
109 | |
110 LanguageDetectionResult result; | |
111 result.is_reliable = is_reliable; | |
112 InitDetectedLanguages(languages, percents, &result.languages); | |
113 return api::i18n::DetectLanguage::Results::Create(result); | |
114 } | |
115 | |
116 void I18nDetectLanguageFunction::InitDetectedLanguages( | |
117 CLD2::Language* languages, | |
118 int* percents, | |
119 std::vector<linked_ptr<DetectedLanguage>>* detected_languages) { | |
120 for (int i = 0; i < kCldNumLangs; i++) { | |
121 std::string language_code = ""; | |
122 | |
123 // Convert LanguageCode 'zh' to 'zh-CN' and 'zh-Hant' to 'zh-TW' for | |
124 // Translate server usage. see DetermineTextLanguage in | |
125 // components/translate/core/language_detection/language_detection_util.cc | |
126 if (languages[i] == CLD2::UNKNOWN_LANGUAGE) { | |
127 // no need to save in detected_languages | |
128 break; | |
129 } else if (languages[i] == CLD2::CHINESE) { | |
130 language_code = "zh-CN"; | |
131 } else if (languages[i] == CLD2::CHINESE_T) { | |
132 language_code = "zh-TW"; | |
133 } else { | |
134 language_code = | |
135 CLD2::LanguageCode(static_cast<CLD2::Language>(languages[i])); | |
136 } | |
137 linked_ptr<DetectedLanguage> detected_lang(new DetectedLanguage); | |
138 detected_lang->language = language_code; | |
139 detected_lang->percentage = percents[i]; | |
140 detected_languages->push_back(detected_lang); | |
141 } | |
142 } | |
143 | |
62 } // namespace extensions | 144 } // namespace extensions |
OLD | NEW |