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

Side by Side Diff: chrome/browser/translate/translate_prefs.cc

Issue 25531002: Move language detection to a component (Closed) Base URL: http://git.chromium.org/chromium/src.git@master
Patch Set: Run translate unittests on iOS Created 7 years, 2 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
OLDNEW
1 // Copyright (c) 2011 The Chromium Authors. All rights reserved. 1 // Copyright (c) 2011 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/translate/translate_prefs.h" 5 #include "chrome/browser/translate/translate_prefs.h"
6 6
7 #include <set> 7 #include <set>
8 8
9 #include "base/prefs/pref_service.h" 9 #include "base/prefs/pref_service.h"
10 #include "base/strings/string_split.h" 10 #include "base/strings/string_split.h"
11 #include "base/strings/string_util.h" 11 #include "base/strings/string_util.h"
12 #include "chrome/browser/browser_process.h" 12 #include "chrome/browser/browser_process.h"
13 #include "chrome/browser/prefs/scoped_user_pref_update.h" 13 #include "chrome/browser/prefs/scoped_user_pref_update.h"
14 #include "chrome/browser/profiles/profile.h" 14 #include "chrome/browser/profiles/profile.h"
15 #include "chrome/browser/translate/translate_accept_languages.h" 15 #include "chrome/browser/translate/translate_accept_languages.h"
16 #include "chrome/browser/translate/translate_manager.h" 16 #include "chrome/browser/translate/translate_manager.h"
17 #include "chrome/common/pref_names.h" 17 #include "chrome/common/pref_names.h"
18 #include "chrome/common/translate/translate_util.h" 18 #include "components/translate/common/translate_util.h"
19 #include "components/user_prefs/pref_registry_syncable.h" 19 #include "components/user_prefs/pref_registry_syncable.h"
20 20
21 const char TranslatePrefs::kPrefTranslateLanguageBlacklist[] = 21 const char TranslatePrefs::kPrefTranslateLanguageBlacklist[] =
22 "translate_language_blacklist"; 22 "translate_language_blacklist";
23 const char TranslatePrefs::kPrefTranslateSiteBlacklist[] = 23 const char TranslatePrefs::kPrefTranslateSiteBlacklist[] =
24 "translate_site_blacklist"; 24 "translate_site_blacklist";
25 const char TranslatePrefs::kPrefTranslateWhitelists[] = 25 const char TranslatePrefs::kPrefTranslateWhitelists[] =
26 "translate_whitelists"; 26 "translate_whitelists";
27 const char TranslatePrefs::kPrefTranslateDeniedCount[] = 27 const char TranslatePrefs::kPrefTranslateDeniedCount[] =
28 "translate_denied_count"; 28 "translate_denied_count";
(...skipping 27 matching lines...) Expand all
56 base::SplitString(lang, '-', &tokens); 56 base::SplitString(lang, '-', &tokens);
57 if (tokens.size() < 1) 57 if (tokens.size() < 1)
58 return lang; 58 return lang;
59 59
60 std::string main_part = tokens[0]; 60 std::string main_part = tokens[0];
61 61
62 // Translate doesn't support General Chinese and the sub code is necessary. 62 // Translate doesn't support General Chinese and the sub code is necessary.
63 if (main_part == "zh") 63 if (main_part == "zh")
64 return lang; 64 return lang;
65 65
66 TranslateUtil::ToTranslateLanguageSynonym(&main_part); 66 translate::ToTranslateLanguageSynonym(&main_part);
67 return main_part; 67 return main_part;
68 } 68 }
69 69
70 // Expands language codes to make these more suitable for Accept-Language. 70 // Expands language codes to make these more suitable for Accept-Language.
71 // Example: ['en-US', 'ja', 'en-CA'] => ['en-US', 'en', 'ja', 'en-CA']. 71 // Example: ['en-US', 'ja', 'en-CA'] => ['en-US', 'en', 'ja', 'en-CA'].
72 // 'en' won't appear twice as this function eliminates duplicates. 72 // 'en' won't appear twice as this function eliminates duplicates.
73 void ExpandLanguageCodes(const std::vector<std::string>& languages, 73 void ExpandLanguageCodes(const std::vector<std::string>& languages,
74 std::vector<std::string>* expanded_languages) { 74 std::vector<std::string>* expanded_languages) {
75 DCHECK(expanded_languages); 75 DCHECK(expanded_languages);
76 DCHECK(expanded_languages->empty()); 76 DCHECK(expanded_languages->empty());
(...skipping 32 matching lines...) Expand 10 before | Expand all | Expand 10 after
109 return IsValueBlacklisted(kPrefTranslateBlockedLanguages, 109 return IsValueBlacklisted(kPrefTranslateBlockedLanguages,
110 original_language); 110 original_language);
111 } 111 }
112 112
113 void TranslatePrefs::BlockLanguage( 113 void TranslatePrefs::BlockLanguage(
114 const std::string& original_language) { 114 const std::string& original_language) {
115 BlacklistValue(kPrefTranslateBlockedLanguages, original_language); 115 BlacklistValue(kPrefTranslateBlockedLanguages, original_language);
116 116
117 // Add the language to the language list at chrome://settings/languages. 117 // Add the language to the language list at chrome://settings/languages.
118 std::string language = original_language; 118 std::string language = original_language;
119 TranslateUtil::ToChromeLanguageSynonym(&language); 119 translate::ToChromeLanguageSynonym(&language);
120 120
121 std::vector<std::string> languages; 121 std::vector<std::string> languages;
122 GetLanguageList(&languages); 122 GetLanguageList(&languages);
123 123
124 if (std::find(languages.begin(), languages.end(), language) == 124 if (std::find(languages.begin(), languages.end(), language) ==
125 languages.end()) { 125 languages.end()) {
126 languages.push_back(language); 126 languages.push_back(language);
127 UpdateLanguageList(languages); 127 UpdateLanguageList(languages);
128 } 128 }
129 } 129 }
(...skipping 284 matching lines...) Expand 10 before | Expand all | Expand 10 after
414 ListValue* list = update.Get(); 414 ListValue* list = update.Get();
415 DCHECK(list != NULL); 415 DCHECK(list != NULL);
416 list->Swap(&blocked_languages_list); 416 list->Swap(&blocked_languages_list);
417 } 417 }
418 418
419 // Update kAcceptLanguages 419 // Update kAcceptLanguages
420 for (std::vector<std::string>::const_iterator it = 420 for (std::vector<std::string>::const_iterator it =
421 blocked_languages.begin(); 421 blocked_languages.begin();
422 it != blocked_languages.end(); ++it) { 422 it != blocked_languages.end(); ++it) {
423 std::string lang = *it; 423 std::string lang = *it;
424 TranslateUtil::ToChromeLanguageSynonym(&lang); 424 translate::ToChromeLanguageSynonym(&lang);
425 bool not_found = 425 bool not_found =
426 std::find(accept_languages.begin(), accept_languages.end(), lang) == 426 std::find(accept_languages.begin(), accept_languages.end(), lang) ==
427 accept_languages.end(); 427 accept_languages.end();
428 if (not_found) 428 if (not_found)
429 accept_languages.push_back(lang); 429 accept_languages.push_back(lang);
430 } 430 }
431 431
432 std::string new_accept_languages_str = JoinString(accept_languages, ","); 432 std::string new_accept_languages_str = JoinString(accept_languages, ",");
433 user_prefs->SetString(prefs::kAcceptLanguages, new_accept_languages_str); 433 user_prefs->SetString(prefs::kAcceptLanguages, new_accept_languages_str);
434 } 434 }
(...skipping 89 matching lines...) Expand 10 before | Expand all | Expand 10 after
524 524
525 bool TranslatePrefs::IsListEmpty(const char* pref_id) const { 525 bool TranslatePrefs::IsListEmpty(const char* pref_id) const {
526 const ListValue* blacklist = prefs_->GetList(pref_id); 526 const ListValue* blacklist = prefs_->GetList(pref_id);
527 return (blacklist == NULL || blacklist->empty()); 527 return (blacklist == NULL || blacklist->empty());
528 } 528 }
529 529
530 bool TranslatePrefs::IsDictionaryEmpty(const char* pref_id) const { 530 bool TranslatePrefs::IsDictionaryEmpty(const char* pref_id) const {
531 const DictionaryValue* dict = prefs_->GetDictionary(pref_id); 531 const DictionaryValue* dict = prefs_->GetDictionary(pref_id);
532 return (dict == NULL || dict->empty()); 532 return (dict == NULL || dict->empty());
533 } 533 }
OLDNEW
« no previous file with comments | « chrome/browser/translate/translate_manager.cc ('k') | chrome/browser/translate/translate_script.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698