Skip to content

Commit 4fab20c

Browse files
committed
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 94b1a17 (Add AddAvailableCatalog() and use it in AddStdCatalog(), 2023-09-30). See wxWidgets#18227. Closes wxWidgets#24019. Closes wxWidgets#24037. (cherry picked from commit 39079cb)
1 parent 425d945 commit 4fab20c

File tree

3 files changed

+37
-3
lines changed

3 files changed

+37
-3
lines changed

docs/changes.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -238,6 +238,7 @@ Changes in behaviour which may result in build errors
238238
All:
239239

240240
- Allow creating wxArrays from std::initialized_list (Lotendan, #23966).
241+
- Fix regression in wxTranslations::AddCatalog() return value (#24019).
241242

242243
All (GUI):
243244

src/common/translation.cpp

Lines changed: 20 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1446,9 +1446,27 @@ bool wxTranslations::AddCatalog(const wxString& domain,
14461446
return true;
14471447

14481448
const wxString msgIdLang = wxUILocale::GetLanguageCanonicalName(msgIdLanguage);
1449-
const wxString domain_lang = GetBestTranslation(domain, msgIdLang);
14501449

1451-
if ( msgIdLang == domain_lang )
1450+
// Check if the original strings can be used directly.
1451+
bool canUseUntranslated = false;
1452+
if ( m_lang.empty() )
1453+
{
1454+
// If we are using the default language, check if the message ID
1455+
// language is acceptable for this system.
1456+
const wxString domain_lang = GetBestTranslation(domain, msgIdLang);
1457+
1458+
if ( msgIdLang == domain_lang )
1459+
canUseUntranslated = true;
1460+
}
1461+
else // But if we have a fixed language, we should just check it instead.
1462+
{
1463+
// Consider message IDs for another region using the same language
1464+
// acceptable.
1465+
if ( msgIdLang.BeforeFirst('_') == m_lang.BeforeFirst('_') )
1466+
canUseUntranslated = true;
1467+
}
1468+
1469+
if ( canUseUntranslated )
14521470
{
14531471
wxLogTrace(TRACE_I18N,
14541472
wxS("not using translations for domain '%s' with msgid language '%s'"),

tests/intl/intltest.cpp

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -237,7 +237,7 @@ void IntlTestCase::IsAvailable()
237237
CPPUNIT_ASSERT_EQUAL( origLocale, setlocale(LC_ALL, NULL) );
238238
}
239239

240-
TEST_CASE("wxTranslations::Available", "[translations]")
240+
TEST_CASE("wxTranslations::AddCatalog", "[translations]")
241241
{
242242
// We currently have translations for French and Japanese in this test
243243
// directory, check that loading those succeeds but loading others doesn't.
@@ -268,6 +268,21 @@ TEST_CASE("wxTranslations::Available", "[translations]")
268268
trans.SetLanguage(wxLANGUAGE_ITALIAN);
269269
CHECK_FALSE( trans.AddAvailableCatalog(domain) );
270270
}
271+
272+
// And loading catalog using the same language as message IDs should
273+
// "succeed" too, even if there is no such file, as in this case the
274+
// message IDs themselves can be used directly.
275+
SECTION("Untranslated")
276+
{
277+
trans.SetLanguage(wxLANGUAGE_GERMAN);
278+
CHECK( trans.AddCatalog(domain, wxLANGUAGE_GERMAN) );
279+
280+
// Using a different region should still work.
281+
CHECK( trans.AddCatalog(domain, wxLANGUAGE_GERMAN_SWISS) );
282+
283+
// But using a completely different language should not.
284+
CHECK_FALSE( trans.AddCatalog(domain, wxLANGUAGE_DUTCH) );
285+
}
271286
}
272287

273288
// The test may fail in ANSI builds because of unsupported encoding, but we

0 commit comments

Comments
 (0)