diff options
author | dfilimon@google.com <dfilimon@google.com@0039d316-1c4b-4281-b951-d872f2087c98> | 2011-07-12 13:06:31 +0000 |
---|---|---|
committer | dfilimon@google.com <dfilimon@google.com@0039d316-1c4b-4281-b951-d872f2087c98> | 2011-07-12 13:06:31 +0000 |
commit | 3f0fe5d075a185562ebaa540f062696d6e53e2a5 (patch) | |
tree | 3810706bce050da31ae088c3fa152bba81fde570 /chrome/browser/translate | |
parent | 0d80b8cb59db7ed226f7b5ff75afd268df9f46fe (diff) | |
download | chromium_src-3f0fe5d075a185562ebaa540f062696d6e53e2a5.zip chromium_src-3f0fe5d075a185562ebaa540f062696d6e53e2a5.tar.gz chromium_src-3f0fe5d075a185562ebaa540f062696d6e53e2a5.tar.bz2 |
GetTargetLanguage() returns the UI language if it's supported by the
translation service or the first supported language in the accepted languages
list. If there is no such language, returns "".
BUG=37328
TEST=1. browser_tests: TranslateManagerTest.TranslateAcceptLanguage and TranslateManagerTest.UnsupportedUILanguag
2. a. Start Chrome in the UI language that's unsupported by the translation
service (like Amharic). On Windows, "chrome --lang=am". On Linux, 'LANGUAGE=am chrome' with Amharic locale installed.
b. add a language supported by the translation service to the Accept-Languages list (e.g Italian and Spanish).
c. When navigating to a page in a third language (e.g. French), it should offer to translate to the first supported language in the Accept-Language list (Italian in the scenario).
Contributed by dfilimon@google.com
Review URL: http://codereview.chromium.org/7327008
git-svn-id: svn://svn.chromium.org/chrome/trunk/src@92162 0039d316-1c4b-4281-b951-d872f2087c98
Diffstat (limited to 'chrome/browser/translate')
-rw-r--r-- | chrome/browser/translate/translate_manager.cc | 27 | ||||
-rw-r--r-- | chrome/browser/translate/translate_manager.h | 11 | ||||
-rw-r--r-- | chrome/browser/translate/translate_manager_browsertest.cc | 22 |
3 files changed, 51 insertions, 9 deletions
diff --git a/chrome/browser/translate/translate_manager.cc b/chrome/browser/translate/translate_manager.cc index 0888ba5..1fcf475 100644 --- a/chrome/browser/translate/translate_manager.cc +++ b/chrome/browser/translate/translate_manager.cc @@ -459,7 +459,7 @@ void TranslateManager::InitiateTranslation(TabContents* tab, if (GetTranslateInfoBarDelegate(tab)) return; - std::string target_lang = GetTargetLanguage(); + std::string target_lang = GetTargetLanguage(prefs); std::string language_code = GetLanguageCode(page_lang); // Nothing to do if either the language Chrome is in or the language of the // page is not supported by the translation server. @@ -761,10 +761,27 @@ void TranslateManager::ShowInfoBar(TabContents* tab, } // static -std::string TranslateManager::GetTargetLanguage() { - std::string target_lang = - GetLanguageCode(g_browser_process->GetApplicationLocale()); - return IsSupportedLanguage(target_lang) ? target_lang : std::string(); +std::string TranslateManager::GetTargetLanguage(PrefService* prefs) { + std::string ui_lang = + GetLanguageCode(g_browser_process->GetApplicationLocale()); + if (IsSupportedLanguage(ui_lang)) + return ui_lang; + + // Getting the accepted languages list + std::string accept_langs_str = prefs->GetString(prefs::kAcceptLanguages); + + std::vector<std::string> accept_langs_list; + base::SplitString(accept_langs_str, ',', &accept_langs_list); + + // Will translate to the first supported language on the Accepted Language + // list or not at all if no such candidate exists + std::vector<std::string>::iterator iter; + for (iter = accept_langs_list.begin(); + iter != accept_langs_list.end(); ++iter) { + if (IsSupportedLanguage(GetLanguageCode(*iter))) + return *iter; + } + return std::string(); } // static diff --git a/chrome/browser/translate/translate_manager.h b/chrome/browser/translate/translate_manager.h index ab710f2..2537d8e 100644 --- a/chrome/browser/translate/translate_manager.h +++ b/chrome/browser/translate/translate_manager.h @@ -168,10 +168,13 @@ class TranslateManager : public NotificationObserver, // translate infobar is showing, it just replaces it with the new one. void ShowInfoBar(TabContents* tab, TranslateInfoBarDelegate* infobar); - // Returns the language to translate to, which is the language the UI is - // configured in. Returns an empty string if that language is not supported - // by the translation service. - static std::string GetTargetLanguage(); + // Returns the language to translate to. The language returned is the + // first language found in the following list that is supported by the + // translation service: + // the UI language + // the accept-language list + // If no language is found then an empty string is returned. + static std::string GetTargetLanguage(PrefService* prefs); // Returns the translate info bar showing in |tab| or NULL if none is showing. static TranslateInfoBarDelegate* GetTranslateInfoBarDelegate( diff --git a/chrome/browser/translate/translate_manager_browsertest.cc b/chrome/browser/translate/translate_manager_browsertest.cc index 5e9cf9e..3028561 100644 --- a/chrome/browser/translate/translate_manager_browsertest.cc +++ b/chrome/browser/translate/translate_manager_browsertest.cc @@ -873,6 +873,10 @@ TEST_F(TranslateManagerTest, UnsupportedUILanguage) { std::string original_lang = browser_process->GetApplicationLocale(); browser_process->SetApplicationLocale("qbz"); + // Make sure that the accept language list only contains unsupported languages + PrefService* prefs = contents()->profile()->GetPrefs(); + prefs->SetString(prefs::kAcceptLanguages, "qbz"); + // Simulate navigating to a page in a language supported by the translate // server. SimulateNavigation(GURL("http://www.google.com"), "en", true); @@ -883,6 +887,24 @@ TEST_F(TranslateManagerTest, UnsupportedUILanguage) { browser_process->SetApplicationLocale(original_lang); } +// Tests that the first supported accept language is selected +TEST_F(TranslateManagerTest, TranslateAcceptLanguage) { + // Set locate to non-existant language + TestingBrowserProcess* browser_process = testing_browser_process_.get(); + std::string original_lang = browser_process->GetApplicationLocale(); + browser_process->SetApplicationLocale("qbz"); + + // Set Qbz and French as the only accepted languages + PrefService* prefs = contents()->profile()->GetPrefs(); + prefs->SetString(prefs::kAcceptLanguages, "qbz,fr"); + + // Go to a German page + SimulateNavigation(GURL("http://google.de"), "de", true); + + // Expect the infobar to pop up + EXPECT_TRUE(GetTranslateInfoBar() != NULL); +} + // Tests that the translate enabled preference is honored. TEST_F(TranslateManagerTest, TranslateEnabledPref) { // Make sure the pref allows translate. |