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

Side by Side Diff: chrome/renderer/translate_helper.cc

Issue 12221085: Translate: split language code typo correction to apply unit tests. (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: rebase Created 7 years, 10 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 | Annotate | Revision Log
« no previous file with comments | « chrome/renderer/translate_helper.h ('k') | chrome/renderer/translate_helper_unittest.cc » ('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/renderer/translate_helper.h" 5 #include "chrome/renderer/translate_helper.h"
6 6
7 #include "base/bind.h" 7 #include "base/bind.h"
8 #include "base/compiler_specific.h" 8 #include "base/compiler_specific.h"
9 #include "base/logging.h" 9 #include "base/logging.h"
10 #include "base/message_loop.h" 10 #include "base/message_loop.h"
(...skipping 75 matching lines...) Expand 10 before | Expand all | Expand 10 after
86 // Get the document language as set by WebKit from the http-equiv 86 // Get the document language as set by WebKit from the http-equiv
87 // meta tag for "content-language". This may or may not also 87 // meta tag for "content-language". This may or may not also
88 // have a value derived from the actual Content-Language HTTP 88 // have a value derived from the actual Content-Language HTTP
89 // header. The two actually have different meanings (despite the 89 // header. The two actually have different meanings (despite the
90 // original intent of http-equiv to be an equivalent) with the former 90 // original intent of http-equiv to be an equivalent) with the former
91 // being the language of the document and the latter being the 91 // being the language of the document and the latter being the
92 // language of the intended audience (a distinction really only 92 // language of the intended audience (a distinction really only
93 // relevant for things like langauge textbooks). This distinction 93 // relevant for things like langauge textbooks). This distinction
94 // shouldn't affect translation. 94 // shouldn't affect translation.
95 std::string language = document.contentLanguage().utf8(); 95 std::string language = document.contentLanguage().utf8();
96 size_t coma_index = language.find(','); 96 CorrectLanguageCodeTypo(&language);
97 if (coma_index != std::string::npos) {
98 // There are more than 1 language specified, just keep the first one.
99 language = language.substr(0, coma_index);
100 }
101 TrimWhitespaceASCII(language, TRIM_ALL, &language);
102
103 // An underscore instead of a dash is a frequent mistake.
104 size_t underscore_index = language.find('_');
105 if (underscore_index != std::string::npos)
106 language[underscore_index] = '-';
107
108 // Change everything up to a dash to lower-case and everything after to upper.
109 size_t dash_index = language.find('-');
110 if (dash_index != std::string::npos) {
111 language = StringToLowerASCII(language.substr(0, dash_index)) +
112 StringToUpperASCII(language.substr(dash_index));
113 } else {
114 language = StringToLowerASCII(language);
115 }
116 97
117 #if defined(ENABLE_LANGUAGE_DETECTION) 98 #if defined(ENABLE_LANGUAGE_DETECTION)
118 if (language.empty()) { 99 if (language.empty()) {
119 base::TimeTicks begin_time = base::TimeTicks::Now(); 100 base::TimeTicks begin_time = base::TimeTicks::Now();
120 language = DetermineTextLanguage(contents); 101 language = DetermineTextLanguage(contents);
121 UMA_HISTOGRAM_MEDIUM_TIMES("Renderer4.LanguageDetection", 102 UMA_HISTOGRAM_MEDIUM_TIMES("Renderer4.LanguageDetection",
122 base::TimeTicks::Now() - begin_time); 103 base::TimeTicks::Now() - begin_time);
123 } else { 104 } else {
124 VLOG(9) << "PageLanguageFromMetaTag: " << language; 105 VLOG(9) << "PageLanguageFromMetaTag: " << language;
125 } 106 }
(...skipping 109 matching lines...) Expand 10 before | Expand all | Expand 10 after
235 } 216 }
236 217
237 bool TranslateHelper::DontDelayTasks() { 218 bool TranslateHelper::DontDelayTasks() {
238 return false; 219 return false;
239 } 220 }
240 221
241 //////////////////////////////////////////////////////////////////////////////// 222 ////////////////////////////////////////////////////////////////////////////////
242 // TranslateHelper, private: 223 // TranslateHelper, private:
243 // 224 //
244 // static 225 // static
226 void TranslateHelper::CorrectLanguageCodeTypo(std::string* code) {
227 DCHECK(code);
228
229 size_t coma_index = code->find(',');
230 if (coma_index != std::string::npos) {
231 // There are more than 1 language specified, just keep the first one.
232 *code = code->substr(0, coma_index);
233 }
234 TrimWhitespaceASCII(*code, TRIM_ALL, code);
235
236 // An underscore instead of a dash is a frequent mistake.
237 size_t underscore_index = code->find('_');
238 if (underscore_index != std::string::npos)
239 (*code)[underscore_index] = '-';
240
241 // Change everything up to a dash to lower-case and everything after to upper.
242 size_t dash_index = code->find('-');
243 if (dash_index != std::string::npos) {
244 *code = StringToLowerASCII(code->substr(0, dash_index)) +
245 StringToUpperASCII(code->substr(dash_index));
246 } else {
247 *code = StringToLowerASCII(*code);
248 }
249 }
250
251 // static
245 void TranslateHelper::ConvertLanguageCodeSynonym(std::string* code) { 252 void TranslateHelper::ConvertLanguageCodeSynonym(std::string* code) {
253 DCHECK(code);
254
246 // Apply liner search here because number of items in the list is just four. 255 // Apply liner search here because number of items in the list is just four.
247 for (size_t i = 0; i < arraysize(kLanguageCodeSynonyms); ++i) { 256 for (size_t i = 0; i < arraysize(kLanguageCodeSynonyms); ++i) {
248 if (code->compare(kLanguageCodeSynonyms[i].from) == 0) { 257 if (code->compare(kLanguageCodeSynonyms[i].from) == 0) {
249 *code = std::string(kLanguageCodeSynonyms[i].to); 258 *code = std::string(kLanguageCodeSynonyms[i].to);
250 break; 259 break;
251 } 260 }
252 } 261 }
253 } 262 }
254 263
255 // static 264 // static
(...skipping 223 matching lines...) Expand 10 before | Expand all | Expand 10 after
479 WebView* web_view = render_view()->GetWebView(); 488 WebView* web_view = render_view()->GetWebView();
480 if (!web_view) { 489 if (!web_view) {
481 // When the WebView is going away, the render view should have called 490 // When the WebView is going away, the render view should have called
482 // CancelPendingTranslation() which should have stopped any pending work, so 491 // CancelPendingTranslation() which should have stopped any pending work, so
483 // that case should not happen. 492 // that case should not happen.
484 NOTREACHED(); 493 NOTREACHED();
485 return NULL; 494 return NULL;
486 } 495 }
487 return web_view->mainFrame(); 496 return web_view->mainFrame();
488 } 497 }
OLDNEW
« no previous file with comments | « chrome/renderer/translate_helper.h ('k') | chrome/renderer/translate_helper_unittest.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698