return reference to non-temporary wxString instance from wxGetTranslation() even when not translating

git-svn-id: https://svn.wxwidgets.org/svn/wx/wxWidgets/trunk@46144 c3d73ce0-8a6f-49c7-b76d-6d57e0e08775
This commit is contained in:
Václav Slavík
2007-05-21 17:44:37 +00:00
parent fe4a282956
commit a64be16ef8
2 changed files with 54 additions and 20 deletions

View File

@@ -512,6 +512,11 @@ public:
size_t n, size_t n,
const wxString& domain = wxEmptyString) const; const wxString& domain = wxEmptyString) const;
// this is hack to work around a problem with wxGetTranslation() which
// returns const wxString& and not wxString, so when it returns untranslated
// string, it needs to have a copy of it somewhere
static const wxString& GetUntranslatedString(const wxString& str);
// 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; }
@@ -569,7 +574,9 @@ inline const wxString& wxGetTranslation(const wxString& str,
if (pLoc) if (pLoc)
return pLoc->GetString(str, domain); return pLoc->GetString(str, domain);
else else
return str; // NB: this function returns reference to a string, so we have to keep
// a copy of it somewhere
return wxLocale::GetUntranslatedString(str);
} }
inline const wxString& wxGetTranslation(const wxString& str1, inline const wxString& wxGetTranslation(const wxString& str1,
const wxString& str2, const wxString& str2,
@@ -580,7 +587,11 @@ inline const wxString& wxGetTranslation(const wxString& str1,
if (pLoc) if (pLoc)
return pLoc->GetString(str1, str2, n, domain); return pLoc->GetString(str1, str2, n, domain);
else else
return n == 1 ? str1 : str2; // NB: this function returns reference to a string, so we have to keep
// a copy of it somewhere
return n == 1
? wxLocale::GetUntranslatedString(str1)
: wxLocale::GetUntranslatedString(str2);
} }
#else // !wxUSE_INTL #else // !wxUSE_INTL
@@ -596,22 +607,27 @@ inline const wxString& wxGetTranslation(const wxString& str1,
#define wxTRANSLATE(str) _T(str) #define wxTRANSLATE(str) _T(str)
inline const wxString& wxGetTranslation(const wxString& str, // NB: we use a template here in order to avoid using
const wxString& WXUNUSED(domain) = wxEmptyString) // wxLocale::GetUntranslatedString() above, which would be required if
{ // we returned const wxString&; this way, the compiler should be able to
return str; // optimize wxGetTranslation() away
}
inline const wxString& wxGetTranslation(const wxString& str1, template<typename TString>
const wxString& str2, inline TString wxGetTranslation(TString str)
size_t n, { return str; }
const wxString& WXUNUSED(domain) = wxEmptyString)
{ template<typename TString, typename TDomain>
if ( n == 1 ) inline TString wxGetTranslation(TString str, TDomain WXUNUSED(domain))
return str1; { return str; }
else
return str2; template<typename TString, typename TDomain>
} inline TString wxGetTranslation(TString str1, TString str2, size_t n)
{ return n == 1 ? str1 : str2; }
template<typename TString, typename TDomain>
inline TString wxGetTranslation(TString str1, TString str2, size_t n,
TDomain WXUNUSED(domain))
{ return n == 1 ? str1 : str2; }
#endif // wxUSE_INTL/!wxUSE_INTL #endif // wxUSE_INTL/!wxUSE_INTL

View File

@@ -69,6 +69,7 @@
#include "wx/ptr_scpd.h" #include "wx/ptr_scpd.h"
#include "wx/apptrait.h" #include "wx/apptrait.h"
#include "wx/stdpaths.h" #include "wx/stdpaths.h"
#include "wx/hashset.h"
#if defined(__WXMAC__) #if defined(__WXMAC__)
#include "wx/mac/private.h" // includes mac headers #include "wx/mac/private.h" // includes mac headers
@@ -2614,7 +2615,7 @@ const wxString& wxLocale::GetString(const wxString& origString,
const wxString& domain) const const wxString& domain) const
{ {
if ( origString.empty() ) if ( origString.empty() )
return origString; return GetUntranslatedString(origString);
const wxString *trans = NULL; const wxString *trans = NULL;
wxMsgCatalog *pMsgCat; wxMsgCatalog *pMsgCat;
@@ -2656,14 +2657,31 @@ const wxString& wxLocale::GetString(const wxString& origString,
#endif // __WXDEBUG__ #endif // __WXDEBUG__
if (n == size_t(-1)) if (n == size_t(-1))
return origString; return GetUntranslatedString(origString);
else else
return n == 1 ? origString : origString2; return GetUntranslatedString(n == 1 ? origString : origString2);
} }
return *trans; return *trans;
} }
WX_DECLARE_HASH_SET(wxString, wxStringHash, wxStringEqual,
wxLocaleUntranslatedStrings);
/* static */
const wxString& wxLocale::GetUntranslatedString(const wxString& str)
{
static wxLocaleUntranslatedStrings s_strings;
wxLocaleUntranslatedStrings::iterator i = s_strings.find(str);
if ( i != s_strings.end() )
return *i;
else
return *s_strings.insert(str).first;
return *i;
}
wxString wxLocale::GetHeaderValue(const wxString& header, wxString wxLocale::GetHeaderValue(const wxString& header,
const wxString& domain) const const wxString& domain) const
{ {