X-Git-Url: https://git.saurik.com/wxWidgets.git/blobdiff_plain/ce411ca8f5ee0ed4276bd560bb71500d375b2f4b..2e49a8074e7c9179b642852ff00ed8fdf0768bb1:/src/common/translation.cpp diff --git a/src/common/translation.cpp b/src/common/translation.cpp index 5e259574d5..c00339d7a5 100644 --- a/src/common/translation.cpp +++ b/src/common/translation.cpp @@ -41,14 +41,19 @@ #include #include +#include "wx/arrstr.h" +#include "wx/dir.h" #include "wx/file.h" #include "wx/filename.h" #include "wx/tokenzr.h" #include "wx/fontmap.h" -#include "wx/scopedptr.h" #include "wx/stdpaths.h" #include "wx/hashset.h" +#ifdef __WXMSW__ + #include "wx/msw/wrapwin.h" +#endif + // ---------------------------------------------------------------------------- // simple types // ---------------------------------------------------------------------------- @@ -69,6 +74,18 @@ const size_t32 MSGCATALOG_MAGIC_SW = 0xde120495; // implementation // ============================================================================ +namespace +{ + +#if !wxUSE_UNICODE +// We need to keep track of (char*) msgids in non-Unicode legacy builds. Instead +// of making the public wxMsgCatalog and wxTranslationsLoader APIs ugly, we +// store them in this global map. +wxStringToStringHashMap gs_msgIdCharset; +#endif + +} // anonymous namespace + // ---------------------------------------------------------------------------- // Plural forms parser // ---------------------------------------------------------------------------- @@ -414,7 +431,7 @@ private: wxPluralFormsNodePtr m_plural; }; -wxDEFINE_SCOPED_PTR_TYPE(wxPluralFormsCalculator) +wxDEFINE_SCOPED_PTR(wxPluralFormsCalculator, wxPluralFormsCalculatorPtr) void wxPluralFormsCalculator::init(wxPluralFormsToken::Number nplurals, wxPluralFormsNode* plural) @@ -785,12 +802,10 @@ wxPluralFormsCalculator* wxPluralFormsCalculator::make(const char* s) // http://www.gnu.org/software/autoconf/manual/gettext/MO-Files.html // ---------------------------------------------------------------------------- -WX_DECLARE_EXPORTED_STRING_HASH_MAP(wxString, wxMessagesHash); - class wxMsgCatalogFile { public: - typedef wxScopedCharTypeBuffer DataBuffer; + typedef wxScopedCharBuffer DataBuffer; // ctor & dtor wxMsgCatalogFile(); @@ -803,7 +818,7 @@ public: wxPluralFormsCalculatorPtr& rPluralFormsCalculator); // fills the hash with string-translation pairs - bool FillHash(wxMessagesHash& hash, const wxString& msgIdCharset) const; + bool FillHash(wxStringToStringHashMap& hash, const wxString& domain) const; // return the charset of the strings in this catalog or empty string if // none/unknown @@ -869,53 +884,6 @@ private: wxDECLARE_NO_COPY_CLASS(wxMsgCatalogFile); }; - -// ---------------------------------------------------------------------------- -// wxMsgCatalog corresponds to one loaded message catalog. -// -// This is a "low-level" class and is used only by wxLocale (that's why -// it's designed to be stored in a linked list) -// ---------------------------------------------------------------------------- - -class wxMsgCatalog -{ -public: -#if !wxUSE_UNICODE - wxMsgCatalog() { m_conv = NULL; } - ~wxMsgCatalog(); -#endif - - // load the catalog from disk - bool LoadFile(const wxString& filename, - const wxString& domain, - const wxString& msgIdCharset); - - bool LoadData(const wxScopedCharTypeBuffer& data, - const wxString& domain, - const wxString& msgIdCharset); - - // get name of the catalog - wxString GetDomain() const { return m_domain; } - - // get the translated string: returns NULL if not found - const wxString *GetString(const wxString& sz, unsigned n = UINT_MAX) const; - - // public variable pointing to the next element in a linked list (or NULL) - wxMsgCatalog *m_pNext; - -private: - wxMessagesHash m_messages; // all messages in the catalog - wxString m_domain; // name of the domain - -#if !wxUSE_UNICODE - // the conversion corresponding to this catalog charset if we installed it - // as the global one - wxCSConv *m_conv; -#endif - - wxPluralFormsCalculatorPtr m_pluralFormsCalculator; -}; - // ---------------------------------------------------------------------------- // wxMsgCatalogFile clas // ---------------------------------------------------------------------------- @@ -1057,10 +1025,10 @@ bool wxMsgCatalogFile::LoadData(const DataBuffer& data, return true; } -bool wxMsgCatalogFile::FillHash(wxMessagesHash& hash, - const wxString& msgIdCharset) const +bool wxMsgCatalogFile::FillHash(wxStringToStringHashMap& hash, + const wxString& domain) const { - wxUnusedVar(msgIdCharset); // silence warning in Unicode build + wxUnusedVar(domain); // silence warning in Unicode build // conversion to use to convert catalog strings to the GUI encoding wxMBConv *inputConv = NULL; @@ -1088,6 +1056,8 @@ bool wxMsgCatalogFile::FillHash(wxMessagesHash& hash, } #if !wxUSE_UNICODE + wxString msgIdCharset = gs_msgIdCharset[domain]; + // conversion to apply to msgid strings before looking them up: we only // need it if the msgids are neither in 7 bit ASCII nor in the same // encoding as the catalog @@ -1179,38 +1149,38 @@ wxMsgCatalog::~wxMsgCatalog() } #endif // !wxUSE_UNICODE -bool wxMsgCatalog::LoadFile(const wxString& filename, - const wxString& domain, - const wxString& msgIdCharset) +/* static */ +wxMsgCatalog *wxMsgCatalog::CreateFromFile(const wxString& filename, + const wxString& domain) { - wxMsgCatalogFile file; + wxScopedPtr cat(new wxMsgCatalog(domain)); - m_domain = domain; + wxMsgCatalogFile file; - if ( !file.LoadFile(filename, m_pluralFormsCalculator) ) - return false; + if ( !file.LoadFile(filename, cat->m_pluralFormsCalculator) ) + return NULL; - if ( !file.FillHash(m_messages, msgIdCharset) ) - return false; + if ( !file.FillHash(cat->m_messages, domain) ) + return NULL; - return true; + return cat.release(); } -bool wxMsgCatalog::LoadData(const wxScopedCharTypeBuffer& data, - const wxString& domain, - const wxString& msgIdCharset) +/* static */ +wxMsgCatalog *wxMsgCatalog::CreateFromData(const wxScopedCharBuffer& data, + const wxString& domain) { - wxMsgCatalogFile file; + wxScopedPtr cat(new wxMsgCatalog(domain)); - m_domain = domain; + wxMsgCatalogFile file; - if ( !file.LoadData(data, m_pluralFormsCalculator) ) - return false; + if ( !file.LoadData(data, cat->m_pluralFormsCalculator) ) + return NULL; - if ( !file.FillHash(m_messages, msgIdCharset) ) - return false; + if ( !file.FillHash(cat->m_messages, domain) ) + return NULL; - return true; + return cat.release(); } const wxString *wxMsgCatalog::GetString(const wxString& str, unsigned n) const @@ -1220,7 +1190,7 @@ const wxString *wxMsgCatalog::GetString(const wxString& str, unsigned n) const { index = m_pluralFormsCalculator->evaluate(n); } - wxMessagesHash::const_iterator i; + wxStringToStringHashMap::const_iterator i; if (index != 0) { i = m_messages.find(wxString(str) + wxChar(index)); // plural @@ -1322,6 +1292,14 @@ void wxTranslations::SetLanguage(const wxString& lang) } +wxArrayString wxTranslations::GetAvailableTranslations(const wxString& domain) const +{ + wxCHECK_MSG( m_loader, wxArrayString(), "loader can't be NULL" ); + + return m_loader->GetAvailableTranslations(domain); +} + + bool wxTranslations::AddStdCatalog() { if ( !AddCatalog(wxS("wxstd")) ) @@ -1349,7 +1327,7 @@ bool wxTranslations::AddCatalog(const wxString& domain, wxLanguage msgIdLanguage, const wxString& msgIdCharset) { - m_msgIdCharset[domain] = msgIdCharset; + gs_msgIdCharset[domain] = msgIdCharset; return AddCatalog(domain, msgIdLanguage); } #endif // !wxUSE_UNICODE @@ -1384,8 +1362,11 @@ bool wxTranslations::AddCatalog(const wxString& domain, bool wxTranslations::LoadCatalog(const wxString& domain, const wxString& lang) { + m_loader->GetAvailableTranslations(domain); wxCHECK_MSG( m_loader, false, "loader can't be NULL" ); + wxMsgCatalog *cat = NULL; + #if wxUSE_FONTMAP // first look for the catalog for this language and the current locale: // notice that we don't use the system name for the locale as this would @@ -1397,93 +1378,49 @@ bool wxTranslations::LoadCatalog(const wxString& domain, const wxString& lang) wxString fullname(lang); fullname << wxS('.') << wxFontMapperBase::GetEncodingName(encSys); - if ( m_loader->LoadCatalog(this, domain, fullname) ) - return true; + cat = m_loader->LoadCatalog(domain, fullname); } #endif // wxUSE_FONTMAP - // Next try: use the provided name language name: - if ( m_loader->LoadCatalog(this, domain, lang) ) - return true; - - // Also try just base locale name: for things like "fr_BE" (Belgium - // French) we should use fall back on plain "fr" if no Belgium-specific - // message catalogs exist - wxString baselang = lang.BeforeFirst('_'); - if ( lang != baselang ) + if ( !cat ) { - if ( m_loader->LoadCatalog(this, domain, baselang) ) - return true; + // Next try: use the provided name language name: + cat = m_loader->LoadCatalog(domain, lang); } - // Nothing worked, the catalog just isn't there - wxLogTrace(TRACE_I18N, - "Catalog \"%s.mo\" not found for language \"%s\".", - domain, lang); - return false; -} - - -// check if the given catalog is loaded -bool wxTranslations::IsLoaded(const wxString& domain) const -{ - return FindCatalog(domain) != NULL; -} - - -bool wxTranslations::LoadCatalogFile(const wxString& filename, - const wxString& domain) -{ - wxMsgCatalog *pMsgCat = new wxMsgCatalog; - -#if wxUSE_UNICODE - const bool ok = pMsgCat->LoadFile(filename, domain, wxEmptyString/*unused*/); -#else - const bool ok = pMsgCat->LoadFile(filename, domain, - m_msgIdCharset[domain]); -#endif - - if ( !ok ) + if ( !cat ) { - // don't add it because it couldn't be loaded anyway - delete pMsgCat; - return false; + // Also try just base locale name: for things like "fr_BE" (Belgium + // French) we should use fall back on plain "fr" if no Belgium-specific + // message catalogs exist + wxString baselang = lang.BeforeFirst('_'); + if ( lang != baselang ) + cat = m_loader->LoadCatalog(domain, baselang); } - // add it to the head of the list so that in GetString it will - // be searched before the catalogs added earlier - pMsgCat->m_pNext = m_pMsgCat; - m_pMsgCat = pMsgCat; - - return true; -} - - -bool wxTranslations::LoadCatalogData(const wxScopedCharTypeBuffer& data, - const wxString& domain) -{ - wxMsgCatalog *pMsgCat = new wxMsgCatalog; - -#if wxUSE_UNICODE - const bool ok = pMsgCat->LoadData(data, domain, wxEmptyString/*unused*/); -#else - const bool ok = pMsgCat->LoadData(data, domain, - m_msgIdCharset[domain]); -#endif + if ( cat ) + { + // add it to the head of the list so that in GetString it will + // be searched before the catalogs added earlier + cat->m_pNext = m_pMsgCat; + m_pMsgCat = cat; - if ( !ok ) + return true; + } + else { - // don't add it because it couldn't be loaded anyway - delete pMsgCat; + // Nothing worked, the catalog just isn't there + wxLogTrace(TRACE_I18N, + "Catalog \"%s.mo\" not found for language \"%s\".", + domain, lang); return false; } +} - // add it to the head of the list so that in GetString it will - // be searched before the catalogs added earlier - pMsgCat->m_pNext = m_pMsgCat; - m_pMsgCat = pMsgCat; - - return true; +// check if the given catalog is loaded +bool wxTranslations::IsLoaded(const wxString& domain) const +{ + return FindCatalog(domain) != NULL; } @@ -1563,8 +1500,8 @@ const wxString& wxTranslations::GetString(const wxString& origString, TRACE_I18N, "string \"%s\"%s not found in %slocale '%s'.", origString, - ((long)n) != -1 ? wxString::Format("[%ld]", (long)n) : wxString(), - !domain.empty() ? wxString::Format("domain '%s' ", domain) : wxString(), + (n != UINT_MAX ? wxString::Format("[%ld]", (long)n) : wxString()), + (!domain.empty() ? wxString::Format("domain '%s' ", domain) : wxString()), m_lang ); @@ -1677,25 +1614,33 @@ wxString GetMsgCatalogSubdirs(const wxString& prefix, const wxString& lang) return searchPath; } -// construct the search path for the given language -static wxString GetFullSearchPath(const wxString& lang) +bool HasMsgCatalogInDir(const wxString& dir, const wxString& domain) +{ + return wxFileName(dir, domain, "mo").FileExists() || + wxFileName(dir + wxFILE_SEP_PATH + "LC_MESSAGES", domain, "mo").FileExists(); +} + +// get prefixes to locale directories; if lang is empty, don't point to +// OSX's .lproj bundles +wxArrayString GetSearchPrefixes(const wxString& lang = wxString()) { - // first take the entries explicitly added by the program wxArrayString paths; - paths.reserve(gs_searchPrefixes.size() + 1); - size_t n, - count = gs_searchPrefixes.size(); - for ( n = 0; n < count; n++ ) - { - paths.Add(GetMsgCatalogSubdirs(gs_searchPrefixes[n], lang)); - } + // first take the entries explicitly added by the program + paths = gs_searchPrefixes; #if wxUSE_STDPATHS // then look in the standard location - const wxString stdp = wxStandardPaths::Get(). - GetLocalizedResourcesDir(lang, wxStandardPaths::ResourceCat_Messages); - + wxString stdp; + if ( lang.empty() ) + { + stdp = wxStandardPaths::Get().GetResourcesDir(); + } + else + { + stdp = wxStandardPaths::Get(). + GetLocalizedResourcesDir(lang, wxStandardPaths::ResourceCat_Messages); + } if ( paths.Index(stdp) == wxNOT_FOUND ) paths.Add(stdp); #endif // wxUSE_STDPATHS @@ -1707,7 +1652,7 @@ static wxString GetFullSearchPath(const wxString& lang) const char *pszLcPath = wxGetenv("LC_PATH"); if ( pszLcPath ) { - const wxString lcp = GetMsgCatalogSubdirs(pszLcPath, lang); + const wxString lcp = pszLcPath; if ( paths.Index(lcp) == wxNOT_FOUND ) paths.Add(lcp); } @@ -1716,22 +1661,32 @@ static wxString GetFullSearchPath(const wxString& lang) wxString wxp = wxGetInstallPrefix(); if ( !wxp.empty() ) { - wxp = GetMsgCatalogSubdirs(wxp + wxS("/share/locale"), lang); + wxp += wxS("/share/locale"); if ( paths.Index(wxp) == wxNOT_FOUND ) paths.Add(wxp); } #endif // __UNIX__ + return paths; +} - // finally construct the full search path +// construct the search path for the given language +wxString GetFullSearchPath(const wxString& lang) +{ wxString searchPath; searchPath.reserve(500); - count = paths.size(); - for ( n = 0; n < count; n++ ) + + const wxArrayString prefixes = GetSearchPrefixes(lang); + + for ( wxArrayString::const_iterator i = prefixes.begin(); + i != prefixes.end(); + ++i ) { - searchPath += paths[n]; - if ( n != count - 1 ) + const wxString p = GetMsgCatalogSubdirs(*i, lang); + + if ( !searchPath.empty() ) searchPath += wxPATH_SEP; + searchPath += p; } return searchPath; @@ -1750,9 +1705,8 @@ void wxFileTranslationsLoader::AddCatalogLookupPathPrefix(const wxString& prefix } -bool wxFileTranslationsLoader::LoadCatalog(wxTranslations *translations, - const wxString& domain, - const wxString& lang) +wxMsgCatalog *wxFileTranslationsLoader::LoadCatalog(const wxString& domain, + const wxString& lang) { wxString searchPath = GetFullSearchPath(lang); @@ -1764,13 +1718,57 @@ bool wxFileTranslationsLoader::LoadCatalog(wxTranslations *translations, wxString strFullName; if ( !wxFindFileInPath(&strFullName, searchPath, fn.GetFullPath()) ) - return false; + return NULL; // open file and read its data wxLogVerbose(_("using catalog '%s' from '%s'."), domain, strFullName.c_str()); wxLogTrace(TRACE_I18N, wxS("Using catalog \"%s\"."), strFullName.c_str()); - return translations->LoadCatalogFile(strFullName, domain); + return wxMsgCatalog::CreateFromFile(strFullName, domain); +} + + +wxArrayString wxFileTranslationsLoader::GetAvailableTranslations(const wxString& domain) const +{ + wxArrayString langs; + const wxArrayString prefixes = GetSearchPrefixes(); + + wxLogTrace(TRACE_I18N, + "looking for available translations of \"%s\" in search path \"%s\"", + domain, wxJoin(prefixes, wxPATH_SEP[0])); + + for ( wxArrayString::const_iterator i = prefixes.begin(); + i != prefixes.end(); + ++i ) + { + if (i->length() == 0) + continue; + wxDir dir; + if ( !dir.Open(*i) ) + continue; + + wxString lang; + for ( bool ok = dir.GetFirst(&lang, "", wxDIR_DIRS); + ok; + ok = dir.GetNext(&lang) ) + { + const wxString langdir = *i + wxFILE_SEP_PATH + lang; + if ( HasMsgCatalogInDir(langdir, domain) ) + { +#ifdef __WXOSX__ + wxString rest; + if ( lang.EndsWith(".lproj", &rest) ) + lang = rest; +#endif // __WXOSX__ + + wxLogTrace(TRACE_I18N, + "found %s translation of \"%s\"", lang, domain); + langs.push_back(lang); + } + } + } + + return langs; } @@ -1779,11 +1777,10 @@ bool wxFileTranslationsLoader::LoadCatalog(wxTranslations *translations, // ---------------------------------------------------------------------------- #ifdef __WINDOWS__ -bool wxResourceTranslationsLoader::LoadCatalog(wxTranslations *translations, - const wxString& domain, - const wxString& lang) -{ +wxMsgCatalog *wxResourceTranslationsLoader::LoadCatalog(const wxString& domain, + const wxString& lang) +{ const void *mo_data = NULL; size_t mo_size = 0; @@ -1793,18 +1790,73 @@ bool wxResourceTranslationsLoader::LoadCatalog(wxTranslations *translations, resname, GetResourceType(), GetModule()) ) - return false; + return NULL; wxLogTrace(TRACE_I18N, "Using catalog from Windows resource \"%s\".", resname); - const bool ok = translations->LoadCatalogData( - wxCharBuffer::CreateNonOwned(static_cast(mo_data), mo_size)); - if ( !ok ) + wxMsgCatalog *cat = wxMsgCatalog::CreateFromData( + wxCharBuffer::CreateNonOwned(static_cast(mo_data), mo_size), + domain); + + if ( !cat ) + { wxLogWarning(_("Resource '%s' is not a valid message catalog."), resname); + } - return ok; + return cat; } + +namespace +{ + +struct EnumCallbackData +{ + wxString prefix; + wxArrayString langs; +}; + +BOOL CALLBACK EnumTranslations(HMODULE WXUNUSED(hModule), + LPCTSTR WXUNUSED(lpszType), + LPTSTR lpszName, + LONG_PTR lParam) +{ + wxString name(lpszName); + name.MakeLower(); // resource names are case insensitive + + EnumCallbackData *data = reinterpret_cast(lParam); + + wxString lang; + if ( name.StartsWith(data->prefix, &lang) && !lang.empty() ) + data->langs.push_back(lang); + + return TRUE; // continue enumeration +} + +} // anonymous namespace + + +wxArrayString wxResourceTranslationsLoader::GetAvailableTranslations(const wxString& domain) const +{ + EnumCallbackData data; + data.prefix = domain + "_"; + data.prefix.MakeLower(); // resource names are case insensitive + + if ( !EnumResourceNames(GetModule(), + GetResourceType().t_str(), + EnumTranslations, + reinterpret_cast(&data)) ) + { + const DWORD err = GetLastError(); + if ( err != NO_ERROR && err != ERROR_RESOURCE_TYPE_NOT_FOUND ) + { + wxLogSysError(_("Couldn't enumerate translations")); + } + } + + return data.langs; +} + #endif // __WINDOWS__