From 39079cbf237e1a93a2acd97280cc2845a811f819 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. --- src/common/translation.cpp | 22 ++++++++++++++++++++-- tests/intl/intltest.cpp | 17 ++++++++++++++++- 2 files changed, 36 insertions(+), 3 deletions(-) diff --git a/src/common/translation.cpp b/src/common/translation.cpp index d731165af0..0d58caf3a5 100644 --- a/src/common/translation.cpp +++ b/src/common/translation.cpp @@ -1346,9 +1346,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 ca3ceebdf3..9b14175fce 100644 --- a/tests/intl/intltest.cpp +++ b/tests/intl/intltest.cpp @@ -239,7 +239,7 @@ void IntlTestCase::IsAvailable() CPPUNIT_ASSERT_EQUAL( origLocale, setlocale(LC_ALL, nullptr) ); } -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. @@ -270,6 +270,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) ); + } } TEST_CASE("wxLocale::Default", "[locale]")