Skip to content

Commit 0039e60

Browse files
ckerrzcbenz
authored andcommitted
fix: i18n of gtk msgbox buttons (electron#20007)
* fix: i18n of gtk msgbox buttons Manually backport electron#19904. See that PR for details. * fix: make linter happy * fix: make linter happy
1 parent 26176eb commit 0039e60

File tree

5 files changed

+72
-32
lines changed

5 files changed

+72
-32
lines changed

atom/browser/ui/file_dialog_gtk.cc

Lines changed: 6 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
// found in the LICENSE file.
44

55
#include "atom/browser/ui/file_dialog.h"
6+
#include "atom/browser/ui/util_gtk.h"
67

78
#include "atom/browser/native_window_views.h"
89
#include "atom/browser/unresponsive_suppressor.h"
@@ -21,27 +22,6 @@ DialogSettings::~DialogSettings() = default;
2122

2223
namespace {
2324

24-
// Copied from L40-L55 in
25-
// https://cs.chromium.org/chromium/src/chrome/browser/ui/libgtkui/select_file_dialog_impl_gtk.cc
26-
#if GTK_CHECK_VERSION(3, 90, 0)
27-
// GTK stock items have been deprecated. The docs say to switch to using the
28-
// strings "_Open", etc. However this breaks i18n. We could supply our own
29-
// internationalized strings, but the "_" in these strings is significant: it's
30-
// the keyboard shortcut to select these actions. TODO(thomasanderson): Provide
31-
// internationalized strings when GTK provides support for it.
32-
const char kCancelLabel[] = "_Cancel";
33-
const char kOkLabel[] = "_OK";
34-
const char kOpenLabel[] = "_Open";
35-
const char kSaveLabel[] = "_Save";
36-
#else
37-
G_GNUC_BEGIN_IGNORE_DEPRECATIONS
38-
const char* const kCancelLabel = GTK_STOCK_CANCEL;
39-
const char* const kOkLabel = GTK_STOCK_OK;
40-
const char* const kOpenLabel = GTK_STOCK_OPEN;
41-
const char* const kSaveLabel = GTK_STOCK_SAVE;
42-
G_GNUC_END_IGNORE_DEPRECATIONS
43-
#endif
44-
4525
static const int kPreviewWidth = 256;
4626
static const int kPreviewHeight = 512;
4727

@@ -65,18 +45,18 @@ class FileChooserDialog {
6545
FileChooserDialog(GtkFileChooserAction action, const DialogSettings& settings)
6646
: parent_(static_cast<atom::NativeWindowViews*>(settings.parent_window)),
6747
filters_(settings.filters) {
68-
const char* confirm_text = kOkLabel;
48+
const char* confirm_text = util_gtk::kOkLabel;
6949

7050
if (!settings.button_label.empty())
7151
confirm_text = settings.button_label.c_str();
7252
else if (action == GTK_FILE_CHOOSER_ACTION_SAVE)
73-
confirm_text = kSaveLabel;
53+
confirm_text = util_gtk::kSaveLabel;
7454
else if (action == GTK_FILE_CHOOSER_ACTION_OPEN)
75-
confirm_text = kOpenLabel;
55+
confirm_text = util_gtk::kOpenLabel;
7656

7757
dialog_ = gtk_file_chooser_dialog_new(
78-
settings.title.c_str(), NULL, action, kCancelLabel, GTK_RESPONSE_CANCEL,
79-
confirm_text, GTK_RESPONSE_ACCEPT, NULL);
58+
settings.title.c_str(), NULL, action, util_gtk::kCancelLabel,
59+
GTK_RESPONSE_CANCEL, confirm_text, GTK_RESPONSE_ACCEPT, NULL);
8060
if (parent_) {
8161
parent_->SetEnabled(false);
8262
libgtkui::SetGtkTransientForAura(dialog_, parent_->GetNativeWindow());

atom/browser/ui/message_box_gtk.cc

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
#include "atom/browser/browser.h"
1010
#include "atom/browser/native_window_observer.h"
1111
#include "atom/browser/native_window_views.h"
12+
#include "atom/browser/ui/util_gtk.h"
1213
#include "atom/browser/unresponsive_suppressor.h"
1314
#include "base/callback.h"
1415
#include "base/strings/string_util.h"
@@ -127,13 +128,13 @@ class GtkMessageBox : public NativeWindowObserver {
127128
const char* TranslateToStock(int id, const std::string& text) {
128129
const std::string lower = base::ToLowerASCII(text);
129130
if (lower == "cancel")
130-
return _("_Cancel");
131+
return util_gtk::kCancelLabel;
131132
if (lower == "no")
132-
return _("_No");
133+
return util_gtk::kNoLabel;
133134
if (lower == "ok")
134-
return _("_OK");
135+
return util_gtk::kOkLabel;
135136
if (lower == "yes")
136-
return _("_Yes");
137+
return util_gtk::kYesLabel;
137138
return text.c_str();
138139
}
139140

@@ -235,8 +236,8 @@ void ShowMessageBox(NativeWindow* parent,
235236

236237
void ShowErrorBox(const base::string16& title, const base::string16& content) {
237238
if (Browser::Get()->is_ready()) {
238-
GtkMessageBox(nullptr, MESSAGE_BOX_TYPE_ERROR, {"OK"}, -1, 0, "Error",
239-
base::UTF16ToUTF8(title).c_str(),
239+
GtkMessageBox(nullptr, MESSAGE_BOX_TYPE_ERROR, {util_gtk::kOkLabel}, -1, 0,
240+
"Error", base::UTF16ToUTF8(title).c_str(),
240241
base::UTF16ToUTF8(content).c_str(), "", false,
241242
gfx::ImageSkia())
242243
.RunSynchronous();

atom/browser/ui/util_gtk.cc

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
// Copyright (c) 2019 GitHub, Inc.
2+
// Use of this source code is governed by the MIT license that can be
3+
// found in the LICENSE file.
4+
5+
#include "atom/browser/ui/util_gtk.h"
6+
7+
#include <gtk/gtk.h>
8+
9+
namespace util_gtk {
10+
11+
// Copied from L40-L55 in
12+
// https://cs.chromium.org/chromium/src/chrome/browser/ui/libgtkui/select_file_dialog_impl_gtk.cc
13+
#if GTK_CHECK_VERSION(3, 90, 0)
14+
// GTK stock items have been deprecated. The docs say to switch to using the
15+
// strings "_Open", etc. However this breaks i18n. We could supply our own
16+
// internationalized strings, but the "_" in these strings is significant: it's
17+
// the keyboard shortcut to select these actions. TODO: Provide
18+
// internationalized strings when GTK provides support for it.
19+
const char* const kCancelLabel = "_Cancel";
20+
const char* const kNoLabel = "_No";
21+
const char* const kOkLabel = "_OK";
22+
const char* const kOpenLabel = "_Open";
23+
const char* const kSaveLabel = "_Save";
24+
const char* const kYesLabel = "_Yes";
25+
#else
26+
G_GNUC_BEGIN_IGNORE_DEPRECATIONS
27+
const char* const kCancelLabel = GTK_STOCK_CANCEL;
28+
const char* const kNoLabel = GTK_STOCK_NO;
29+
const char* const kOkLabel = GTK_STOCK_OK;
30+
const char* const kOpenLabel = GTK_STOCK_OPEN;
31+
const char* const kSaveLabel = GTK_STOCK_SAVE;
32+
const char* const kYesLabel = GTK_STOCK_YES;
33+
G_GNUC_END_IGNORE_DEPRECATIONS
34+
#endif
35+
36+
} // namespace util_gtk

atom/browser/ui/util_gtk.h

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
// Copyright (c) 2019 GitHub, Inc.
2+
// Use of this source code is governed by the MIT license that can be
3+
// found in the LICENSE file.
4+
5+
#ifndef ATOM_BROWSER_UI_UTIL_GTK_H_
6+
#define ATOM_BROWSER_UI_UTIL_GTK_H_
7+
8+
namespace util_gtk {
9+
10+
/* These are `const char*` rather than the project-preferred `const char[]`
11+
because they must fit the type of an external dependency */
12+
extern const char* const kCancelLabel;
13+
extern const char* const kNoLabel;
14+
extern const char* const kOkLabel;
15+
extern const char* const kOpenLabel;
16+
extern const char* const kSaveLabel;
17+
extern const char* const kYesLabel;
18+
19+
} // namespace util_gtk
20+
21+
#endif // ATOM_BROWSER_UI_UTIL_GTK_H_

filenames.gni

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -468,6 +468,8 @@ filenames = {
468468
"atom/browser/ui/tray_icon_cocoa.mm",
469469
"atom/browser/ui/tray_icon_observer.h",
470470
"atom/browser/ui/tray_icon_win.cc",
471+
"atom/browser/ui/util_gtk.cc",
472+
"atom/browser/ui/util_gtk.h",
471473
"atom/browser/ui/views/atom_views_delegate.cc",
472474
"atom/browser/ui/views/atom_views_delegate.h",
473475
"atom/browser/ui/views/autofill_popup_view.cc",

0 commit comments

Comments
 (0)