diff --git a/include/wx/translation.h b/include/wx/translation.h index ca97649a4b..0f38931d2f 100644 --- a/include/wx/translation.h +++ b/include/wx/translation.h @@ -203,6 +203,8 @@ private: static void SetNonOwned(wxTranslations *t); friend class wxLocale; + wxString DoGetBestAvailableTranslation(const wxString& domain, const wxString& additionalAvailableLanguage); + private: wxString m_lang; wxTranslationsLoader *m_loader; diff --git a/src/common/translation.cpp b/src/common/translation.cpp index 7ced54853d..6ed764df5b 100644 --- a/src/common/translation.cpp +++ b/src/common/translation.cpp @@ -1308,13 +1308,11 @@ bool wxTranslations::AddStdCatalog() // the name without the version if it's not found, as message catalogs // typically won't have the version in their names under non-Unix platforms // (i.e. where they're not installed by our own "make install"). - if ( AddAvailableCatalog("wxstd-" wxSTRINGIZE(wxMAJOR_VERSION) "." wxSTRINGIZE(wxMINOR_VERSION)) ) - return true; + wxString domain("wxstd-" wxSTRINGIZE(wxMAJOR_VERSION) "." wxSTRINGIZE(wxMINOR_VERSION)); + if ( GetBestAvailableTranslation(domain).empty() ) + domain = wxS("wxstd"); - if ( AddCatalog(wxS("wxstd")) ) - return true; - - return false; + return AddCatalog(domain); } bool wxTranslations::AddAvailableCatalog(const wxString& domain) @@ -1334,39 +1332,30 @@ bool wxTranslations::AddAvailableCatalog(const wxString& domain) bool wxTranslations::AddCatalog(const wxString& domain, wxLanguage msgIdLanguage) { - if ( AddAvailableCatalog(domain) ) - return true; - const wxString msgIdLang = wxUILocale::GetLanguageCanonicalName(msgIdLanguage); - - // Check if the original strings can be used directly. - bool canUseUntranslated = false; - if ( m_lang.empty() ) - { - // If we are using the default language, check if the message ID - // language is acceptable for this system. - const wxString domain_lang = GetBestTranslation(domain, msgIdLang); - - if ( msgIdLang == domain_lang ) - canUseUntranslated = true; - } - else // But if we have a fixed language, we should just check it instead. - { - // Consider message IDs for another region using the same language - // acceptable. - if ( msgIdLang.BeforeFirst('_') == m_lang.BeforeFirst('_') ) - canUseUntranslated = true; - } - - if ( canUseUntranslated ) + const wxString domain_lang = GetBestTranslation(domain, msgIdLang); + if ( domain_lang.empty() ) { wxLogTrace(TRACE_I18N, - wxS("not using translations for domain '%s' with msgid language '%s'"), - domain, msgIdLang); + wxS("no suitable translation for domain '%s' found"), + domain); + return false; + } + + if ( LoadCatalog(domain, domain_lang) ) + { + wxLogTrace(TRACE_I18N, + wxS("adding '%s' translation for domain '%s' (msgid language '%s')"), + domain_lang, domain, msgIdLang); return true; } - return false; + // LoadCatalog() failed, but GetBestTranslation() returned non-empty language. + // That must mean that msgIdLanguage was used. + wxLogTrace(TRACE_I18N, + wxS("not using translations for domain '%s' with msgid language '%s'"), + domain, msgIdLang); + return true; } @@ -1444,23 +1433,19 @@ wxString wxTranslations::GetBestTranslation(const wxString& domain, wxString wxTranslations::GetBestTranslation(const wxString& domain, const wxString& msgIdLanguage) { - wxString lang = GetBestAvailableTranslation(domain); + // Determine the best language, including the msgId language, which is always + // available because it is present in the code: + wxString lang = DoGetBestAvailableTranslation(domain, msgIdLanguage); + if ( lang.empty() ) { - wxArrayString available; - available.push_back(msgIdLanguage); - available.push_back(msgIdLanguage.BeforeFirst('_')); - lang = GetPreferredUILanguage(available); - if ( lang.empty() ) - { - wxLogTrace(TRACE_I18N, - "no available language for domain '%s'", domain); - } - else - { - wxLogTrace(TRACE_I18N, - "using message ID language '%s' for domain '%s'", lang); - } + wxLogTrace(TRACE_I18N, + "no available language for domain '%s'", domain); + } + else if ( lang == msgIdLanguage || lang == msgIdLanguage.BeforeFirst('_') ) + { + wxLogTrace(TRACE_I18N, + "using message ID language '%s' for domain '%s'", lang, domain); } return lang; @@ -1468,7 +1453,21 @@ wxString wxTranslations::GetBestTranslation(const wxString& domain, wxString wxTranslations::GetBestAvailableTranslation(const wxString& domain) { - const wxArrayString available(GetAvailableTranslations(domain)); + // Determine the best language from the ones with actual translation file: + // As this function never considers the language of the original messages as being + // available, pass empty string as message ID language to the helper function. + return DoGetBestAvailableTranslation(domain, wxString()); +} + +wxString wxTranslations::DoGetBestAvailableTranslation(const wxString& domain, const wxString& additionalAvailableLanguage) +{ + wxArrayString available(GetAvailableTranslations(domain)); + if ( !additionalAvailableLanguage.empty() ) + { + available.push_back(additionalAvailableLanguage); + available.push_back(additionalAvailableLanguage.BeforeFirst('_')); + } + if ( !m_lang.empty() ) { wxLogTrace(TRACE_I18N,