Reuse the same XRC function for all translations
Translate all strings in the new GetNodeText() function replacing the old GetText() which was mostly used for translatable strings before -- except that <item> tag contents didn't use it because it also performed string unescaping, not wanted for the control items, in addition to translation. Replace the old GetText() (while still keeping it for compatibility, i.e. to avoid breaking any custom XRC handlers using it) with the new function which is more flexible and can be used for all tags. No real changes, this is just a refactoring.
This commit is contained in:
@@ -513,7 +513,10 @@ public:
|
|||||||
// - replaces \n, \r, \t by respective chars (according to C syntax)
|
// - replaces \n, \r, \t by respective chars (according to C syntax)
|
||||||
// - replaces _ by & and __ by _ (needed for _File => &File because of XML)
|
// - replaces _ by & and __ by _ (needed for _File => &File because of XML)
|
||||||
// - calls wxGetTranslations (unless disabled in wxXmlResource)
|
// - calls wxGetTranslations (unless disabled in wxXmlResource)
|
||||||
wxString GetText(const wxString& param, bool translate = true) wxOVERRIDE;
|
//
|
||||||
|
// The first two conversions can be disabled by using wxXRC_TEXT_NO_ESCAPE
|
||||||
|
// in flags and the last one -- by using wxXRC_TEXT_NO_TRANSLATE.
|
||||||
|
wxString GetNodeText(const wxXmlNode *node, int flags = 0) wxOVERRIDE;
|
||||||
|
|
||||||
// Returns the XRCID.
|
// Returns the XRCID.
|
||||||
int GetID() wxOVERRIDE;
|
int GetID() wxOVERRIDE;
|
||||||
|
@@ -32,6 +32,13 @@ class WXDLLIMPEXP_FWD_CORE wxXmlResourceHandler;
|
|||||||
// by wxXmlResourceHandler implementation itself.
|
// by wxXmlResourceHandler implementation itself.
|
||||||
#define XRC_ADD_STYLE(style) AddStyle(wxT(#style), style)
|
#define XRC_ADD_STYLE(style) AddStyle(wxT(#style), style)
|
||||||
|
|
||||||
|
// Flags for GetNodeText().
|
||||||
|
enum
|
||||||
|
{
|
||||||
|
wxXRC_TEXT_NO_TRANSLATE = 1,
|
||||||
|
wxXRC_TEXT_NO_ESCAPE = 2
|
||||||
|
};
|
||||||
|
|
||||||
// Abstract base class for the implementation object used by
|
// Abstract base class for the implementation object used by
|
||||||
// wxXmlResourceHandlerImpl. The real implementation is in
|
// wxXmlResourceHandlerImpl. The real implementation is in
|
||||||
// wxXmlResourceHandlerImpl class in the "xrc" library while this class is in
|
// wxXmlResourceHandlerImpl class in the "xrc" library while this class is in
|
||||||
@@ -61,7 +68,7 @@ public:
|
|||||||
virtual wxString GetParamValue(const wxString& param) = 0;
|
virtual wxString GetParamValue(const wxString& param) = 0;
|
||||||
virtual wxString GetParamValue(const wxXmlNode* node) = 0;
|
virtual wxString GetParamValue(const wxXmlNode* node) = 0;
|
||||||
virtual int GetStyle(const wxString& param = wxT("style"), int defaults = 0) = 0;
|
virtual int GetStyle(const wxString& param = wxT("style"), int defaults = 0) = 0;
|
||||||
virtual wxString GetText(const wxString& param, bool translate = true) = 0;
|
virtual wxString GetNodeText(const wxXmlNode *node, int flags = 0) = 0;
|
||||||
virtual int GetID() = 0;
|
virtual int GetID() = 0;
|
||||||
virtual wxString GetName() = 0;
|
virtual wxString GetName() = 0;
|
||||||
virtual bool GetBool(const wxString& param, bool defaultv = false) = 0;
|
virtual bool GetBool(const wxString& param, bool defaultv = false) = 0;
|
||||||
@@ -254,9 +261,14 @@ protected:
|
|||||||
{
|
{
|
||||||
return GetImpl()->GetStyle(param, defaults);
|
return GetImpl()->GetStyle(param, defaults);
|
||||||
}
|
}
|
||||||
|
wxString GetNodeText(const wxXmlNode *node, int flags = 0)
|
||||||
|
{
|
||||||
|
return GetImpl()->GetNodeText(node, flags);
|
||||||
|
}
|
||||||
wxString GetText(const wxString& param, bool translate = true)
|
wxString GetText(const wxString& param, bool translate = true)
|
||||||
{
|
{
|
||||||
return GetImpl()->GetText(param, translate);
|
return GetImpl()->GetNodeText(GetImpl()->GetParamNode(param),
|
||||||
|
translate ? 0 : wxXRC_TEXT_NO_TRANSLATE);
|
||||||
}
|
}
|
||||||
int GetID() const
|
int GetID() const
|
||||||
{
|
{
|
||||||
|
@@ -94,10 +94,7 @@ wxObject *wxCheckListBoxXmlHandler::DoCreateResource()
|
|||||||
// handle <item checked="boolean">Label</item>
|
// handle <item checked="boolean">Label</item>
|
||||||
|
|
||||||
// add to the list
|
// add to the list
|
||||||
wxString str = GetNodeContent(m_node);
|
strList.Add(GetNodeText(m_node, wxXRC_TEXT_NO_ESCAPE));
|
||||||
if (m_resource->GetFlags() & wxXRC_USE_LOCALE)
|
|
||||||
str = wxGetTranslation(str, m_resource->GetDomain());
|
|
||||||
strList.Add(str);
|
|
||||||
return NULL;
|
return NULL;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@@ -70,10 +70,7 @@ wxObject *wxChoiceXmlHandler::DoCreateResource()
|
|||||||
// handle <item>Label</item>
|
// handle <item>Label</item>
|
||||||
|
|
||||||
// add to the list
|
// add to the list
|
||||||
wxString str = GetNodeContent(m_node);
|
strList.Add(GetNodeText(m_node, wxXRC_TEXT_NO_ESCAPE));
|
||||||
if (m_resource->GetFlags() & wxXRC_USE_LOCALE)
|
|
||||||
str = wxGetTranslation(str, m_resource->GetDomain());
|
|
||||||
strList.Add(str);
|
|
||||||
|
|
||||||
return NULL;
|
return NULL;
|
||||||
}
|
}
|
||||||
|
@@ -77,10 +77,7 @@ wxObject *wxComboBoxXmlHandler::DoCreateResource()
|
|||||||
// handle <item>Label</item>
|
// handle <item>Label</item>
|
||||||
|
|
||||||
// add to the list
|
// add to the list
|
||||||
wxString str = GetNodeContent(m_node);
|
strList.Add(GetNodeText(m_node, wxXRC_TEXT_NO_ESCAPE));
|
||||||
if (m_resource->GetFlags() & wxXRC_USE_LOCALE)
|
|
||||||
str = wxGetTranslation(str, m_resource->GetDomain());
|
|
||||||
strList.Add(str);
|
|
||||||
|
|
||||||
return NULL;
|
return NULL;
|
||||||
}
|
}
|
||||||
|
@@ -99,10 +99,7 @@ wxObject *wxEditableListBoxXmlHandler::DoCreateResource()
|
|||||||
}
|
}
|
||||||
else if ( m_insideBox && m_node->GetName() == EDITLBOX_ITEM_NAME )
|
else if ( m_insideBox && m_node->GetName() == EDITLBOX_ITEM_NAME )
|
||||||
{
|
{
|
||||||
wxString str = GetNodeContent(m_node);
|
m_items.push_back(GetNodeText(m_node, wxXRC_TEXT_NO_ESCAPE));
|
||||||
if ( m_resource->GetFlags() & wxXRC_USE_LOCALE )
|
|
||||||
str = wxGetTranslation(str, m_resource->GetDomain());
|
|
||||||
m_items.push_back(str);
|
|
||||||
|
|
||||||
return NULL;
|
return NULL;
|
||||||
}
|
}
|
||||||
|
@@ -69,10 +69,7 @@ wxObject *wxSimpleHtmlListBoxXmlHandler::DoCreateResource()
|
|||||||
// handle <item>Label</item>
|
// handle <item>Label</item>
|
||||||
|
|
||||||
// add to the list
|
// add to the list
|
||||||
wxString str = GetNodeContent(m_node);
|
strList.Add(GetNodeText(m_node, wxXRC_TEXT_NO_ESCAPE));
|
||||||
if (m_resource->GetFlags() & wxXRC_USE_LOCALE)
|
|
||||||
str = wxGetTranslation(str, m_resource->GetDomain());
|
|
||||||
strList.Add(str);
|
|
||||||
|
|
||||||
return NULL;
|
return NULL;
|
||||||
}
|
}
|
||||||
|
@@ -77,10 +77,7 @@ wxObject *wxListBoxXmlHandler::DoCreateResource()
|
|||||||
// handle <item>Label</item>
|
// handle <item>Label</item>
|
||||||
|
|
||||||
// add to the list
|
// add to the list
|
||||||
wxString str = GetNodeContent(m_node);
|
strList.Add(GetNodeText(m_node, wxXRC_TEXT_NO_ESCAPE));
|
||||||
if (m_resource->GetFlags() & wxXRC_USE_LOCALE)
|
|
||||||
str = wxGetTranslation(str, m_resource->GetDomain());
|
|
||||||
strList.Add(str);
|
|
||||||
|
|
||||||
return NULL;
|
return NULL;
|
||||||
}
|
}
|
||||||
|
@@ -85,10 +85,7 @@ wxObject *wxOwnerDrawnComboBoxXmlHandler::DoCreateResource()
|
|||||||
// handle <item>Label</item>
|
// handle <item>Label</item>
|
||||||
|
|
||||||
// add to the list
|
// add to the list
|
||||||
wxString str = GetNodeContent(m_node);
|
strList.Add(GetNodeText(m_node, wxXRC_TEXT_NO_ESCAPE));
|
||||||
if (m_resource->GetFlags() & wxXRC_USE_LOCALE)
|
|
||||||
str = wxGetTranslation(str, m_resource->GetDomain());
|
|
||||||
strList.Add(str);
|
|
||||||
|
|
||||||
return NULL;
|
return NULL;
|
||||||
}
|
}
|
||||||
|
@@ -107,30 +107,15 @@ wxObject *wxRadioBoxXmlHandler::DoCreateResource()
|
|||||||
// we handle handle <item>Label</item> constructs here, and the item
|
// we handle handle <item>Label</item> constructs here, and the item
|
||||||
// tag can have tooltip, helptext, enabled and hidden attributes
|
// tag can have tooltip, helptext, enabled and hidden attributes
|
||||||
|
|
||||||
wxString label = GetNodeContent(m_node);
|
m_labels.push_back(GetNodeText(m_node, wxXRC_TEXT_NO_ESCAPE));
|
||||||
|
|
||||||
wxString tooltip;
|
|
||||||
m_node->GetAttribute(wxT("tooltip"), &tooltip);
|
|
||||||
|
|
||||||
wxString helptext;
|
|
||||||
bool hasHelptext = m_node->GetAttribute(wxT("helptext"), &helptext);
|
|
||||||
|
|
||||||
if (m_resource->GetFlags() & wxXRC_USE_LOCALE)
|
|
||||||
{
|
|
||||||
label = wxGetTranslation(label, m_resource->GetDomain());
|
|
||||||
if ( !tooltip.empty() )
|
|
||||||
tooltip = wxGetTranslation(tooltip, m_resource->GetDomain());
|
|
||||||
if ( hasHelptext )
|
|
||||||
helptext = wxGetTranslation(helptext, m_resource->GetDomain());
|
|
||||||
}
|
|
||||||
|
|
||||||
m_labels.push_back(label);
|
|
||||||
#if wxUSE_TOOLTIPS
|
#if wxUSE_TOOLTIPS
|
||||||
m_tooltips.push_back(tooltip);
|
m_tooltips.push_back(GetNodeText(GetParamNode(wxT("tooltip")),
|
||||||
|
wxXRC_TEXT_NO_ESCAPE));
|
||||||
#endif // wxUSE_TOOLTIPS
|
#endif // wxUSE_TOOLTIPS
|
||||||
#if wxUSE_HELP
|
#if wxUSE_HELP
|
||||||
m_helptexts.push_back(helptext);
|
const wxXmlNode* const nodeHelp = GetParamNode(wxT("helptext"));
|
||||||
m_helptextSpecified.push_back(hasHelptext);
|
m_helptexts.push_back(GetNodeText(nodeHelp, wxXRC_TEXT_NO_ESCAPE));
|
||||||
|
m_helptextSpecified.push_back(nodeHelp != NULL);
|
||||||
#endif // wxUSE_HELP
|
#endif // wxUSE_HELP
|
||||||
m_isEnabled.push_back(GetBoolAttr("enabled", 1));
|
m_isEnabled.push_back(GetBoolAttr("enabled", 1));
|
||||||
m_isShown.push_back(!GetBoolAttr("hidden", 0));
|
m_isShown.push_back(!GetBoolAttr("hidden", 0));
|
||||||
|
@@ -1506,73 +1506,83 @@ int wxXmlResourceHandlerImpl::GetStyle(const wxString& param, int defaults)
|
|||||||
|
|
||||||
|
|
||||||
|
|
||||||
wxString wxXmlResourceHandlerImpl::GetText(const wxString& param, bool translate)
|
wxString wxXmlResourceHandlerImpl::GetNodeText(const wxXmlNode* node, int flags)
|
||||||
{
|
{
|
||||||
wxXmlNode *parNode = GetParamNode(param);
|
wxString str1(GetNodeContent(node));
|
||||||
wxString str1(GetNodeContent(parNode));
|
if ( str1.empty() )
|
||||||
|
return str1;
|
||||||
|
|
||||||
wxString str2;
|
wxString str2;
|
||||||
|
|
||||||
// "\\" wasn't translated to "\" prior to 2.5.3.0:
|
if ( !(flags & wxXRC_TEXT_NO_ESCAPE) )
|
||||||
const bool escapeBackslash = (m_handler->m_resource->CompareVersion(2,5,3,0) >= 0);
|
|
||||||
|
|
||||||
// VS: First version of XRC resources used $ instead of & (which is
|
|
||||||
// illegal in XML), but later I realized that '_' fits this purpose
|
|
||||||
// much better (because &File means "File with F underlined").
|
|
||||||
const wxChar amp_char = (m_handler->m_resource->CompareVersion(2,3,0,1) < 0)
|
|
||||||
? '$' : '_';
|
|
||||||
|
|
||||||
for ( wxString::const_iterator dt = str1.begin(); dt != str1.end(); ++dt )
|
|
||||||
{
|
{
|
||||||
// Remap amp_char to &, map double amp_char to amp_char (for things
|
// "\\" wasn't translated to "\" prior to 2.5.3.0:
|
||||||
// like "&File..." -- this is illegal in XML, so we use "_File..."):
|
const bool escapeBackslash = (m_handler->m_resource->CompareVersion(2,5,3,0) >= 0);
|
||||||
if ( *dt == amp_char )
|
|
||||||
|
// VS: First version of XRC resources used $ instead of & (which is
|
||||||
|
// illegal in XML), but later I realized that '_' fits this purpose
|
||||||
|
// much better (because &File means "File with F underlined").
|
||||||
|
const wxChar amp_char = (m_handler->m_resource->CompareVersion(2,3,0,1) < 0)
|
||||||
|
? '$' : '_';
|
||||||
|
|
||||||
|
for ( wxString::const_iterator dt = str1.begin(); dt != str1.end(); ++dt )
|
||||||
{
|
{
|
||||||
if ( dt+1 == str1.end() || *(++dt) == amp_char )
|
// Remap amp_char to &, map double amp_char to amp_char (for things
|
||||||
str2 << amp_char;
|
// like "&File..." -- this is illegal in XML, so we use "_File..."):
|
||||||
else
|
if ( *dt == amp_char )
|
||||||
str2 << wxT('&') << *dt;
|
|
||||||
}
|
|
||||||
// Remap \n to CR, \r to LF, \t to TAB, \\ to \:
|
|
||||||
else if ( *dt == wxT('\\') )
|
|
||||||
{
|
|
||||||
switch ( (*(++dt)).GetValue() )
|
|
||||||
{
|
{
|
||||||
case wxT('n'):
|
if ( dt+1 == str1.end() || *(++dt) == amp_char )
|
||||||
str2 << wxT('\n');
|
str2 << amp_char;
|
||||||
break;
|
else
|
||||||
|
str2 << wxT('&') << *dt;
|
||||||
case wxT('t'):
|
}
|
||||||
str2 << wxT('\t');
|
// Remap \n to CR, \r to LF, \t to TAB, \\ to \:
|
||||||
break;
|
else if ( *dt == wxT('\\') )
|
||||||
|
{
|
||||||
case wxT('r'):
|
switch ( (*(++dt)).GetValue() )
|
||||||
str2 << wxT('\r');
|
{
|
||||||
break;
|
case wxT('n'):
|
||||||
|
str2 << wxT('\n');
|
||||||
case wxT('\\') :
|
|
||||||
// "\\" wasn't translated to "\" prior to 2.5.3.0:
|
|
||||||
if ( escapeBackslash )
|
|
||||||
{
|
|
||||||
str2 << wxT('\\');
|
|
||||||
break;
|
break;
|
||||||
}
|
|
||||||
wxFALLTHROUGH;// else fall-through to default: branch below
|
|
||||||
|
|
||||||
default:
|
case wxT('t'):
|
||||||
str2 << wxT('\\') << *dt;
|
str2 << wxT('\t');
|
||||||
break;
|
break;
|
||||||
|
|
||||||
|
case wxT('r'):
|
||||||
|
str2 << wxT('\r');
|
||||||
|
break;
|
||||||
|
|
||||||
|
case wxT('\\') :
|
||||||
|
// "\\" wasn't translated to "\" prior to 2.5.3.0:
|
||||||
|
if ( escapeBackslash )
|
||||||
|
{
|
||||||
|
str2 << wxT('\\');
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
wxFALLTHROUGH;// else fall-through to default: branch below
|
||||||
|
|
||||||
|
default:
|
||||||
|
str2 << wxT('\\') << *dt;
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
str2 << *dt;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
else
|
}
|
||||||
{
|
else // Don't escape anything in this string.
|
||||||
str2 << *dt;
|
{
|
||||||
}
|
// We won't use str1 at all, so move its contents to str2.
|
||||||
|
str2.swap(str1);
|
||||||
}
|
}
|
||||||
|
|
||||||
if (m_handler->m_resource->GetFlags() & wxXRC_USE_LOCALE)
|
if (m_handler->m_resource->GetFlags() & wxXRC_USE_LOCALE)
|
||||||
{
|
{
|
||||||
if (translate && parNode &&
|
if (!(flags & wxXRC_TEXT_NO_TRANSLATE) && node &&
|
||||||
parNode->GetAttribute(wxT("translate"), wxEmptyString) != wxT("0"))
|
node->GetAttribute(wxT("translate"), wxEmptyString) != wxT("0"))
|
||||||
{
|
{
|
||||||
return wxGetTranslation(str2, m_handler->m_resource->GetDomain());
|
return wxGetTranslation(str2, m_handler->m_resource->GetDomain());
|
||||||
}
|
}
|
||||||
@@ -2508,14 +2518,14 @@ void wxXmlResourceHandlerImpl::SetupWindow(wxWindow *wnd)
|
|||||||
wnd->SetFocus();
|
wnd->SetFocus();
|
||||||
#if wxUSE_TOOLTIPS
|
#if wxUSE_TOOLTIPS
|
||||||
if (HasParam(wxT("tooltip")))
|
if (HasParam(wxT("tooltip")))
|
||||||
wnd->SetToolTip(GetText(wxT("tooltip")));
|
wnd->SetToolTip(GetNodeText(GetParamNode(wxT("tooltip"))));
|
||||||
#endif
|
#endif
|
||||||
if (HasParam(wxT("font")))
|
if (HasParam(wxT("font")))
|
||||||
wnd->SetFont(GetFont(wxT("font"), wnd));
|
wnd->SetFont(GetFont(wxT("font"), wnd));
|
||||||
if (HasParam(wxT("ownfont")))
|
if (HasParam(wxT("ownfont")))
|
||||||
wnd->SetOwnFont(GetFont(wxT("ownfont"), wnd));
|
wnd->SetOwnFont(GetFont(wxT("ownfont"), wnd));
|
||||||
if (HasParam(wxT("help")))
|
if (HasParam(wxT("help")))
|
||||||
wnd->SetHelpText(GetText(wxT("help")));
|
wnd->SetHelpText(GetNodeText(GetParamNode(wxT("help"))));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
Reference in New Issue
Block a user