Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 9 additions & 0 deletions docs/doxygen/mainpages/const_cpp.h
Original file line number Diff line number Diff line change
Expand Up @@ -407,6 +407,15 @@ more details.
the applications using the library to disable implicit
conversions from and to <tt>const char*</tt> in wxString class.
Support for this option appeared in wxWidgets 3.1.4.}
@itemdef{wxNO_REQUIRE_LITERAL_MSGIDS,
this symbol is not defined by wxWidgets itself, but can be defined by
the applications using the library to allow variables as string arguments to
translation macros such as _() and wxPLURAL. The default since wxWidgets
3.3.0 is to allow only string literals.
Note that passing string variables as arguments to translation macros is
likely to be a bug, and does not produce the expected results. If you
feel you need to define this macro, you should first consider whether
your code is doing the right thing.}
@itemdef{WXMAKINGDLL_XXX,
used internally and defined when building the
library @c XXX as a DLL; when a monolithic wxWidgets build is used only a
Expand Down
104 changes: 89 additions & 15 deletions include/wx/translation.h
Original file line number Diff line number Diff line change
Expand Up @@ -45,30 +45,19 @@ using wxTranslationsHashMap = std::unordered_map<wxString, wxString>;
// --keyword="_" --keyword="wxPLURAL:1,2" options
// to extract the strings from the sources)
#ifndef WXINTL_NO_GETTEXT_MACRO
#ifndef wxNO_IMPLICIT_WXSTRING_ENCODING
#define _(s) wxGetTranslation((s))
#else
#define _(s) wxGetTranslation(wxASCII_STR(s))
#endif
#define _(s) wxUnderscoreWrapper((s))
#endif

#define wxPLURAL(sing, plur, n) wxGetTranslation((sing), (plur), n)
#define wxPLURAL(sing, plur, n) wxPluralWrapper((sing), (plur), n)

// wx-specific macro for translating strings in the given context: if you use
// them, you need to also add
// --keyword="wxGETTEXT_IN_CONTEXT:1c,2" --keyword="wxGETTEXT_IN_CONTEXT_PLURAL:1c,2,3"
// options to xgettext invocation.
#ifndef wxNO_IMPLICIT_WXSTRING_ENCODING
#define wxGETTEXT_IN_CONTEXT(c, s) \
wxGetTranslation((s), wxString(), c)
wxGettextInContextWrapper((c), (s))
#define wxGETTEXT_IN_CONTEXT_PLURAL(c, sing, plur, n) \
wxGetTranslation((sing), (plur), n, wxString(), c)
#else
#define wxGETTEXT_IN_CONTEXT(c, s) \
wxGetTranslation(wxASCII_STR(s), wxString(), wxASCII_STR(c))
#define wxGETTEXT_IN_CONTEXT_PLURAL(c, sing, plur, n) \
wxGetTranslation(wxASCII_STR(sing), wxASCII_STR(plur), n, wxString(), wxASCII_STR(c))
#endif
wxGettextInContextPluralWrapper((c), (sing), (plur), (n))

// another one which just marks the strings for extraction, but doesn't
// perform the translation (use -kwxTRANSLATE with xgettext!)
Expand Down Expand Up @@ -344,8 +333,93 @@ inline const wxString& wxGetTranslation(const char *str1,
wxString(context, conv));
}

#define wxTRANS_INPUT_STR(s) wxASCII_STR(s)
#else
#define wxTRANS_INPUT_STR(s) s
#endif // wxNO_IMPLICIT_WXSTRING_ENCODING

namespace wxTransImplStrict
{

// Wrapper functions that only accept string literals as arguments,
// not variables, not char* pointers.
template<size_t N>
const wxString& wxUnderscoreWrapper(const char (&msg)[N])
{
return wxGetTranslation(wxTRANS_INPUT_STR(msg));
}

template<size_t M, size_t N>
const wxString& wxPluralWrapper(const char (&msg)[M],
const char (&plural)[N],
int count)
{
return wxGetTranslation(wxTRANS_INPUT_STR(msg), wxTRANS_INPUT_STR(plural),
count);
}

template<size_t M, size_t N>
const wxString& wxGettextInContextWrapper(const char (&ctx)[M],
const char (&msg)[N])
{
return wxGetTranslation(wxTRANS_INPUT_STR(msg), wxString(),
wxTRANS_INPUT_STR(ctx));
}

template<size_t L, size_t M, size_t N>
const wxString& wxGettextInContextPluralWrapper(const char (&ctx)[L],
const char (&msg)[M],
const char (&plural)[N],
int count)
{
return wxGetTranslation(wxTRANS_INPUT_STR(msg), wxTRANS_INPUT_STR(plural),
count, wxString(), wxTRANS_INPUT_STR(ctx));
}

} // namespace wxTransImplStrict

namespace wxTransImplCompatible
{

// Wrapper functions that accept both string literals and variables
// as arguments.
inline const wxString& wxUnderscoreWrapper(const char *msg)
{
return wxGetTranslation(wxTRANS_INPUT_STR(msg));
}

inline const wxString& wxPluralWrapper(const char *msg,
const char *plural,
int count)
{
return wxGetTranslation(wxTRANS_INPUT_STR(msg), wxTRANS_INPUT_STR(plural),
count);
}

inline const wxString& wxGettextInContextWrapper(const char *ctx,
const char *msg)
{
return wxGetTranslation(wxTRANS_INPUT_STR(msg), wxString(),
wxTRANS_INPUT_STR(ctx));
}

inline const wxString& wxGettextInContextPluralWrapper(const char *ctx,
const char *msg,
const char *plural,
int count)
{
return wxGetTranslation(wxTRANS_INPUT_STR(msg), wxTRANS_INPUT_STR(plural),
count, wxString(), wxTRANS_INPUT_STR(ctx));
}

} // namespace wxTransImplCompatible

#ifdef wxNO_REQUIRE_LITERAL_MSGIDS
using namespace wxTransImplCompatible;
#else
using namespace wxTransImplStrict;
#endif

#else // !wxUSE_INTL

// the macros should still be defined - otherwise compilation would fail
Expand Down
13 changes: 10 additions & 3 deletions interface/wx/translation.h
Original file line number Diff line number Diff line change
Expand Up @@ -465,9 +465,11 @@ class wxMsgCatalog
///@{

/**
This macro is identical to _() but for the plural variant of
This macro is similar to _() but for the plural variant of
wxGetTranslation().

The string arguments must be @em string @em literals.

@return A const wxString.

@header{wx/intl.h}
Expand All @@ -479,6 +481,8 @@ class wxMsgCatalog

See the description of @c context argument of wxGetTranslation().

The arguments must be @em string @em literals.

@see wxGETTEXT_IN_CONTEXT_PLURAL()

@since 3.1.1
Expand All @@ -488,6 +492,8 @@ class wxMsgCatalog
/**
Similar to wxPLURAL() but translates the string in the given context.

The string arguments must be @em string @em literals.

See the description of @c context argument of wxGetTranslation().

@see wxGETTEXT_IN_CONTEXT()
Expand Down Expand Up @@ -567,7 +573,8 @@ class wxMsgCatalog
If @a domain is specified then only that domain/catalog is searched for a
matching string. As this function is used very often, an alternative (and
also common in Unix world) syntax is provided: the _() macro is defined to
do the same thing as wxGetTranslation().
do nearly the same thing as wxGetTranslation(), with the exception that
the argument to _() must be a string literal.

If @a context is not empty (notice that this argument is only available
starting from wxWidgets 3.1.1), item translation is looked up in the
Expand Down Expand Up @@ -633,7 +640,7 @@ const wxString& wxGetTranslation(const wxString& string,

@header{wx/intl.h}
*/
const wxString& _(const wxString& string);
#define _(string)

///@}

Loading