Add AddAvailableCatalog() and use it in AddStdCatalog()

The new function only returns true if the catalog could be really
loaded and not if it is considered not to be needed because the message
ID language (which is typically "en-US") happens to be present in the
preferred UI languages list (which seems to always include "en-US" in at
least Western European MSW).

This allows to distinguish, albeit in a rather awkward (but
backwards-compatible) way between having a translation for the given
language and not needed such translation.

It is still not clear if it is really correct to return "en-US" from the
list of preferred languages even if the user has never intentionally
configured the OS to indicate that English is acceptable, but at least
now we can work around this issue and use AddAvailableCatalog() in
AddStdCatalog() to make sure we only skip loading unversioned wxstd.mo
if the versioned wxstd-x.y.mo file is really found instead of never
doing it, as was the case until now (see #23886).

Also add GetBestAvailableTranslation() helper which seems more useful
than the existing GetBestTranslation() one and is similarly related to
it.

See #18227.
This commit is contained in:
Vadim Zeitlin 2023-09-30 20:52:53 +02:00
parent fd29d33489
commit 94b1a17aeb
4 changed files with 167 additions and 38 deletions

View file

@ -239,6 +239,39 @@ void IntlTestCase::IsAvailable()
CPPUNIT_ASSERT_EQUAL( origLocale, setlocale(LC_ALL, nullptr) );
}
TEST_CASE("wxTranslations::Available", "[translations]")
{
// We currently have translations for French and Japanese in this test
// directory, check that loading those succeeds but loading others doesn't.
wxFileTranslationsLoader::AddCatalogLookupPathPrefix("./intl");
const wxString domain("internat");
wxTranslations trans;
SECTION("All")
{
auto available = trans.GetAvailableTranslations(domain);
REQUIRE( available.size() == 2 );
available.Sort();
CHECK( available[0] == "fr" );
CHECK( available[1] == "ja" );
}
SECTION("French")
{
trans.SetLanguage(wxLANGUAGE_FRENCH);
CHECK( trans.AddAvailableCatalog(domain) );
}
SECTION("Italian")
{
trans.SetLanguage(wxLANGUAGE_ITALIAN);
CHECK_FALSE( trans.AddAvailableCatalog(domain) );
}
}
TEST_CASE("wxLocale::Default", "[locale]")
{
const int langDef = wxUILocale::GetSystemLanguage();