From 9fa1a4dacb8748ffe75ec9ed2754f9f207b3e124 Mon Sep 17 00:00:00 2001 From: Lauri Nurmi Date: Fri, 10 Jan 2025 15:25:45 +0200 Subject: [PATCH 1/3] Also accept wide string literals as translation macro arguments Not accepting them was an undeliberate regression introduced in PR #24957. There weren't any such calls in wx's own tests or samples, so now adding some to the internat sample to prevent a similar regression in the future. --- include/wx/translation.h | 28 ++++++++++++++-------------- samples/internat/internat.cpp | 6 ++++++ 2 files changed, 20 insertions(+), 14 deletions(-) diff --git a/include/wx/translation.h b/include/wx/translation.h index 9c6e42d32db7..a03b9d245157 100644 --- a/include/wx/translation.h +++ b/include/wx/translation.h @@ -341,35 +341,35 @@ inline const wxString& wxGetTranslation(const char *str1, namespace wxTransImplStrict { -// Wrapper functions that only accept string literals as arguments, -// not variables, not char* pointers. -template -const wxString& wxUnderscoreWrapper(const char (&msg)[N]) +// Wrapper functions that only accept string literals (regular or wide) as +// arguments, not variables, not char* pointers, not wchar_t* pointers. +template +const wxString& wxUnderscoreWrapper(const T (&msg)[N]) { return wxGetTranslation(wxTRANS_INPUT_STR(msg)); } -template -const wxString& wxPluralWrapper(const char (&msg)[M], - const char (&plural)[N], +template +const wxString& wxPluralWrapper(const T (&msg)[M], + const T (&plural)[N], int count) { return wxGetTranslation(wxTRANS_INPUT_STR(msg), wxTRANS_INPUT_STR(plural), count); } -template -const wxString& wxGettextInContextWrapper(const char (&ctx)[M], - const char (&msg)[N]) +template +const wxString& wxGettextInContextWrapper(const T (&ctx)[M], + const T (&msg)[N]) { return wxGetTranslation(wxTRANS_INPUT_STR(msg), wxString(), wxTRANS_INPUT_STR(ctx)); } -template -const wxString& wxGettextInContextPluralWrapper(const char (&ctx)[L], - const char (&msg)[M], - const char (&plural)[N], +template +const wxString& wxGettextInContextPluralWrapper(const T (&ctx)[L], + const T (&msg)[M], + const T (&plural)[N], int count) { return wxGetTranslation(wxTRANS_INPUT_STR(msg), wxTRANS_INPUT_STR(plural), diff --git a/samples/internat/internat.cpp b/samples/internat/internat.cpp index c574fb3e0691..6e5d30b976d2 100644 --- a/samples/internat/internat.cpp +++ b/samples/internat/internat.cpp @@ -320,6 +320,12 @@ MyFrame::MyFrame() macro_menu->Append(INTERNAT_MACRO_8, wxGETTEXT_IN_CONTEXT_PLURAL("context_2", "sing", "plur", 1)); macro_menu->Append(INTERNAT_MACRO_9, wxGETTEXT_IN_CONTEXT_PLURAL("context_2", "sing", "plur", 2)); + // Also wide strings can be used: + _(L"item"); + wxGETTEXT_IN_CONTEXT(L"context", L"item"); + wxPLURAL(L"sing", L"plur", 3); + wxGETTEXT_IN_CONTEXT_PLURAL(L"context", L"sing", L"plur", 3); + wxMenu *help_menu = new wxMenu; #ifdef USE_COREUTILS_MO help_menu->Append(wxID_HELP, _("Show coreutils &help")); From e9ae3e67782968c1065e42a588011a55277b9601 Mon Sep 17 00:00:00 2001 From: Lauri Nurmi Date: Sat, 11 Jan 2025 21:03:09 +0200 Subject: [PATCH 2/3] fixup! Also accept wide string literals as translation macro arguments --- samples/internat/internat.cpp | 6 ------ tests/intl/intltest.cpp | 10 ++++++++++ 2 files changed, 10 insertions(+), 6 deletions(-) diff --git a/samples/internat/internat.cpp b/samples/internat/internat.cpp index 6e5d30b976d2..c574fb3e0691 100644 --- a/samples/internat/internat.cpp +++ b/samples/internat/internat.cpp @@ -320,12 +320,6 @@ MyFrame::MyFrame() macro_menu->Append(INTERNAT_MACRO_8, wxGETTEXT_IN_CONTEXT_PLURAL("context_2", "sing", "plur", 1)); macro_menu->Append(INTERNAT_MACRO_9, wxGETTEXT_IN_CONTEXT_PLURAL("context_2", "sing", "plur", 2)); - // Also wide strings can be used: - _(L"item"); - wxGETTEXT_IN_CONTEXT(L"context", L"item"); - wxPLURAL(L"sing", L"plur", 3); - wxGETTEXT_IN_CONTEXT_PLURAL(L"context", L"sing", L"plur", 3); - wxMenu *help_menu = new wxMenu; #ifdef USE_COREUTILS_MO help_menu->Append(wxID_HELP, _("Show coreutils &help")); diff --git a/tests/intl/intltest.cpp b/tests/intl/intltest.cpp index 611344bc7bd9..6a6a647a7962 100644 --- a/tests/intl/intltest.cpp +++ b/tests/intl/intltest.cpp @@ -796,4 +796,14 @@ TEST_CASE("wxUILocale::ShowSystem", "[.]") WARN("Preferred UI languages:\n" << preferredLangsStr); } +// Not an actual test, but a way to ensure that wide string versions +// of translation macros are available and successfully compile. +static void EnsureWideStringVersionsOfMacrosExist() { + // Also wide strings can be used: + _(L"item"); + wxGETTEXT_IN_CONTEXT(L"context", L"item"); + wxPLURAL(L"sing", L"plur", 3); + wxGETTEXT_IN_CONTEXT_PLURAL(L"context", L"sing", L"plur", 3); +} + #endif // wxUSE_INTL From 723b359eecb2c8aa3c2b7e66a819bb0c8feff1ed Mon Sep 17 00:00:00 2001 From: Vadim Zeitlin Date: Sat, 11 Jan 2025 23:15:08 +0100 Subject: [PATCH 3/3] fixup! Also accept wide string literals as translation macro arguments --- include/wx/translation.h | 14 ++++++++++++-- tests/allheaders.cpp | 6 ++++++ tests/intl/intltest.cpp | 10 ---------- 3 files changed, 18 insertions(+), 12 deletions(-) diff --git a/include/wx/translation.h b/include/wx/translation.h index a03b9d245157..1ea68f660615 100644 --- a/include/wx/translation.h +++ b/include/wx/translation.h @@ -333,8 +333,18 @@ inline const wxString& wxGetTranslation(const char *str1, wxString(context, conv)); } - #define wxTRANS_INPUT_STR(s) wxASCII_STR(s) -#else +// We can't construct wxString implicitly in this case, so use a helper. +inline wxString wxTRANS_INPUT_STR(const char* s) +{ + return wxString::FromAscii(s); +} + +inline wxString wxTRANS_INPUT_STR(const wchar_t* s) +{ + return wxString(s); +} +#else // !wxNO_IMPLICIT_WXSTRING_ENCODING + // We can rely on implicit conversion, so don't bother with the helper. #define wxTRANS_INPUT_STR(s) s #endif // wxNO_IMPLICIT_WXSTRING_ENCODING diff --git a/tests/allheaders.cpp b/tests/allheaders.cpp index b59e4cd91f7b..4cec55633d03 100644 --- a/tests/allheaders.cpp +++ b/tests/allheaders.cpp @@ -421,4 +421,10 @@ TEST_CASE("wxNO_IMPLICIT_WXSTRING_ENCODING", "[string]") wxPLURAL("singular", "plural", 2); wxGETTEXT_IN_CONTEXT("context", "text"); wxGETTEXT_IN_CONTEXT_PLURAL("context", "singular", "plural", 3); + + // Also wide strings can be used: + _(L"item"); + wxGETTEXT_IN_CONTEXT(L"context", L"item"); + wxPLURAL(L"sing", L"plur", 3); + wxGETTEXT_IN_CONTEXT_PLURAL(L"context", L"sing", L"plur", 3); } diff --git a/tests/intl/intltest.cpp b/tests/intl/intltest.cpp index 6a6a647a7962..611344bc7bd9 100644 --- a/tests/intl/intltest.cpp +++ b/tests/intl/intltest.cpp @@ -796,14 +796,4 @@ TEST_CASE("wxUILocale::ShowSystem", "[.]") WARN("Preferred UI languages:\n" << preferredLangsStr); } -// Not an actual test, but a way to ensure that wide string versions -// of translation macros are available and successfully compile. -static void EnsureWideStringVersionsOfMacrosExist() { - // Also wide strings can be used: - _(L"item"); - wxGETTEXT_IN_CONTEXT(L"context", L"item"); - wxPLURAL(L"sing", L"plur", 3); - wxGETTEXT_IN_CONTEXT_PLURAL(L"context", L"sing", L"plur", 3); -} - #endif // wxUSE_INTL