diff options
author | kuan@chromium.org <kuan@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98> | 2010-02-02 06:42:11 +0000 |
---|---|---|
committer | kuan@chromium.org <kuan@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98> | 2010-02-02 06:42:11 +0000 |
commit | 96721067675b115fec50d299302fb2ec08de394d (patch) | |
tree | b3649c6fa7ecbba7f1c65b21bbb76dfa8ec15185 | |
parent | 3919ae943247f31414cd444dab0a053e22226179 (diff) | |
download | chromium_src-96721067675b115fec50d299302fb2ec08de394d.zip chromium_src-96721067675b115fec50d299302fb2ec08de394d.tar.gz chromium_src-96721067675b115fec50d299302fb2ec08de394d.tar.bz2 |
implement proper localized display names for translate languages
- in the process, modify to use indexes into vector of languages to communicate between infobars and delegates
BUG=31297
TEST=verify that language names in translate infobars' language buttons, dropdown lists and options are properly localized.
Review URL: http://codereview.chromium.org/566012
git-svn-id: svn://svn.chromium.org/chrome/trunk/src@37809 0039d316-1c4b-4281-b951-d872f2087c98
8 files changed, 166 insertions, 104 deletions
diff --git a/chrome/browser/renderer_host/translation_service.cc b/chrome/browser/renderer_host/translation_service.cc index db4441e..8f37e35 100644 --- a/chrome/browser/renderer_host/translation_service.cc +++ b/chrome/browser/renderer_host/translation_service.cc @@ -45,6 +45,63 @@ LocaleToCLDLanguage kLocaleToCLDLanguages[] = { { "es-419", "es" }, }; +// The list of languages the Google translation server supports. +const char* kSupportedLanguages[] = { + "af", // Afrikaans + "sq", // Albanian + "ar", // Arabic + "be", // Belarusian + "bg", // Bulgarian + "ca", // Catalan + "zh-CN", // Chinese (Simplified) + "zh-TW", // Chinese (Traditional) + "hr", // Croatian + "cs", // Czech + "da", // Danish + "nl", // Dutch + "en", // English + "et", // Estonian + "tl", // Tagalog (Filipino) + "fi", // Finnish + "fr", // French + "gl", // Galician + "de", // German + "el", // Greek + "he", // Hebrew + "hi", // Hindi + "hu", // Hungarian + "is", // Icelandic + "id", // Indonesian + "it", // Italian + "ga", // Irish + "ja", // Japanese + "ko", // Korean + "lv", // Latvian + "lt", // Lithuanian + "mk", // Macedonian + "ms", // Malay + "mt", // Maltese + "no", // Norwegian + "fa", // Persian + "pl", // Polish + "pt", // Portuguese + "ro", // Romanian + "ru", // Russian + "sr", // Serbian + "sk", // Slovak + "sl", // Slovenian + "es", // Spanish + "sw", // Swahili + "sv", // Swedish + "th", // Thai + "tr", // Turkish + "uk", // Ukrainian + "vi", // Vietnamese + "cy", // Welsh + "yi", // Yiddish +}; + + // The maximum size in bytes after which the server will refuse the request. const size_t kTextRequestMaxSize = 1024 * 30; @@ -347,6 +404,14 @@ bool TranslationService::IsTranslationEnabled() { return GURL(kServiceURL).host() != "disabled"; } +// static +void TranslationService::GetSupportedLanguages( + std::vector<std::string>* languages) { + DCHECK(languages && languages->empty()); + for (size_t i = 0; i < arraysize(kSupportedLanguages); ++i) + languages->push_back(kSupportedLanguages[i]); +} + //////////////////////////////////////////////////////////////////////////////// // TranslationService, protected: diff --git a/chrome/browser/renderer_host/translation_service.h b/chrome/browser/renderer_host/translation_service.h index bcf6322..1e9ed91 100644 --- a/chrome/browser/renderer_host/translation_service.h +++ b/chrome/browser/renderer_host/translation_service.h @@ -69,6 +69,10 @@ class TranslationService : public URLFetcher::Delegate { // Returns true if the TranslationService is enabled. static bool IsTranslationEnabled(); + // Fills |languages| with the list of languages that the translate server can + // translate to and from. + static void GetSupportedLanguages(std::vector<std::string>* languages); + protected: // The amount of time in ms after which a pending request is sent if no other // translation request has been received. diff --git a/chrome/browser/translate/languages_menu_model.cc b/chrome/browser/translate/languages_menu_model.cc index 3f26d58..ba749df 100644 --- a/chrome/browser/translate/languages_menu_model.cc +++ b/chrome/browser/translate/languages_menu_model.cc @@ -1,6 +1,6 @@ -// Copyright (c) 2010 The Chromium Authors. All rights reserved. Use of this -// source code is governed by a BSD-style license that can be found in the -// LICENSE file. +// Copyright (c) 2010 The Chromium Authors. All rights reserved. +// Use of this source code is governed by a BSD-style license that can be +// found in the LICENSE file. #include "chrome/browser/translate/languages_menu_model.h" @@ -24,7 +24,7 @@ LanguagesMenuModel::LanguagesMenuModel( } std::vector<std::string>::const_iterator iter = languages.begin(); for (int i = base_command_id; iter != languages.end(); ++i, ++iter) { - AddItem(i, ASCIIToUTF16(*iter)); + AddItem(i, TranslateInfoBarDelegate::GetDisplayNameForLocale(*iter)); } } diff --git a/chrome/browser/translate/options_menu_model.cc b/chrome/browser/translate/options_menu_model.cc index 4dfd631..da1f896 100644 --- a/chrome/browser/translate/options_menu_model.cc +++ b/chrome/browser/translate/options_menu_model.cc @@ -1,6 +1,6 @@ -// Copyright (c) 2010 The Chromium Authors. All rights reserved. Use of this -// source code is governed by a BSD-style license that can be found in the -// LICENSE file. +// Copyright (c) 2010 The Chromium Authors. All rights reserved. +// Use of this source code is governed by a BSD-style license that can be +// found in the LICENSE file. #include "chrome/browser/translate/options_menu_model.h" @@ -13,7 +13,8 @@ OptionsMenuModel::OptionsMenuModel(menus::SimpleMenuModel::Delegate* delegate, TranslateInfoBarDelegate* translate_delegate, bool before_translate) : menus::SimpleMenuModel(delegate) { string16 original_language = - ASCIIToUTF16(translate_delegate->original_language()); + TranslateInfoBarDelegate::GetDisplayNameForLocale( + translate_delegate->original_lang_code()); if (before_translate) { AddCheckItem(IDC_TRANSLATE_OPTIONS_NEVER_TRANSLATE_LANG, l10n_util::GetStringFUTF16( @@ -23,10 +24,12 @@ OptionsMenuModel::OptionsMenuModel(menus::SimpleMenuModel::Delegate* delegate, l10n_util::GetStringUTF16( IDS_TRANSLATE_INFOBAR_OPTIONS_NEVER_TRANSLATE_SITE)); } else { + string16 target_language = + TranslateInfoBarDelegate::GetDisplayNameForLocale( + translate_delegate->target_lang_code()); AddCheckItem(IDC_TRANSLATE_OPTIONS_ALWAYS, l10n_util::GetStringFUTF16(IDS_TRANSLATE_INFOBAR_OPTIONS_ALWAYS, - original_language, - ASCIIToUTF16(translate_delegate->target_language()))); + original_language, target_language)); } AddItemWithStringId(IDC_TRANSLATE_OPTIONS_ABOUT, IDS_TRANSLATE_INFOBAR_OPTIONS_ABOUT); diff --git a/chrome/browser/translate/translate_infobars_delegates.cc b/chrome/browser/translate/translate_infobars_delegates.cc index 15d165b..3dba0ca 100644 --- a/chrome/browser/translate/translate_infobars_delegates.cc +++ b/chrome/browser/translate/translate_infobars_delegates.cc @@ -6,6 +6,8 @@ #include "app/l10n_util.h" #include "app/resource_bundle.h" +#include "chrome/browser/browser_process.h" +#include "chrome/browser/renderer_host/translation_service.h" #include "chrome/browser/tab_contents/tab_contents.h" #include "grit/generated_resources.h" #include "grit/theme_resources.h" @@ -30,61 +32,19 @@ void TranslateInfoBarDelegate::InfoBarClosed() { // TranslateInfoBarDelegate: public: ------------------------------------------- -void TranslateInfoBarDelegate::ModifyOriginalLanguage( - const std::string& original_language) { - original_language_ = original_language; +void TranslateInfoBarDelegate::ModifyOriginalLanguage(int lang_index) { + original_lang_index_ = lang_index; // TODO(kuan): Send stats to Google Translate that original language has been // modified. } -void TranslateInfoBarDelegate::ModifyTargetLanguage( - const std::string& target_language) { +void TranslateInfoBarDelegate::ModifyTargetLanguage(int lang_index) { NOTREACHED() << "Subclass should override"; } void TranslateInfoBarDelegate::GetAvailableOriginalLanguages( std::vector<std::string>* languages) { - // TODO(kuan): Call backend when it's ready; hardcode a list for now. - languages->push_back("Arabic"); - languages->push_back("Bengali"); - languages->push_back("Bulgarian"); - languages->push_back("Chinese (Simplified Han)"); - languages->push_back("Chinese (Traditional Han)"); - languages->push_back("Croatian"); - languages->push_back("Czech"); - languages->push_back("Danish"); - languages->push_back("Dutch"); - languages->push_back("English"); - languages->push_back("Estonian"); - languages->push_back("Filipino"); - languages->push_back("Finnish"); - languages->push_back("French"); - languages->push_back("German"); - languages->push_back("Greek"); - languages->push_back("Hebrew"); - languages->push_back("Hindi"); - languages->push_back("Hungarian"); - languages->push_back("Indonesian"); - languages->push_back("Italian"); - languages->push_back("Japanese"); - languages->push_back("Korean"); - languages->push_back("Latvian"); - languages->push_back("Lithuanian"); - languages->push_back("Norwegian"); - languages->push_back("Polish"); - languages->push_back("Portuguese"); - languages->push_back("Romanian"); - languages->push_back("Russian"); - languages->push_back("Serbian"); - languages->push_back("Slovak"); - languages->push_back("Slovenian"); - languages->push_back("Spanish"); - languages->push_back("Swedish"); - languages->push_back("Tamil"); - languages->push_back("Thai"); - languages->push_back("Turkish"); - languages->push_back("Ukrainian"); - languages->push_back("Vietnamese"); + TranslationService::GetSupportedLanguages(languages); } void TranslateInfoBarDelegate::GetAvailableTargetLanguages( @@ -93,7 +53,7 @@ void TranslateInfoBarDelegate::GetAvailableTargetLanguages( } void TranslateInfoBarDelegate::Translate() { - tab_contents_->TranslatePage(original_language_, target_language_); + tab_contents_->TranslatePage(original_lang_code(), target_lang_code()); } bool TranslateInfoBarDelegate::IsLanguageBlacklisted() { @@ -123,25 +83,46 @@ void TranslateInfoBarDelegate::ToggleAlwaysTranslate() { NOTREACHED() << "Subclass should override"; } +// TranslateInfoBarDelegate: static: ------------------------------------------- + +string16 TranslateInfoBarDelegate::GetDisplayNameForLocale( + const std::string& language_code) { + return l10n_util::GetDisplayNameForLocale( + language_code, g_browser_process->GetApplicationLocale(), true); +} + // TranslateInfoBarDelegate: protected: ---------------------------------------- TranslateInfoBarDelegate::TranslateInfoBarDelegate(TabContents* tab_contents, - PrefService* user_prefs, const std::string& original_language, - const std::string& target_language) + PrefService* user_prefs, const std::string& original_lang_code, + const std::string& target_lang_code) : InfoBarDelegate(tab_contents), tab_contents_(tab_contents), - original_language_(original_language), - target_language_(target_language), + original_lang_index_(0), + target_lang_index_(0), prefs_(user_prefs) { + TranslationService::GetSupportedLanguages(&supported_languages_); + for (size_t i = 0; i < supported_languages_.size(); ++i) { + if (original_lang_code == supported_languages_[i]) { + original_lang_index_ = i; + break; + } + } + for (size_t i = 0; i < supported_languages_.size(); ++i) { + if (target_lang_code == supported_languages_[i]) { + target_lang_index_ = i; + break; + } + } } // BeforeTranslateInfoBarDelegate: public: ------------------------------------- BeforeTranslateInfoBarDelegate::BeforeTranslateInfoBarDelegate( TabContents* tab_contents, PrefService* user_prefs, const GURL& url, - const std::string& original_language, const std::string& target_language) - : TranslateInfoBarDelegate(tab_contents, user_prefs, original_language, - target_language), + const std::string& original_lang_code, const std::string& target_lang_code) + : TranslateInfoBarDelegate(tab_contents, user_prefs, original_lang_code, + target_lang_code), site_(url.HostNoBrackets()), never_translate_language_(false), never_translate_site_(false) { @@ -149,16 +130,16 @@ BeforeTranslateInfoBarDelegate::BeforeTranslateInfoBarDelegate( bool BeforeTranslateInfoBarDelegate::IsLanguageBlacklisted() { never_translate_language_ = - prefs_.IsLanguageBlacklisted(original_language()); + prefs_.IsLanguageBlacklisted(original_lang_code()); return never_translate_language_; } void BeforeTranslateInfoBarDelegate::ToggleLanguageBlacklist() { never_translate_language_ = !never_translate_language_; if (never_translate_language_) - prefs_.BlacklistLanguage(original_language()); + prefs_.BlacklistLanguage(original_lang_code()); else - prefs_.RemoveLanguageFromBlacklist(original_language()); + prefs_.RemoveLanguageFromBlacklist(original_lang_code()); } bool BeforeTranslateInfoBarDelegate::IsSiteBlacklisted() { @@ -185,36 +166,34 @@ InfoBar* BeforeTranslateInfoBarDelegate::CreateInfoBar() { AfterTranslateInfoBarDelegate::AfterTranslateInfoBarDelegate( TabContents* tab_contents, PrefService* user_prefs, - const std::string& original_language, const std::string& target_language) - : TranslateInfoBarDelegate(tab_contents, user_prefs, original_language, - target_language), + const std::string& original_lang_code, const std::string& target_lang_code) + : TranslateInfoBarDelegate(tab_contents, user_prefs, original_lang_code, + target_lang_code), always_translate_(false) { } void AfterTranslateInfoBarDelegate::GetAvailableTargetLanguages( std::vector<std::string>* languages) { - // TODO(kuan): Call backend when it's ready; hardcode a list for now. - GetAvailableOriginalLanguages(languages); + TranslationService::GetSupportedLanguages(languages); } -void AfterTranslateInfoBarDelegate::ModifyTargetLanguage( - const std::string& target_language) { - target_language_ = target_language; +void AfterTranslateInfoBarDelegate::ModifyTargetLanguage(int lang_index) { + target_lang_index_ = lang_index; } bool AfterTranslateInfoBarDelegate::ShouldAlwaysTranslate() { - always_translate_ = prefs_.IsLanguagePairWhitelisted(original_language(), - target_language()); + always_translate_ = prefs_.IsLanguagePairWhitelisted(original_lang_code(), + target_lang_code()); return always_translate_; } void AfterTranslateInfoBarDelegate::ToggleAlwaysTranslate() { always_translate_ = !always_translate_; if (always_translate_) - prefs_.WhitelistLanguagePair(original_language(), target_language()); + prefs_.WhitelistLanguagePair(original_lang_code(), target_lang_code()); else - prefs_.RemoveLanguagePairFromWhitelist(original_language(), - target_language()); + prefs_.RemoveLanguagePairFromWhitelist(original_lang_code(), + target_lang_code()); } #if !defined(TOOLKIT_VIEWS) diff --git a/chrome/browser/translate/translate_infobars_delegates.h b/chrome/browser/translate/translate_infobars_delegates.h index 80bada0..18416c4 100644 --- a/chrome/browser/translate/translate_infobars_delegates.h +++ b/chrome/browser/translate/translate_infobars_delegates.h @@ -18,8 +18,8 @@ class TranslateInfoBarDelegate : public InfoBarDelegate { std::vector<std::string>* languages); virtual void GetAvailableTargetLanguages( std::vector<std::string>* languages); - virtual void ModifyOriginalLanguage(const std::string& original_language); - virtual void ModifyTargetLanguage(const std::string& target_language); + virtual void ModifyOriginalLanguage(int lang_index); + virtual void ModifyTargetLanguage(int lang_index); virtual void Translate(); virtual bool IsLanguageBlacklisted(); virtual void ToggleLanguageBlacklist(); @@ -28,11 +28,14 @@ class TranslateInfoBarDelegate : public InfoBarDelegate { virtual bool ShouldAlwaysTranslate(); virtual void ToggleAlwaysTranslate(); - const std::string& original_language() const { - return original_language_; + const std::string& original_lang_code() const { + return supported_languages_[original_lang_index_]; } - const std::string& target_language() const { - return target_language_; + const std::string& target_lang_code() const { + return supported_languages_[target_lang_index_]; + } + const std::string& GetLocaleFromIndex(int lang_index) const { + return supported_languages_[lang_index]; } TabContents* tab_contents() const { return tab_contents_; @@ -49,14 +52,19 @@ class TranslateInfoBarDelegate : public InfoBarDelegate { } virtual void InfoBarClosed(); + // Returns the printable version of the language code |language_code|. + static string16 GetDisplayNameForLocale(const std::string& language_code); + protected: TranslateInfoBarDelegate(TabContents* contents, PrefService* user_prefs, const std::string& original_language, const std::string& target_language); TabContents* tab_contents_; // Weak. - std::string original_language_; - std::string target_language_; + int original_lang_index_; + int target_lang_index_; TranslatePrefs prefs_; + // The list of languages supported. + std::vector<std::string> supported_languages_; DISALLOW_COPY_AND_ASSIGN(TranslateInfoBarDelegate); }; @@ -92,7 +100,7 @@ class AfterTranslateInfoBarDelegate : public TranslateInfoBarDelegate { // Overriden from TranslateInfoBar: virtual void GetAvailableTargetLanguages( std::vector<std::string>* languages); - virtual void ModifyTargetLanguage(const std::string& target_language); + virtual void ModifyTargetLanguage(int lang_index); virtual bool ShouldAlwaysTranslate(); virtual void ToggleAlwaysTranslate(); diff --git a/chrome/browser/views/infobars/translate_infobars.cc b/chrome/browser/views/infobars/translate_infobars.cc index e5edffa..c65bea4 100644 --- a/chrome/browser/views/infobars/translate_infobars.cc +++ b/chrome/browser/views/infobars/translate_infobars.cc @@ -204,8 +204,10 @@ TranslateInfoBar::TranslateInfoBar(TranslateInfoBarDelegate* delegate, label_3_ = NULL; } + string16 language_name = TranslateInfoBarDelegate::GetDisplayNameForLocale( + GetDelegate()->original_lang_code()); original_language_menu_button_ = CreateMenuButton(kMenuIDOriginalLanguage, - ASCIIToWide(GetDelegate()->original_language())); + UTF16ToWideHack(language_name)); AddChildView(original_language_menu_button_); options_menu_button_ = CreateMenuButton(kMenuIDOptions, @@ -214,7 +216,6 @@ TranslateInfoBar::TranslateInfoBar(TranslateInfoBarDelegate* delegate, } TranslateInfoBar::~TranslateInfoBar() { - } // TranslateInfoBar, views::View overrides: ------------------------------------ @@ -325,7 +326,7 @@ void TranslateInfoBar::RunMenu(views::View* source, const gfx::Point& pt) { if (menu_id == kMenuIDOptions) { if (!options_menu_model_.get()) { options_menu_model_.reset(new OptionsMenuModel(this, GetDelegate(), - before_translate_)); + before_translate_)); options_menu_menu_.reset(new views::Menu2(options_menu_model_.get())); } options_menu_menu_->RunMenuAt(pt, @@ -379,9 +380,8 @@ bool TranslateInfoBar::GetAcceleratorForCommandId(int command_id, void TranslateInfoBar::ExecuteCommand(int command_id) { if (command_id >= IDC_TRANSLATE_ORIGINAL_LANGUAGE_BASE) { - string16 new_language = original_language_menu_model_->GetLabelAt( + OnLanguageModified(original_language_menu_button_, command_id - IDC_TRANSLATE_ORIGINAL_LANGUAGE_BASE); - OnLanguageModified(original_language_menu_button_, new_language); } else { switch (command_id) { case IDC_TRANSLATE_OPTIONS_NEVER_TRANSLATE_LANG: @@ -417,7 +417,7 @@ void TranslateInfoBar::ExecuteCommand(int command_id) { // TranslateInfoBar, protected: ------------------------------------------------ views::MenuButton* TranslateInfoBar::CreateMenuButton(int menu_id, - const std::wstring& label) { + const std::wstring& label) { views::MenuButton* menu_button = new views::MenuButton(NULL, label, this, true); menu_button->SetID(menu_id); @@ -453,16 +453,18 @@ gfx::Point TranslateInfoBar::DetermineMenuPositionAndAlignment( } void TranslateInfoBar::OnLanguageModified(views::MenuButton* menu_button, - const string16& new_language) { + int new_language_index) { // Only proceed if a different language has been selected. + string16 new_language = TranslateInfoBarDelegate::GetDisplayNameForLocale( + GetDelegate()->GetLocaleFromIndex(new_language_index)); if (new_language == WideToUTF16(menu_button->text())) return; - std::string ascii_lang(UTF16ToUTF8(new_language)); if (menu_button == original_language_menu_button_) - GetDelegate()->ModifyOriginalLanguage(ascii_lang); + GetDelegate()->ModifyOriginalLanguage(new_language_index); else - GetDelegate()->ModifyTargetLanguage(ascii_lang); - menu_button->SetText(UTF16ToWide(new_language)); + GetDelegate()->ModifyTargetLanguage(new_language_index); + + menu_button->SetText(UTF16ToWideHack(new_language)); menu_button->ClearMaxTextSize(); menu_button->SizeToPreferredSize(); Layout(); @@ -539,8 +541,10 @@ void BeforeTranslateInfoBar::ButtonPressed( AfterTranslateInfoBar::AfterTranslateInfoBar( AfterTranslateInfoBarDelegate* delegate) : TranslateInfoBar(delegate, false, IDS_TRANSLATE_INFOBAR_AFTER_MESSAGE) { + string16 language_name = TranslateInfoBarDelegate::GetDisplayNameForLocale( + GetDelegate()->target_lang_code()); target_language_menu_button_ = CreateMenuButton(kMenuIDTargetLanguage, - ASCIIToWide(GetDelegate()->target_language())); + UTF16ToWideHack(language_name)); AddChildView(target_language_menu_button_); } @@ -578,9 +582,8 @@ void AfterTranslateInfoBar::RunMenu(views::View* source, const gfx::Point& pt) { void AfterTranslateInfoBar::ExecuteCommand(int command_id) { if (command_id >= IDC_TRANSLATE_TARGET_LANGUAGE_BASE) { - string16 new_language = target_language_menu_model_->GetLabelAt( + OnLanguageModified(target_language_menu_button_, command_id - IDC_TRANSLATE_TARGET_LANGUAGE_BASE); - OnLanguageModified(target_language_menu_button_, new_language); } else { TranslateInfoBar::ExecuteCommand(command_id); } diff --git a/chrome/browser/views/infobars/translate_infobars.h b/chrome/browser/views/infobars/translate_infobars.h index 1d0561e..ae355dd 100644 --- a/chrome/browser/views/infobars/translate_infobars.h +++ b/chrome/browser/views/infobars/translate_infobars.h @@ -49,7 +49,7 @@ class TranslateInfoBar : public InfoBar, gfx::Point DetermineMenuPositionAndAlignment(views::MenuButton* menu_button, views::Menu2::Alignment* alignment); void OnLanguageModified(views::MenuButton* menu_button, - const string16& new_language); + int new_language_index); TranslateInfoBarDelegate* GetDelegate() const; // Overridden from InfoBar: |