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)
This commit is contained in:
Vadim Zeitlin 2023-11-05 02:12:58 +01:00
parent 425d9455e8
commit 4fab20cca1
3 changed files with 37 additions and 3 deletions

View File

@ -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):

View File

@ -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'"),

View File

@ -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