Make it possible to tell wxXmlResource which domain to pull

translatable strings from.

Make the wxLocale::GetString methods virtual so they can be overridden
in derived classes.


git-svn-id: https://svn.wxwidgets.org/svn/wx/wxWidgets/trunk@40728 c3d73ce0-8a6f-49c7-b76d-6d57e0e08775
This commit is contained in:
Robin Dunn
2006-08-21 18:27:15 +00:00
parent 4e9080bc8e
commit d4a724d407
13 changed files with 260 additions and 33 deletions

View File

@@ -1536,15 +1536,19 @@ This function is deprecated, use \helpref{wxString}{wxstring} class instead.
\membersection{::wxGetTranslation}\label{wxgettranslation} \membersection{::wxGetTranslation}\label{wxgettranslation}
\func{const char *}{wxGetTranslation}{\param{const char * }{str}} \func{const wxChar *}{wxGetTranslation}{\param{const wxChar* }{str},
\param{const wxChar* }{domain = NULL}}
\func{const char *}{wxGetTranslation}{\param{const char * }{str}, \param{const char * }{strPlural}, \param{size\_t }{n}} \func{const wxChar *}{wxGetTranslation}{\param{const wxChar* }{str}, \param{const wxChar* }{strPlural}, \param{size\_t }{n},
\param{const wxChar* }{domain = NULL}}}
This function returns the translation of string {\it str} in the current This function returns the translation of string {\it str} in the current
\helpref{locale}{wxlocale}. If the string is not found in any of the loaded \helpref{locale}{wxlocale}. If the string is not found in any of the loaded
message catalogs (see \helpref{internationalization overview}{internationalization}), the message catalogs (see \helpref{internationalization overview}{internationalization}), the
original string is returned. In debug build, an error message is logged -- this original string is returned. In debug build, an error message is logged -- this
should help to find the strings which were not yet translated. As this function should help to find the strings which were not yet translated. If
{\it 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 is used very often, an alternative (and also common in Unix world) syntax is
provided: the \helpref{\_()}{underscore} macro is defined to do the same thing provided: the \helpref{\_()}{underscore} macro is defined to do the same thing
as wxGetTranslation. as wxGetTranslation.

View File

@@ -35,7 +35,9 @@ enum wxXmlResourceFlags
\membersection{wxXmlResource::wxXmlResource}\label{wxxmlresourcector} \membersection{wxXmlResource::wxXmlResource}\label{wxxmlresourcector}
\func{}{wxXmlResource}{\param{const wxString\& }{filemask}, \param{int }{flags = wxXRC\_USE\_LOCALE}} \func{}{wxXmlResource}{\param{const wxString\& }{filemask},
\param{int }{flags = wxXRC\_USE\_LOCALE},
\param{const wxString& }{domain = wxEmptyString}}
Constructor. Constructor.
@@ -46,7 +48,13 @@ load all resource files inside a zip archive.}
wxXRC\_NO\_SUBCLASSING: subclass property of object nodes will be ignored wxXRC\_NO\_SUBCLASSING: subclass property of object nodes will be ignored
(useful for previews in XRC editors).} (useful for previews in XRC editors).}
\func{}{wxXmlResource}{\param{int }{flags = wxXRC\_USE\_LOCALE}} \docparam{domain}{The name of the gettext catalog to search for
translatable strings. By default all loaded catalogs will be
searched. This provides a way to allow the strings to only come
from a specific catalog.}
\func{}{wxXmlResource}{\param{int }{flags = wxXRC\_USE\_LOCALE},
\param{const wxString& }{domain = wxEmptyString}}
Constructor. Constructor.
@@ -56,6 +64,11 @@ wxXRC\_NO\_SUBCLASSING: subclass property of object nodes will be ignored
XRC files from being reloaded from disk in case they have been modified there XRC files from being reloaded from disk in case they have been modified there
since being last loaded (may slightly speed up loading them).} since being last loaded (may slightly speed up loading them).}
\docparam{domain}{The name of the gettext catalog to search for
translatable strings. By default all loaded catalogs will be
searched. This provides a way to allow the strings to only come
from a specific catalog.}
\membersection{wxXmlResource::\destruct{wxXmlResource}}\label{wxxmlresourcedtor} \membersection{wxXmlResource::\destruct{wxXmlResource}}\label{wxxmlresourcedtor}
@@ -254,3 +267,19 @@ This function unloads a resource previously loaded by
Returns \true if the resource was successfully unloaded and \false if it hasn't Returns \true if the resource was successfully unloaded and \false if it hasn't
been found in the list of loaded resources. been found in the list of loaded resources.
\membersection{wxXmlResource::GetDomain}\label{wxxmlresourcegetdomain}
\func{wxChar*}{GetDomain}{}
Returns the domain (message catalog) that will be used to load
translatable strings in the XRC.
\membersection{wxXmlResource::SetDomain}\label{wxxmlresourcesetdomain}
\func{wxChar*}{SetDomain}{\param{const wxChar* }{domain}}
Sets the domain (message catalog) that will be used to load
translatable strings in the XRC.

View File

@@ -402,7 +402,7 @@ public:
int flags = wxLOCALE_LOAD_DEFAULT | wxLOCALE_CONV_ENCODING); int flags = wxLOCALE_LOAD_DEFAULT | wxLOCALE_CONV_ENCODING);
// restores old locale // restores old locale
~wxLocale(); virtual ~wxLocale();
// Try to get user's (or OS's) preferred language setting. // Try to get user's (or OS's) preferred language setting.
// Return wxLANGUAGE_UNKNOWN if language-guessing algorithm failed // Return wxLANGUAGE_UNKNOWN if language-guessing algorithm failed
@@ -490,13 +490,13 @@ public:
// //
// domains are searched in the last to first order, i.e. catalogs // domains are searched in the last to first order, i.e. catalogs
// added later override those added before. // added later override those added before.
const wxChar *GetString(const wxChar *szOrigString, virtual const wxChar *GetString(const wxChar *szOrigString,
const wxChar *szDomain = NULL) const; const wxChar *szDomain = NULL) const;
// plural form version of the same: // plural form version of the same:
const wxChar *GetString(const wxChar *szOrigString, virtual const wxChar *GetString(const wxChar *szOrigString,
const wxChar *szOrigString2, const wxChar *szOrigString2,
size_t n, size_t n,
const wxChar *szDomain = NULL) const; const wxChar *szDomain = NULL) const;
// Returns the current short name for the locale // Returns the current short name for the locale
const wxString& GetName() const { return m_strShort; } const wxString& GetName() const { return m_strShort; }
@@ -548,20 +548,20 @@ private:
extern WXDLLIMPEXP_BASE wxLocale* wxGetLocale(); extern WXDLLIMPEXP_BASE wxLocale* wxGetLocale();
// get the translation of the string in the current locale // get the translation of the string in the current locale
inline const wxChar *wxGetTranslation(const wxChar *sz) inline const wxChar *wxGetTranslation(const wxChar *sz, const wxChar* domain=NULL)
{ {
wxLocale *pLoc = wxGetLocale(); wxLocale *pLoc = wxGetLocale();
if (pLoc) if (pLoc)
return pLoc->GetString(sz); return pLoc->GetString(sz, domain);
else else
return sz; return sz;
} }
inline const wxChar *wxGetTranslation(const wxChar *sz1, const wxChar *sz2, inline const wxChar *wxGetTranslation(const wxChar *sz1, const wxChar *sz2,
size_t n) size_t n, const wxChar* domain=NULL)
{ {
wxLocale *pLoc = wxGetLocale(); wxLocale *pLoc = wxGetLocale();
if (pLoc) if (pLoc)
return pLoc->GetString(sz1, sz2, n); return pLoc->GetString(sz1, sz2, n, domain);
else else
return n == 1 ? sz1 : sz2; return n == 1 ? sz1 : sz2;
} }

View File

@@ -104,21 +104,25 @@ public:
// Constructor. // Constructor.
// Flags: wxXRC_USE_LOCALE // Flags: wxXRC_USE_LOCALE
// translatable strings will be translated via _() // translatable strings will be translated via _()
// using the given domain if specified
// wxXRC_NO_SUBCLASSING // wxXRC_NO_SUBCLASSING
// subclass property of object nodes will be ignored // subclass property of object nodes will be ignored
// (useful for previews in XRC editors) // (useful for previews in XRC editors)
// wxXRC_NO_RELOADING // wxXRC_NO_RELOADING
// don't check the modification time of the XRC files and // don't check the modification time of the XRC files and
// reload them if they have changed on disk // reload them if they have changed on disk
wxXmlResource(int flags = wxXRC_USE_LOCALE); wxXmlResource(int flags = wxXRC_USE_LOCALE,
const wxString& domain=wxEmptyString);
// Constructor. // Constructor.
// Flags: wxXRC_USE_LOCALE // Flags: wxXRC_USE_LOCALE
// translatable strings will be translated via _() // translatable strings will be translated via _()
// using the given domain if specified
// wxXRC_NO_SUBCLASSING // wxXRC_NO_SUBCLASSING
// subclass property of object nodes will be ignored // subclass property of object nodes will be ignored
// (useful for previews in XRC editors) // (useful for previews in XRC editors)
wxXmlResource(const wxString& filemask, int flags = wxXRC_USE_LOCALE); wxXmlResource(const wxString& filemask, int flags = wxXRC_USE_LOCALE,
const wxString& domain=wxEmptyString);
// Destructor. // Destructor.
~wxXmlResource(); ~wxXmlResource();
@@ -242,6 +246,10 @@ public:
// Set flags after construction. // Set flags after construction.
void SetFlags(int flags) { m_flags = flags; } void SetFlags(int flags) { m_flags = flags; }
// Get/Set the domain to be passed to the translation functions, defaults to NULL.
wxChar* GetDomain() const { return m_domain; }
void SetDomain(const wxChar* domain);
protected: protected:
// Scans the resources list for unloaded files and loads them. Also reloads // Scans the resources list for unloaded files and loads them. Also reloads
// files that have been modified since last loading. // files that have been modified since last loading.
@@ -281,6 +289,9 @@ private:
wxFileSystem& GetCurFileSystem() { return m_curFileSystem; } wxFileSystem& GetCurFileSystem() { return m_curFileSystem; }
#endif #endif
// domain to pass to translation functions, if any.
wxChar* m_domain;
friend class wxXmlResourceHandler; friend class wxXmlResourceHandler;
friend class wxXmlResourceModule; friend class wxXmlResourceModule;

View File

@@ -113,7 +113,7 @@ wxObject *wxCheckListBoxXmlHandler::DoCreateResource()
// add to the list // add to the list
wxString str = GetNodeContent(m_node); wxString str = GetNodeContent(m_node);
if (m_resource->GetFlags() & wxXRC_USE_LOCALE) if (m_resource->GetFlags() & wxXRC_USE_LOCALE)
str = wxGetTranslation(str); str = wxGetTranslation(str, m_resource->GetDomain());
strList.Add(str); strList.Add(str);
return NULL; return NULL;
} }

View File

@@ -82,7 +82,7 @@ wxObject *wxChoiceXmlHandler::DoCreateResource()
// add to the list // add to the list
wxString str = GetNodeContent(m_node); wxString str = GetNodeContent(m_node);
if (m_resource->GetFlags() & wxXRC_USE_LOCALE) if (m_resource->GetFlags() & wxXRC_USE_LOCALE)
str = wxGetTranslation(str); str = wxGetTranslation(str, m_resource->GetDomain());
strList.Add(str); strList.Add(str);
return NULL; return NULL;

View File

@@ -87,7 +87,7 @@ wxObject *wxComboBoxXmlHandler::DoCreateResource()
// add to the list // add to the list
wxString str = GetNodeContent(m_node); wxString str = GetNodeContent(m_node);
if (m_resource->GetFlags() & wxXRC_USE_LOCALE) if (m_resource->GetFlags() & wxXRC_USE_LOCALE)
str = wxGetTranslation(str); str = wxGetTranslation(str, m_resource->GetDomain());
strList.Add(str); strList.Add(str);
return NULL; return NULL;

View File

@@ -88,7 +88,7 @@ wxObject *wxListBoxXmlHandler::DoCreateResource()
// add to the list // add to the list
wxString str = GetNodeContent(m_node); wxString str = GetNodeContent(m_node);
if (m_resource->GetFlags() & wxXRC_USE_LOCALE) if (m_resource->GetFlags() & wxXRC_USE_LOCALE)
str = wxGetTranslation(str); str = wxGetTranslation(str, m_resource->GetDomain());
strList.Add(str); strList.Add(str);
return NULL; return NULL;

View File

@@ -97,7 +97,7 @@ wxObject *wxOwnerDrawnComboBoxXmlHandler::DoCreateResource()
// add to the list // add to the list
wxString str = GetNodeContent(m_node); wxString str = GetNodeContent(m_node);
if (m_resource->GetFlags() & wxXRC_USE_LOCALE) if (m_resource->GetFlags() & wxXRC_USE_LOCALE)
str = wxGetTranslation(str); str = wxGetTranslation(str, m_resource->GetDomain());
strList.Add(str); strList.Add(str);
return NULL; return NULL;

View File

@@ -116,11 +116,11 @@ wxObject *wxRadioBoxXmlHandler::DoCreateResource()
if (m_resource->GetFlags() & wxXRC_USE_LOCALE) if (m_resource->GetFlags() & wxXRC_USE_LOCALE)
{ {
str = wxGetTranslation(str); str = wxGetTranslation(str, m_resource->GetDomain());
if ( !tooltip.empty() ) if ( !tooltip.empty() )
tooltip = wxGetTranslation(tooltip); tooltip = wxGetTranslation(tooltip, m_resource->GetDomain());
if ( hasHelptext ) if ( hasHelptext )
helptext = wxGetTranslation(helptext); helptext = wxGetTranslation(helptext, m_resource->GetDomain());
} }
labels.push_back(str); labels.push_back(str);

View File

@@ -65,24 +65,41 @@ wxXmlResource *wxXmlResource::ms_instance = NULL;
return old; return old;
} }
wxXmlResource::wxXmlResource(int flags) wxXmlResource::wxXmlResource(int flags, const wxString& domain)
{ {
m_flags = flags; m_flags = flags;
m_version = -1; m_version = -1;
m_domain = NULL;
if (! domain.empty() )
SetDomain(domain);
} }
wxXmlResource::wxXmlResource(const wxString& filemask, int flags) wxXmlResource::wxXmlResource(const wxString& filemask, int flags, const wxString& domain)
{ {
m_flags = flags; m_flags = flags;
m_version = -1; m_version = -1;
m_domain = NULL;
if (! domain.empty() )
SetDomain(domain);
Load(filemask); Load(filemask);
} }
wxXmlResource::~wxXmlResource() wxXmlResource::~wxXmlResource()
{ {
if (m_domain)
free(m_domain);
ClearHandlers(); ClearHandlers();
} }
void wxXmlResource::SetDomain(const wxChar* domain)
{
if (m_domain)
free(m_domain);
m_domain = NULL;
if (domain && wxStrlen(domain))
m_domain = wxStrdup(domain);
}
/* static */ /* static */
wxString wxXmlResource::ConvertFileNameToURL(const wxString& filename) wxString wxXmlResource::ConvertFileNameToURL(const wxString& filename)
@@ -916,7 +933,7 @@ wxString wxXmlResourceHandler::GetText(const wxString& param, bool translate)
if (translate && parNode && if (translate && parNode &&
parNode->GetPropVal(wxT("translate"), wxEmptyString) != wxT("0")) parNode->GetPropVal(wxT("translate"), wxEmptyString) != wxT("0"))
{ {
return wxGetTranslation(str2); return wxGetTranslation(str2, m_resource->GetDomain());
} }
else else
{ {

View File

@@ -461,7 +461,163 @@ public:
}; };
//---------------------------------------------------------------------------
%{
class wxPyLocale : public wxLocale
{
public:
wxPyLocale();
wxPyLocale(const wxChar *szName, // name (for messages)
const wxChar *szShort = (const wxChar *) NULL, // dir prefix (for msg files)
const wxChar *szLocale = (const wxChar *) NULL, // locale (for setlocale)
bool bLoadDefault = true, // preload wxstd.mo?
bool bConvertEncoding = false); // convert Win<->Unix if necessary?
wxPyLocale(int language, // wxLanguage id or custom language
int flags = wxLOCALE_LOAD_DEFAULT | wxLOCALE_CONV_ENCODING);
~wxPyLocale();
virtual const wxChar *GetString(const wxChar *szOrigString,
const wxChar *szDomain = NULL) const;
virtual const wxChar *GetString(const wxChar *szOrigString,
const wxChar *szOrigString2, size_t n,
const wxChar *szDomain = NULL) const;
virtual wxChar *GetSingularString(const wxChar *szOrigString,
const wxChar *szDomain = NULL) const;
virtual wxChar *GetPluralString(const wxChar *szOrigString,
const wxChar *szOrigString2, size_t n,
const wxChar *szDomain = NULL) const;
PYPRIVATE;
private:
DECLARE_NO_COPY_CLASS(wxPyLocale)
};
wxPyLocale::wxPyLocale() : wxLocale()
{
}
wxPyLocale::wxPyLocale(const wxChar *szName, // name (for messages)
const wxChar *szShort, // dir prefix (for msg files)
const wxChar *szLocale, // locale (for setlocale)
bool bLoadDefault, // preload wxstd.mo?
bool bConvertEncoding) // convert Win<->Unix if necessary?
: wxLocale(szName, szShort, szLocale, bLoadDefault, bConvertEncoding)
{
}
wxPyLocale::wxPyLocale(int language, // wxLanguage id or custom language
int flags) : wxLocale(language, flags)
{
}
wxPyLocale::~wxPyLocale()
{
}
const wxChar *wxPyLocale::GetString(const wxChar *szOrigString,
const wxChar *szDomain) const
{
wxChar *str = GetSingularString(szOrigString, szDomain);
return (str != NULL) ? str : wxLocale::GetString(szOrigString, szDomain);
}
const wxChar *wxPyLocale::GetString(const wxChar *szOrigString,
const wxChar *szOrigString2, size_t n,
const wxChar *szDomain) const
{
wxChar *str = GetPluralString(szOrigString, szOrigString2, n, szDomain);
return (str != NULL) ? str : wxLocale::GetString(szOrigString, szOrigString2, n, szDomain);
}
wxChar *wxPyLocale::GetSingularString(const wxChar *szOrigString,
const wxChar *szDomain) const
{
bool found;
static wxString str;
str = _T("error in translation"); // when the first if condition is true but the second if condition is not we do not want to return the previously queried string.
wxPyBlock_t blocked = wxPyBeginBlockThreads();
if((found=wxPyCBH_findCallback(m_myInst, "GetSingularString"))) {
PyObject* param1 = wx2PyString(szOrigString);
PyObject* param2 = wx2PyString(szDomain);
PyObject* ret = wxPyCBH_callCallbackObj(m_myInst, Py_BuildValue("(OO)", param1, param2));
Py_DECREF(param1);
Py_DECREF(param2);
if (ret) {
str = Py2wxString(ret);
Py_DECREF(ret);
}
}
wxPyEndBlockThreads(blocked);
return (found ? (wxChar*)str.c_str() : NULL);
}
wxChar *wxPyLocale::GetPluralString(const wxChar *szOrigString,
const wxChar *szOrigString2, size_t n,
const wxChar *szDomain) const
{
bool found;
static wxString str;
str = _T("error in translation"); // when the first if condition is true but the second if condition is not we do not want to return the previously queried string.
wxPyBlock_t blocked = wxPyBeginBlockThreads();
if((found=wxPyCBH_findCallback(m_myInst, "GetPluralString"))) {
PyObject* param1 = wx2PyString(szOrigString);
PyObject* param2 = wx2PyString(szOrigString2);
PyObject* param4 = wx2PyString(szDomain);
PyObject* ret = wxPyCBH_callCallbackObj(m_myInst, Py_BuildValue("(OOiO)", param1, param2, (int)n, param4));
Py_DECREF(param1);
Py_DECREF(param2);
Py_DECREF(param4);
if( ret) {
str = Py2wxString(ret);
Py_DECREF(ret);
}
}
wxPyEndBlockThreads(blocked);
return (found ? (wxChar*)str.c_str() : NULL);
}
%}
class wxPyLocale : public wxLocale
{
public:
%pythonAppend wxPyLocale "self._setCallbackInfo(self, PyLocale)"
// ctor & dtor
// -----------
%extend {
wxPyLocale(int language = -1,
int flags = wxLOCALE_LOAD_DEFAULT | wxLOCALE_CONV_ENCODING) {
wxPyLocale* loc;
if (language == -1)
loc = new wxPyLocale();
else
loc = new wxPyLocale(language, flags);
// Python before 2.4 needs to have LC_NUMERIC set to "C" in order
// for the floating point conversions and such to work right.
%#if PY_VERSION_HEX < 0x02040000
setlocale(LC_NUMERIC, "C");
%#endif
return loc;
}
}
~wxPyLocale();
void _setCallbackInfo(PyObject* self, PyObject* _class);
virtual const wxChar *GetSingularString(const wxChar *szOrigString,
const wxChar *szDomain = NULL) const;
virtual const wxChar *GetPluralString(const wxChar *szOrigString,
const wxChar *szOrigString2, size_t n,
const wxChar *szDomain = NULL) const;
};
//---------------------------------------------------------------------------
// get the current locale object (note that it may be NULL!) // get the current locale object (note that it may be NULL!)
wxLocale* wxGetLocale(); wxLocale* wxGetLocale();
@@ -469,7 +625,9 @@ wxLocale* wxGetLocale();
// get the translation of the string in the current locale // get the translation of the string in the current locale
%nokwargs wxGetTranslation; %nokwargs wxGetTranslation;
wxString wxGetTranslation(const wxString& str); wxString wxGetTranslation(const wxString& str);
wxString wxGetTranslation(const wxString& str, const wxString& domain);
wxString wxGetTranslation(const wxString& str, const wxString& strPlural, size_t n); wxString wxGetTranslation(const wxString& str, const wxString& strPlural, size_t n);
wxString wxGetTranslation(const wxString& str, const wxString& strPlural, size_t n, const wxString& domain);
//--------------------------------------------------------------------------- //---------------------------------------------------------------------------
%newgroup %newgroup

View File

@@ -45,8 +45,10 @@ class wxXmlResource : public wxObject
{ {
public: public:
%pythonAppend wxXmlResource(const wxString& filemask, int flags) "self.InitAllHandlers()" %pythonAppend wxXmlResource(const wxString& filemask, int flags,
%pythonAppend wxXmlResource(int flags) "val.InitAllHandlers()" const wxString& domain=wxEmptyString) "self.InitAllHandlers()"
%pythonAppend wxXmlResource(int flags,
const wxString& domain=wxEmptyString) "val.InitAllHandlers()"
// Ctors. // Ctors.
// Flags: wxXRC_USE_LOCALE // Flags: wxXRC_USE_LOCALE
@@ -54,8 +56,11 @@ public:
// wxXRC_NO_SUBCLASSING // wxXRC_NO_SUBCLASSING
// subclass property of object nodes will be ignored // subclass property of object nodes will be ignored
// (useful for previews in XRC editors) // (useful for previews in XRC editors)
wxXmlResource(const wxString& filemask, int flags = wxXRC_USE_LOCALE); wxXmlResource(const wxString& filemask, int flags = wxXRC_USE_LOCALE,
%RenameCtor(EmptyXmlResource, wxXmlResource(int flags = wxXRC_USE_LOCALE)); const wxString& domain=wxEmptyString);
%RenameCtor(EmptyXmlResource, wxXmlResource(int flags = wxXRC_USE_LOCALE,
const wxString& domain=wxEmptyString));
~wxXmlResource(); ~wxXmlResource();
@@ -195,6 +200,9 @@ public:
// Set flags after construction. // Set flags after construction.
void SetFlags(int flags) { m_flags = flags; } void SetFlags(int flags) { m_flags = flags; }
// Get/Set the domain to be passed to the translation functions, defaults to NULL.
wxString GetDomain() const;
void SetDomain(const wxString& domain);
}; };
//---------------------------------------------------------------------- //----------------------------------------------------------------------