changing a wxLocale object also changes wxFileDialog language Topic is solved

If you are using the main C++ distribution of wxWidgets, Feel free to ask any question related to wxWidgets development here. This means questions regarding to C++ and wxWidgets, not compile problems.
Post Reply
mael15
Super wx Problem Solver
Super wx Problem Solver
Posts: 449
Joined: Fri May 22, 2009 8:52 am
Location: Bremen, Germany

changing a wxLocale object also changes wxFileDialog language

Post by mael15 » Mon Aug 24, 2020 12:55 pm

My program can create pdfs in different languages. I store the selected language in a

Code: Select all

wxLocale *documentationLocale
object and change it with this code:

Code: Select all

bool wxTranslationHelperCustom::applyNewDocuLanguage(wxLanguage selLang)
{
	if (GetDocuLocale() && (wxLanguage)GetDocuLocale()->GetLanguage() == selLang)
		return false;

	wxString oldLang = GetDocuLocale() ? GetDocuLocale()->GetCanonicalName() : wxT("nix");
	if (GetDocuLocale())
		delete GetDocuLocale();
		
	documentationLocale = new wxLocale;
	bool ok = GetDocuLocale()->Init(selLang);
	GetDocuLocale()->AddCatalogLookupPathPrefix(m_LangDirPath);
	bool success = GetDocuLocale()->AddCatalog(appName, selLang, selLang != wxLANGUAGE_ENGLISH ? wxT("iso-8859-1") : wxT("UTF-8"));
	OutputDebugString(wxString::Format(wxT("new language selected: %s -> %s (Success: %s)\n"), oldLang, GetDocuLocale()->GetCanonicalName(), success ? wxT("yes") : wxT("no")));
	return true;
}
The problem is, the language used in wxMSW standard dialogs like wxFileDialog for "cancel" changes too. How can I keep this language from changing?

User avatar
doublemax
Moderator
Moderator
Posts: 15289
Joined: Fri Apr 21, 2006 8:03 pm
Location: $FCE2

Re: changing a wxLocale object also changes wxFileDialog language

Post by doublemax » Mon Aug 24, 2020 3:55 pm

The language of the native dialogs is tighly bound to the locale of the application, i don't think there is anything you can do about it. Can't you change the locale only during the generation of the PDFs and switch back afterwards?
Use the source, Luke!

mael15
Super wx Problem Solver
Super wx Problem Solver
Posts: 449
Joined: Fri May 22, 2009 8:52 am
Location: Bremen, Germany

Re: changing a wxLocale object also changes wxFileDialog language

Post by mael15 » Mon Aug 24, 2020 6:21 pm

doublemax wrote:
Mon Aug 24, 2020 3:55 pm
The language of the native dialogs is tighly bound to the locale of the application, i don't think there is anything you can do about it. Can't you change the locale only during the generation of the PDFs and switch back afterwards?
switching back would have to take place often because I also have a permanent preview of the pdf. But I could do it for the dialogs maybe.

mael15
Super wx Problem Solver
Super wx Problem Solver
Posts: 449
Joined: Fri May 22, 2009 8:52 am
Location: Bremen, Germany

Re: changing a wxLocale object also changes wxFileDialog language

Post by mael15 » Tue Aug 25, 2020 8:47 am

what part of my code has this global effect to change the wxFileDialog language? I thought I only change my documentationLocale?

utelle
Moderator
Moderator
Posts: 997
Joined: Tue Jul 05, 2005 10:00 pm
Location: Cologne, Germany
Contact:

Re: changing a wxLocale object also changes wxFileDialog language

Post by utelle » Tue Aug 25, 2020 11:55 am

mael15 wrote:
Tue Aug 25, 2020 8:47 am
what part of my code has this global effect to change the wxFileDialog language? I thought I only change my documentationLocale?
Calling wxLocale method Init has global side effects - as is stated in the wx documentation:
wx Documentation wrote: The call of this function has several global side effects which you should understand: first of all, the application locale is changed - note that this will affect many of standard C library functions such as printf() or strftime(). Second, this wxLocale object becomes the new current global locale for the application and so all subsequent calls to wxGetTranslation() will try to translate the messages using the message catalogs for this locale.
It would be nice, if wxWidgets would allow to use a wxLocale locally only without affecting the global locale. However, this doesn't seem to be possible in the current wxWidgets version.

If you only need to translate strings to a different language for your PDF output, you may try the wxTranslations class, which has no effect on the global locale.

Post Reply