From 2e59325fed2416cd23d0b418cd46e302148aa584 Mon Sep 17 00:00:00 2001 From: "jeremy@chromium.org" Date: Wed, 17 Feb 2010 07:49:53 +0000 Subject: Translate: Move label parsing logic into common code. * Add a string16 & Cocoa version of GetStringF(). * Move logic for parsing translate infobar label into translate_infobars_delegates. * Make views variant of translate infobars use new code. BUG=34466 TEST=Translate infobars should continue to workon Windows. Review URL: http://codereview.chromium.org/597042 git-svn-id: svn://svn.chromium.org/chrome/trunk/src@39206 0039d316-1c4b-4281-b951-d872f2087c98 --- app/l10n_util.cc | 7 ++++++ app/l10n_util.h | 4 ++++ .../translate/translate_infobars_delegates.cc | 28 ++++++++++++++++++++++ .../translate/translate_infobars_delegates.h | 15 ++++++++++++ .../browser/views/infobars/translate_infobars.cc | 23 ++++-------------- 5 files changed, 59 insertions(+), 18 deletions(-) diff --git a/app/l10n_util.cc b/app/l10n_util.cc index 1feb09d..8d418d2 100644 --- a/app/l10n_util.cc +++ b/app/l10n_util.cc @@ -705,6 +705,13 @@ std::wstring GetStringF(int message_id, string16(), string16(), offsets)); } +string16 GetStringFUTF16(int message_id, + const string16& a, + const string16& b, + std::vector* offsets) { + return GetStringF(message_id, a, b, string16(), string16(), offsets); +} + std::wstring GetStringF(int message_id, int a) { return GetStringF(message_id, IntToWString(a)); } diff --git a/app/l10n_util.h b/app/l10n_util.h index 8ed7596..66b9d23 100644 --- a/app/l10n_util.h +++ b/app/l10n_util.h @@ -155,6 +155,10 @@ std::wstring GetStringF(int message_id, const std::wstring& a, const std::wstring& b, std::vector* offsets); +string16 GetStringFUTF16(int message_id, + const string16& a, + const string16& b, + std::vector* offsets); // Convenience formatters for a single number. std::wstring GetStringF(int message_id, int a); diff --git a/chrome/browser/translate/translate_infobars_delegates.cc b/chrome/browser/translate/translate_infobars_delegates.cc index ed86b6c..06d7da8 100644 --- a/chrome/browser/translate/translate_infobars_delegates.cc +++ b/chrome/browser/translate/translate_infobars_delegates.cc @@ -158,6 +158,34 @@ void TranslateInfoBarDelegate::ToggleAlwaysTranslate() { } } +void TranslateInfoBarDelegate::GetMessageText(string16 *message_text, + std::vector *offsets, bool *swapped_language_placeholders) { + *swapped_language_placeholders = false; + offsets->clear(); + + std::vector offsets_tmp; + int message_resource_id = IDS_TRANSLATE_INFOBAR_BEFORE_MESSAGE; + if (state() == kAfterTranslate) + message_resource_id = IDS_TRANSLATE_INFOBAR_AFTER_MESSAGE; + *message_text = l10n_util::GetStringFUTF16(message_resource_id, + string16(), string16(), &offsets_tmp); + + if (offsets_tmp.empty() || offsets_tmp.size() > 2) { + NOTREACHED() << "Invalid no. of placeholders in label."; + return; + } + // Sort the offsets if necessary. + if (offsets_tmp.size() == 2 && offsets_tmp[0] > offsets_tmp[1]) { + size_t offset0 = offsets_tmp[0]; + offsets_tmp[0] = offsets_tmp[1]; + offsets_tmp[1] = offset0; + *swapped_language_placeholders = true; + } + if (offsets_tmp[offsets_tmp.size() - 1] != message_text->length()) + offsets_tmp.push_back(message_text->length()); + *offsets = offsets_tmp; +} + // TranslateInfoBarDelegate: static: ------------------------------------------- string16 TranslateInfoBarDelegate::GetDisplayNameForLocale( diff --git a/chrome/browser/translate/translate_infobars_delegates.h b/chrome/browser/translate/translate_infobars_delegates.h index 311b847..ceeda17 100644 --- a/chrome/browser/translate/translate_infobars_delegates.h +++ b/chrome/browser/translate/translate_infobars_delegates.h @@ -60,6 +60,21 @@ class TranslateInfoBarDelegate : public InfoBarDelegate { return state_; } + // Retrieve the text for the toolbar label. The toolbar label is a bit + // strange since we need to place popup menus inside the string in question. + // To do this we use two placeholders. + // + // |message_text| is the text to display for the label. + // |offsets| contains the offsets of the number of placeholders in the text + // + message_text->length() i.e. it can contain 2 or 3 elements. + // offsets[0] < offsets[1] even in cases where the languages need to be + // displayed in reverse order. + // |swapped_language_placeholders| is true if we need to flip the order + // of the menus in the current locale. + void GetMessageText(string16 *message_text, + std::vector *offsets, + bool *swapped_language_placeholders); + // Overridden from InfoBarDelegate. virtual Type GetInfoBarType() { return PAGE_ACTION_TYPE; diff --git a/chrome/browser/views/infobars/translate_infobars.cc b/chrome/browser/views/infobars/translate_infobars.cc index 9c2c175..d9660d2 100644 --- a/chrome/browser/views/infobars/translate_infobars.cc +++ b/chrome/browser/views/infobars/translate_infobars.cc @@ -623,24 +623,11 @@ void TranslateInfoBar::Observe(NotificationType type, void TranslateInfoBar::CreateLabels() { // Determine text for labels. std::vector offsets; - std::wstring message_text = l10n_util::GetStringF( - (GetDelegate()->state() == TranslateInfoBarDelegate::kAfterTranslate ? - IDS_TRANSLATE_INFOBAR_AFTER_MESSAGE : - IDS_TRANSLATE_INFOBAR_BEFORE_MESSAGE), - std::wstring(), std::wstring(), &offsets); - if (!offsets.empty() && offsets.size() <= 2) { - // Sort the offsets if necessary. - if (offsets.size() == 2 && offsets[0] > offsets[1]) { - size_t offset0 = offsets[0]; - offsets[0] = offsets[1]; - offsets[1] = offset0; - swapped_language_placeholders_ = true; - } - if (offsets[offsets.size() - 1] != message_text.length()) - offsets.push_back(message_text.length()); - } else { - NOTREACHED() << "Invalid no. of placeholders in label."; - } + string16 message_text_utf16; + GetDelegate()->GetMessageText(&message_text_utf16, &offsets, + &swapped_language_placeholders_); + + std::wstring message_text = UTF16ToWideHack(message_text_utf16); // Create label controls. const gfx::Font& font = ResourceBundle::GetSharedInstance().GetFont( -- cgit v1.1