Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(264)

Side by Side Diff: chrome/browser/extensions/api/i18n/i18n_api.cc

Issue 1244343002: Revert of New thin layer of API extension chrome.i18n.detectLanguage (patchset #11 id:300001 of htt… (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Do revert histograms.xml. Apparently it is OK if it's only been a few hours. Created 5 years, 5 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
« no previous file with comments | « chrome/browser/extensions/api/i18n/i18n_api.h ('k') | chrome/common/extensions/api/i18n.json » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
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 <vector> 9 #include <vector>
9 10
11 #include "base/lazy_instance.h"
10 #include "base/prefs/pref_service.h" 12 #include "base/prefs/pref_service.h"
11 #include "base/strings/string_piece.h" 13 #include "base/strings/string_piece.h"
12 #include "base/strings/string_split.h" 14 #include "base/strings/string_split.h"
13 #include "chrome/browser/profiles/profile.h" 15 #include "chrome/browser/profiles/profile.h"
16 #include "chrome/common/extensions/api/i18n.h"
14 #include "chrome/common/pref_names.h" 17 #include "chrome/common/pref_names.h"
15 #include "third_party/cld_2/src/internal/compact_lang_det_impl.h" 18
19 namespace GetAcceptLanguages = extensions::api::i18n::GetAcceptLanguages;
16 20
17 namespace extensions { 21 namespace extensions {
18 22
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
24 namespace { 23 namespace {
25 24
26 // Max number of languages detected by CLD2.
27 const int kCldNumLangs = 3;
28
29 // Errors. 25 // Errors.
30 static const char kEmptyAcceptLanguagesError[] = "accept-languages is empty."; 26 static const char kEmptyAcceptLanguagesError[] = "accept-languages is empty.";
31 27
32 } 28 }
33 29
34 bool I18nGetAcceptLanguagesFunction::RunSync() { 30 bool I18nGetAcceptLanguagesFunction::RunSync() {
35 std::string accept_languages = 31 std::string accept_languages =
36 GetProfile()->GetPrefs()->GetString(prefs::kAcceptLanguages); 32 GetProfile()->GetPrefs()->GetString(prefs::kAcceptLanguages);
37 // Currently, there are 2 ways to set browser's accept-languages: through UI 33 // Currently, there are 2 ways to set browser's accept-languages: through UI
38 // or directly modify the preference file. The accept-languages set through 34 // or directly modify the preference file. The accept-languages set through
(...skipping 17 matching lines...) Expand all
56 52
57 if (languages.empty()) { 53 if (languages.empty()) {
58 error_ = kEmptyAcceptLanguagesError; 54 error_ = kEmptyAcceptLanguagesError;
59 return false; 55 return false;
60 } 56 }
61 57
62 results_ = GetAcceptLanguages::Results::Create(languages); 58 results_ = GetAcceptLanguages::Results::Create(languages);
63 return true; 59 return true;
64 } 60 }
65 61
66 ExtensionFunction::ResponseAction I18nDetectLanguageFunction::Run() {
67 scoped_ptr<api::i18n::DetectLanguage::Params> params(
68 api::i18n::DetectLanguage::Params::Create(*args_));
69 EXTENSION_FUNCTION_VALIDATE(params);
70
71 return RespondNow(ArgumentList(GetLanguage(params->text)));
72 }
73
74 scoped_ptr<base::ListValue> I18nDetectLanguageFunction::GetLanguage(
75 const std::string& text) {
76 // TODO(mcindy): improve this by providing better CLD hints
77 // asummed no cld hints is provided
78 CLD2::CLDHints cldhints = {
79 nullptr, "", CLD2::UNKNOWN_ENCODING, CLD2::UNKNOWN_LANGUAGE};
80
81 bool is_plain_text = true; // assume the text is a plain text
82 int flags = 0; // no flags, see compact_lang_det.h for details
83 int text_bytes; // amount of non-tag/letters-only text (assumed 0)
84 int valid_prefix_bytes; // amount of valid UTF8 character in the string
85 double normalized_score[kCldNumLangs];
86
87 CLD2::Language languages[kCldNumLangs];
88 int percents[kCldNumLangs];
89 bool is_reliable = false;
90
91 // populating languages and percents
92 int cld_language = CLD2::ExtDetectLanguageSummaryCheckUTF8(
93 text.c_str(), static_cast<int>(text.size()), is_plain_text, &cldhints,
94 flags, languages, percents, normalized_score,
95 nullptr, // assumed no ResultChunkVector is used
96 &text_bytes, &is_reliable, &valid_prefix_bytes);
97
98 // Check if non-UTF8 character is encountered
99 // See bug http://crbug.com/444258.
100 if (valid_prefix_bytes < static_cast<int>(text.size()) &&
101 cld_language == CLD2::UNKNOWN_LANGUAGE) {
102 // Detect Language upto before the first non-UTF8 character
103 CLD2::DetectLanguageSummaryV2(
104 text.c_str(), valid_prefix_bytes, is_plain_text, &cldhints,
105 true, // allow extended languages
106 flags, CLD2::UNKNOWN_LANGUAGE, languages, percents, normalized_score,
107 nullptr, // assumed no ResultChunkVector is used
108 &text_bytes, &is_reliable);
109 }
110
111 LanguageDetectionResult result;
112 result.is_reliable = is_reliable;
113 InitDetectedLanguages(languages, percents, &result.languages);
114 return api::i18n::DetectLanguage::Results::Create(result);
115 }
116
117 void I18nDetectLanguageFunction::InitDetectedLanguages(
118 CLD2::Language* languages,
119 int* percents,
120 std::vector<linked_ptr<DetectedLanguage>>* detected_languages) {
121 for (int i = 0; i < kCldNumLangs; i++) {
122 std::string language_code = "";
123
124 // Convert LanguageCode 'zh' to 'zh-CN' and 'zh-Hant' to 'zh-TW' for
125 // Translate server usage. see DetermineTextLanguage in
126 // components/translate/core/language_detection/language_detection_util.cc
127 if (languages[i] == CLD2::UNKNOWN_LANGUAGE) {
128 // no need to save in detected_languages
129 break;
130 } else if (languages[i] == CLD2::CHINESE) {
131 language_code = "zh-CN";
132 } else if (languages[i] == CLD2::CHINESE_T) {
133 language_code = "zh-TW";
134 } else {
135 language_code =
136 CLD2::LanguageCode(static_cast<CLD2::Language>(languages[i]));
137 }
138 linked_ptr<DetectedLanguage> detected_lang(new DetectedLanguage);
139 detected_lang->language = language_code;
140 detected_lang->percentage = percents[i];
141 detected_languages->push_back(detected_lang);
142 }
143 }
144
145 } // namespace extensions 62 } // namespace extensions
OLDNEW
« no previous file with comments | « chrome/browser/extensions/api/i18n/i18n_api.h ('k') | chrome/common/extensions/api/i18n.json » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698