From 4fab20cca1809715b5f6b8af2878dbaee69fca62 Mon Sep 17 00:00:00 2001 From: Vadim Zeitlin Date: Sun, 5 Nov 2023 02:12:58 +0100 Subject: [PATCH] Return true from AddCatalog() if message ID matches language When the original messages language matches the language of the locale being used, these strings can be used directly and so AddCatalog() should still return true for them but it didn't do it any more after the changes of 94b1a17aeb (Add AddAvailableCatalog() and use it in AddStdCatalog(), 2023-09-30). See #18227. Closes #24019. Closes #24037. (cherry picked from commit 39079cbf237e1a93a2acd97280cc2845a811f819) --- docs/changes.txt | 1 + src/common/translation.cpp | 22 ++++++++++++++++++++-- tests/intl/intltest.cpp | 17 ++++++++++++++++- 3 files changed, 37 insertions(+), 3 deletions(-) diff --git a/docs/changes.txt b/docs/changes.txt index 12b1dd5253..063f96d75e 100644 --- a/docs/changes.txt +++ b/docs/changes.txt @@ -238,6 +238,7 @@ Changes in behaviour which may result in build errors All: - Allow creating wxArrays from std::initialized_list (Lotendan, #23966). +- Fix regression in wxTranslations::AddCatalog() return value (#24019). All (GUI): diff --git a/src/common/translation.cpp b/src/common/translation.cpp index d1a22217bb..13708230b5 100644 --- a/src/common/translation.cpp +++ b/src/common/translation.cpp @@ -1446,9 +1446,27 @@ bool wxTranslations::AddCatalog(const wxString& domain, return true; const wxString msgIdLang = wxUILocale::GetLanguageCanonicalName(msgIdLanguage); - const wxString domain_lang = GetBestTranslation(domain, msgIdLang); - if ( msgIdLang == domain_lang ) + // 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 ) { wxLogTrace(TRACE_I18N, wxS("not using translations for domain '%s' with msgid language '%s'"), diff --git a/tests/intl/intltest.cpp b/tests/intl/intltest.cpp index f66e77eab0..f66c373f9c 100644 --- a/tests/intl/intltest.cpp +++ b/tests/intl/intltest.cpp @@ -237,7 +237,7 @@ void IntlTestCase::IsAvailable() CPPUNIT_ASSERT_EQUAL( origLocale, setlocale(LC_ALL, NULL) ); } -TEST_CASE("wxTranslations::Available", "[translations]") +TEST_CASE("wxTranslations::AddCatalog", "[translations]") { // We currently have translations for French and Japanese in this test // directory, check that loading those succeeds but loading others doesn't. @@ -268,6 +268,21 @@ TEST_CASE("wxTranslations::Available", "[translations]") trans.SetLanguage(wxLANGUAGE_ITALIAN); CHECK_FALSE( trans.AddAvailableCatalog(domain) ); } + + // And loading catalog using the same language as message IDs should + // "succeed" too, even if there is no such file, as in this case the + // message IDs themselves can be used directly. + SECTION("Untranslated") + { + trans.SetLanguage(wxLANGUAGE_GERMAN); + CHECK( trans.AddCatalog(domain, wxLANGUAGE_GERMAN) ); + + // Using a different region should still work. + CHECK( trans.AddCatalog(domain, wxLANGUAGE_GERMAN_SWISS) ); + + // But using a completely different language should not. + CHECK_FALSE( trans.AddCatalog(domain, wxLANGUAGE_DUTCH) ); + } } // The test may fail in ANSI builds because of unsupported encoding, but we