Multilingual application

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.
User avatar
Nucleorion
Knows some wx things
Knows some wx things
Posts: 45
Joined: Sat Jan 07, 2017 10:08 pm

Re: Multilingual application

Post by Nucleorion »

Whell. And now, how is the best way to I save the lang selected?

I try whit config.ini but this is loaded after app.cpp
User avatar
doublemax
Moderator
Moderator
Posts: 19114
Joined: Fri Apr 21, 2006 8:03 pm
Location: $FCE2

Re: Multilingual application

Post by doublemax »

config.ini should work, you just have to load it before you create any GUI elements, i.e. at the beginning of wxApp::OnInit()
Use the source, Luke!
User avatar
Nucleorion
Knows some wx things
Knows some wx things
Posts: 45
Joined: Sat Jan 07, 2017 10:08 pm

Re: Multilingual application

Post by Nucleorion »

I've already tried that, but then it will not let me use it in main.cpp

I tried to put it in duplicate ie declare it and load it both in the app and in the mail. And works. But I do not think that's the right way to do it.
User avatar
doublemax
Moderator
Moderator
Posts: 19114
Joined: Fri Apr 21, 2006 8:03 pm
Location: $FCE2

Re: Multilingual application

Post by doublemax »

I've already tried that, but then it will not let me use it in main.cpp
Please explain. What's "it" in this sentence? Maybe show some code.
Use the source, Luke!
User avatar
Nucleorion
Knows some wx things
Knows some wx things
Posts: 45
Joined: Sat Jan 07, 2017 10:08 pm

Re: Multilingual application

Post by Nucleorion »

ExampleApp.h

Code: Select all

class MultilingualExampleApp : public wxApp
{
    public:
        virtual bool OnInit();
        wxLocale m_locale;

		wxFileConfig *ConfigINI = new wxFileConfig(wxEmptyString,
									wxEmptyString,
									wxPathOnly(wxStandardPaths::Get().GetDataDir()) + _T("\\config.ini"));

};
ExampleApp.cpp

Code: Select all

bool MultilingualExampleApp::OnInit()
{
	wxFileName f(wxStandardPaths::Get().GetExecutablePath());
	wxString appPath(f.GetPath()+ _T("\\config.ini"));

	ConfigINI->SetPath(appPath);

	ConfigINI->SetPath("/settings");
	wxString langSelected=ConfigINI->Read("Language","en_US");///Read("Nombre","Valor por defecto")

	m_locale.Init(wxLANGUAGE_SPANISH);//Init to the spanish
	wxLocale::AddCatalogLookupPathPrefix(wxT(".\\langs"));//add path
	m_locale.AddCatalog( wxT ("es"));//add catalog wxWidgetspath/locale/es.mo
	m_locale.AddCatalog(langSelected);//add my .mo edit files
ExampleMain.cpp

Code: Select all

void MultilingualExampleFrame::OnMenu_Language_English_Selected(wxCommandEvent& event)
{
	ConfigINI->SetPath("/settings");      //Error in this line
	ConfigINI->Write("Language","en_US");
	ConfigINI->Flush();//Force save data in .ini file
}
Error: 'ConfigINI' was not declared in this scope|
ONEEYEMAN
Part Of The Furniture
Part Of The Furniture
Posts: 7458
Joined: Sat Apr 16, 2005 7:22 am
Location: USA, Ukraine

Re: Multilingual application

Post by ONEEYEMAN »

Hi,
You main frame does not know about this. Just because it's public in the wxApp class doesn't mean it is accessible everywhere.
Create a function in wxApp-derived class that return this pointer and call it in the main frame class.

Thank you.
User avatar
doublemax
Moderator
Moderator
Posts: 19114
Joined: Fri Apr 21, 2006 8:03 pm
Location: $FCE2

Re: Multilingual application

Post by doublemax »

First of all, you should move the initialization of ConfigINI to the OnInit method.

Second: ConfigINI is defined in wxApp, so you can't access it directly from MultilingualExampleFrame.

Do it this way:

Code: Select all

ConfigINI = new wxFileConfig(wxEmptyString,
                           wxEmptyString,
                           wxPathOnly(wxStandardPaths::Get().GetDataDir()) + _T("\\config.ini"));

// set global config instance
wxConfigBase::Set( ConfigINI );

Code: Select all

void MultilingualExampleFrame::OnMenu_Language_English_Selected(wxCommandEvent& event)
{
   // get global config instance
   wxConfigBase *config = wxConfigBase::Get();
   if( config != NULL )
   {
     config->SetPath("/settings");
     config->Write("Language","en_US");
     config->Flush();//Force save data in .ini file
   }
}
Use the source, Luke!
User avatar
Nucleorion
Knows some wx things
Knows some wx things
Posts: 45
Joined: Sat Jan 07, 2017 10:08 pm

Re: Multilingual application

Post by Nucleorion »

I understand this:

ExampleApp.h

Code: Select all

class MultilingualExampleApp : public wxApp
{
    public:
        virtual bool OnInit();
        wxLocale m_locale;

	wxFileConfig *ConfigINI;

};
ExampleApp.cpp

Code: Select all

bool MultilingualExampleApp::OnInit()
{
ConfigINI = new wxFileConfig(wxEmptyString,
                           wxEmptyString,
                           wxPathOnly(wxStandardPaths::Get().GetDataDir()) + _T("\\config.ini"));

// set global config instance
wxConfig::SetPath( ConfigINI );		///Error in this line

	wxFileName f(wxStandardPaths::Get().GetExecutablePath());
	wxString appPath(f.GetPath()+ _T("\\config.ini"));

	ConfigINI->SetPath(appPath);

	ConfigINI->SetPath("/settings");
	wxString langSelected=ConfigINI->Read("Language","en_US");///Read("Nombre","Valor por defecto")

	m_locale.Init(wxLANGUAGE_SPANISH);//Init to the spanish
	wxLocale::AddCatalogLookupPathPrefix(wxT(".\\langs"));//add path
	m_locale.AddCatalog( wxT ("es"));//add catalog wxWidgetspath/locale/es.mo
	m_locale.AddCatalog(langSelected);//add my .mo edit files
ExampleMain.cpp

Code: Select all

void MultilingualExampleFrame::OnMenu_Language_English_Selected(wxCommandEvent& event)
{
   // get global config instance
   wxConfigBase *config = wxConfigBase::Get();
   if( config != NULL )
   {
     config->SetPath("/settings");
     config->Write("Language","en_US");
     config->Flush();//Force save data in .ini file
   }
}
error: cannot call member function 'virtual void wxRegConfig::SetPath(const wxString&)' without object

I think i dont understand where put lines
User avatar
Nucleorion
Knows some wx things
Knows some wx things
Posts: 45
Joined: Sat Jan 07, 2017 10:08 pm

Re: Multilingual application

Post by Nucleorion »

ONEEYEMAN wrote:Hi,
You main frame does not know about this. Just because it's public in the wxApp class doesn't mean it is accessible everywhere.
Create a function in wxApp-derived class that return this pointer and call it in the main frame class.

Thank you.
How I create a function in wxApp-derived class. Sorry, I need so know where is it :(
User avatar
doublemax
Moderator
Moderator
Posts: 19114
Joined: Fri Apr 21, 2006 8:03 pm
Location: $FCE2

Re: Multilingual application

Post by doublemax »

Code: Select all

wxConfig::SetPath( ConfigINI );      ///Error in this line
Sorry, this should be:

Code: Select all

wxConfigBase::Set( ConfigINI ); 
Use the source, Luke!
User avatar
Nucleorion
Knows some wx things
Knows some wx things
Posts: 45
Joined: Sat Jan 07, 2017 10:08 pm

Re: Multilingual application

Post by Nucleorion »

Code: Select all

wxConfigBase *config = wxConfigBase::Get();
This line is right? I get errors:

'wxConfigBase' was not declared in this scope
'config' was not declared in this scope
'wxConfigBase' is not a class, namespace, or enumeration
User avatar
doublemax
Moderator
Moderator
Posts: 19114
Joined: Fri Apr 21, 2006 8:03 pm
Location: $FCE2

Re: Multilingual application

Post by doublemax »

You need to include "wx/config.h" so that the compiler knows what wxConfigBase is.
Use the source, Luke!
User avatar
Nucleorion
Knows some wx things
Knows some wx things
Posts: 45
Joined: Sat Jan 07, 2017 10:08 pm

Re: Multilingual application

Post by Nucleorion »

Ok. Work fine now.

I had #include <wx/config.h> in ExampleMain.h

Believe that the main.h. are picked the Main.cpp by putting the line #include "ExampleMain.h"
Post Reply