From 7e61cd0dfbc81948a2534654de3b2e868669d747 Mon Sep 17 00:00:00 2001 From: Charles Kerr Date: Tue, 27 Aug 2019 09:57:12 -0500 Subject: [PATCH] fix: i18n of gtk msgbox buttons (#19904) * fix: i18n of gtk msgbox buttons similar to #19756 (12df0e8) but for messageboxes * refactor: DRY the gtk+ button mnemonics * fix: don't compile gtk_util on non-Linux platforms rename from `gtk_util.[cc,h]` to `util_gtk.[cc,h]` so that it gets picked up by the `extra_source_filters` rule in `BUILD.gn`. * fix: make linter happy It really shows that I cannot build locally atm... :P --- filenames.gni | 2 ++ shell/browser/ui/file_dialog_gtk.cc | 32 +++++-------------------- shell/browser/ui/message_box_gtk.cc | 11 ++++----- shell/browser/ui/util_gtk.cc | 36 +++++++++++++++++++++++++++++ shell/browser/ui/util_gtk.h | 21 +++++++++++++++++ 5 files changed, 70 insertions(+), 32 deletions(-) create mode 100644 shell/browser/ui/util_gtk.cc create mode 100644 shell/browser/ui/util_gtk.h diff --git a/filenames.gni b/filenames.gni index 6519c1748739..6e051fc8d3f0 100644 --- a/filenames.gni +++ b/filenames.gni @@ -326,6 +326,8 @@ filenames = { "shell/browser/ui/file_dialog_gtk.cc", "shell/browser/ui/file_dialog_mac.mm", "shell/browser/ui/file_dialog_win.cc", + "shell/browser/ui/util_gtk.cc", + "shell/browser/ui/util_gtk.h", "shell/browser/ui/inspectable_web_contents.cc", "shell/browser/ui/inspectable_web_contents.h", "shell/browser/ui/inspectable_web_contents_delegate.h", diff --git a/shell/browser/ui/file_dialog_gtk.cc b/shell/browser/ui/file_dialog_gtk.cc index 5f9d8bbe28f5..87d63846d079 100644 --- a/shell/browser/ui/file_dialog_gtk.cc +++ b/shell/browser/ui/file_dialog_gtk.cc @@ -3,6 +3,7 @@ // found in the LICENSE file. #include "shell/browser/ui/file_dialog.h" +#include "shell/browser/ui/util_gtk.h" #include "base/callback.h" #include "base/files/file_util.h" @@ -21,27 +22,6 @@ DialogSettings::~DialogSettings() = default; namespace { -// Copied from L40-L55 in -// https://cs.chromium.org/chromium/src/chrome/browser/ui/libgtkui/select_file_dialog_impl_gtk.cc -#if GTK_CHECK_VERSION(3, 90, 0) -// GTK stock items have been deprecated. The docs say to switch to using the -// strings "_Open", etc. However this breaks i18n. We could supply our own -// internationalized strings, but the "_" in these strings is significant: it's -// the keyboard shortcut to select these actions. TODO(thomasanderson): Provide -// internationalized strings when GTK provides support for it. -const char kOkLabel[] = "_Ok"; -const char kCancelLabel[] = "_Cancel"; -const char kOpenLabel[] = "_Open"; -const char kSaveLabel[] = "_Save"; -#else -G_GNUC_BEGIN_IGNORE_DEPRECATIONS -const char* const kOkLabel = GTK_STOCK_OK; -const char* const kCancelLabel = GTK_STOCK_CANCEL; -const char* const kOpenLabel = GTK_STOCK_OPEN; -const char* const kSaveLabel = GTK_STOCK_SAVE; -G_GNUC_END_IGNORE_DEPRECATIONS -#endif - static const int kPreviewWidth = 256; static const int kPreviewHeight = 512; @@ -66,18 +46,18 @@ class FileChooserDialog { : parent_( static_cast(settings.parent_window)), filters_(settings.filters) { - const char* confirm_text = kOkLabel; + const char* confirm_text = gtk_util::kOkLabel; if (!settings.button_label.empty()) confirm_text = settings.button_label.c_str(); else if (action == GTK_FILE_CHOOSER_ACTION_SAVE) - confirm_text = kOpenLabel; + confirm_text = gtk_util::kOpenLabel; else if (action == GTK_FILE_CHOOSER_ACTION_OPEN) - confirm_text = kSaveLabel; + confirm_text = gtk_util::kSaveLabel; dialog_ = gtk_file_chooser_dialog_new( - settings.title.c_str(), NULL, action, kCancelLabel, GTK_RESPONSE_CANCEL, - confirm_text, GTK_RESPONSE_ACCEPT, NULL); + settings.title.c_str(), NULL, action, gtk_util::kCancelLabel, + GTK_RESPONSE_CANCEL, confirm_text, GTK_RESPONSE_ACCEPT, NULL); if (parent_) { parent_->SetEnabled(false); libgtkui::SetGtkTransientForAura(dialog_, parent_->GetNativeWindow()); diff --git a/shell/browser/ui/message_box_gtk.cc b/shell/browser/ui/message_box_gtk.cc index 6160ea90650e..c3480cb7ada2 100644 --- a/shell/browser/ui/message_box_gtk.cc +++ b/shell/browser/ui/message_box_gtk.cc @@ -3,8 +3,7 @@ // found in the LICENSE file. #include "shell/browser/ui/message_box.h" - -#include +#include "shell/browser/ui/util_gtk.h" #include "base/callback.h" #include "base/strings/string_util.h" @@ -123,13 +122,13 @@ class GtkMessageBox : public NativeWindowObserver { const char* TranslateToStock(int id, const std::string& text) { const std::string lower = base::ToLowerASCII(text); if (lower == "cancel") - return _("_Cancel"); + return gtk_util::kCancelLabel; if (lower == "no") - return _("_No"); + return gtk_util::kNoLabel; if (lower == "ok") - return _("_OK"); + return gtk_util::kOkLabel; if (lower == "yes") - return _("_Yes"); + return gtk_util::kYesLabel; return text.c_str(); } diff --git a/shell/browser/ui/util_gtk.cc b/shell/browser/ui/util_gtk.cc new file mode 100644 index 000000000000..dfe9231eee45 --- /dev/null +++ b/shell/browser/ui/util_gtk.cc @@ -0,0 +1,36 @@ +// Copyright (c) 2019 GitHub, Inc. +// Use of this source code is governed by the MIT license that can be +// found in the LICENSE file. + +#include "shell/browser/ui/util_gtk.h" + +#include + +namespace gtk_util { + +// Copied from L40-L55 in +// https://cs.chromium.org/chromium/src/chrome/browser/ui/libgtkui/select_file_dialog_impl_gtk.cc +#if GTK_CHECK_VERSION(3, 90, 0) +// GTK stock items have been deprecated. The docs say to switch to using the +// strings "_Open", etc. However this breaks i18n. We could supply our own +// internationalized strings, but the "_" in these strings is significant: it's +// the keyboard shortcut to select these actions. TODO: Provide +// internationalized strings when GTK provides support for it. +const char* const kCancelLabel = "_Cancel"; +const char* const kNoLabel = "_No"; +const char* const kOkLabel = "_OK"; +const char* const kOpenLabel = "_Open"; +const char* const kSaveLabel = "_Save"; +const char* const kYesLabel = "_Yes"; +#else +G_GNUC_BEGIN_IGNORE_DEPRECATIONS +const char* const kCancelLabel = GTK_STOCK_CANCEL; +const char* const kNoLabel = GTK_STOCK_NO; +const char* const kOkLabel = GTK_STOCK_OK; +const char* const kOpenLabel = GTK_STOCK_OPEN; +const char* const kSaveLabel = GTK_STOCK_SAVE; +const char* const kYesLabel = GTK_STOCK_YES; +G_GNUC_END_IGNORE_DEPRECATIONS +#endif + +} // namespace gtk_util diff --git a/shell/browser/ui/util_gtk.h b/shell/browser/ui/util_gtk.h new file mode 100644 index 000000000000..62909aa7a125 --- /dev/null +++ b/shell/browser/ui/util_gtk.h @@ -0,0 +1,21 @@ +// Copyright (c) 2019 GitHub, Inc. +// Use of this source code is governed by the MIT license that can be +// found in the LICENSE file. + +#ifndef SHELL_BROWSER_UI_UTIL_GTK_H_ +#define SHELL_BROWSER_UI_UTIL_GTK_H_ + +namespace gtk_util { + +/* These are `const char*` rather than the project-preferred `const char[]` + because they must fit the type of an external dependency */ +extern const char* const kCancelLabel; +extern const char* const kNoLabel; +extern const char* const kOkLabel; +extern const char* const kOpenLabel; +extern const char* const kSaveLabel; +extern const char* const kYesLabel; + +} // namespace gtk_util + +#endif // SHELL_BROWSER_UI_UTIL_GTK_H_