Trolltech Home | Qt-interest Home | Recent Threads | All Threads | Author | Date
All threads index page 1

Qt-interest Archive, June 2007
Hi allo, is it possible with qt-4.1.0 changing language at run time?


Message 1 in thread

Hi all,

I have such a fragment of code called by an event handler of my application:
if (glbexc.appl && glbexc.tor) {
	if (!strcmp(pRtView->getLanguage(),"ENG")) {
		glbexc.appl->removeTranslator(glbexc.tor);
		if (glbexc.tor->load( QString("ktool.") + QString("en"), 
glbexc.CurrentWDir ));
		else glbexc.tor->load( QString("ktool.") + QTextCodec::locale(), "." );
		glbexc.appl->installTranslator( glbexc.tor );
		}
	else if (!strcmp(pRtView->getLanguage(),"ITA")) {
		glbexc.appl->removeTranslator(glbexc.tor);
		if (glbexc.tor->load( QString("ktool.") + QString("it"), 
glbexc.CurrentWDir ));
		else glbexc.tor->load( QString("ktool.") + QTextCodec::locale(), "." );
		glbexc.appl->installTranslator( glbexc.tor );
		}
	}


my applicatio loiad the default traslator, and it ignore the eventual changes 
required by the previous one.

Does anyone know how to solve such a problem?


Cheers




-- 
 [ signature omitted ] 

Message 2 in thread

It may be safer to delete the QTranslatior :)

What I use for translation is this function:

/**
 Try to load and install translation for given language
 @param tr_lang Code of language to load
 @return true if successful, false if unsuccessful
 */
bool loadTranslation(const char *tr_lang) {
 static QTranslator *translator=NULL;
 if (translator) {
  qApp->removeTranslator(translator);
  delete translator;
  translator=NULL;
 }
 QString lang=QString("mpv_")+tr_lang;
 translator=new QTranslator();
 //Look for translation file in config directory in $HOME
 if (!translator->load(lang,QDir::home().path()+"/"+CONFIG_DIR+"/lang")) {
#ifndef WINDOWS_SYSTEM
  //look for translation file in DATA_PATH (unix-like systems only)
  if (!translator->load(lang,QString(DATA_PATH)+"/lang")) {
#endif
   //Look in binary path - testing compilations and windows builds
   if (!translator->load(lang,Settings::appPath()+"/lang")) {
    warn("Translation file " + lang + " not found");
    delete translator;
    translator=NULL;
    return false;
   }
#ifndef WINDOWS_SYSTEM
  }
#endif
 }
 qApp->installTranslator(translator);
 return true;
}

In main, I then invoke it by this:

 //Try LC_ALL, LC_MESSAGES and LANG - first one that is set is used
 const char *env_lang=getenv("LC_ALL");
 if (!env_lang) env_lang=getenv("LC_MESSAGES");
 if (!env_lang) env_lang=getenv("LANG");
 if (env_lang) {//LC_ALL/LC_MESSAGES/LANG variable is present in
environment -> attempt to load localization
  loadTranslation(env_lang);
 }

later, if I invoke the loadTranslation function with different
language code, I get different translation (although I have to reload
the strings in widgets when translation changes). It works ...

Martin Petricek


On 6/1/07, Fabio Giovagnini <fabio.giovagnini@xxxxxxxxxxxxxxx> wrote:
> Hi all,
>
> I have such a fragment of code called by an event handler of my application:
> if (glbexc.appl && glbexc.tor) {
>         if (!strcmp(pRtView->getLanguage(),"ENG")) {
>                 glbexc.appl->removeTranslator(glbexc.tor);
>                 if (glbexc.tor->load( QString("ktool.") + QString("en"),
> glbexc.CurrentWDir ));
>                 else glbexc.tor->load( QString("ktool.") + QTextCodec::locale(), "." );
>                 glbexc.appl->installTranslator( glbexc.tor );
>                 }
>         else if (!strcmp(pRtView->getLanguage(),"ITA")) {
>                 glbexc.appl->removeTranslator(glbexc.tor);
>                 if (glbexc.tor->load( QString("ktool.") + QString("it"),
> glbexc.CurrentWDir ));
>                 else glbexc.tor->load( QString("ktool.") + QTextCodec::locale(), "." );
>                 glbexc.appl->installTranslator( glbexc.tor );
>                 }
>         }
>
>
> my applicatio loiad the default traslator, and it ignore the eventual changes
> required by the previous one.
>
> Does anyone know how to solve such a problem?
>
>
> Cheers
>
>
>
>
> --
> Fabio Giovagnini

--
 [ signature omitted ] 

Message 3 in thread

This part was clear to me;
I ignored the retraslatinUi() member function.
Falko gave me the solition

Hello Fabio,

yes it is possible. But it is not quite easy.
You have to call retranslateUi() on all currently visible dialogs and
windows of your application.

Regards,
Falko


Thanks a lot for your answer.



Alle 23:11, domenica 3 giugno 2007, BH ha scritto:
> It may be safer to delete the QTranslatior :)
>
> What I use for translation is this function:
>
> /**
>  Try to load and install translation for given language
>  @param tr_lang Code of language to load
>  @return true if successful, false if unsuccessful
>  */
> bool loadTranslation(const char *tr_lang) {
>  static QTranslator *translator=NULL;
>  if (translator) {
>   qApp->removeTranslator(translator);
>   delete translator;
>   translator=NULL;
>  }
>  QString lang=QString("mpv_")+tr_lang;
>  translator=new QTranslator();
>  //Look for translation file in config directory in $HOME
>  if (!translator->load(lang,QDir::home().path()+"/"+CONFIG_DIR+"/lang")) {
> #ifndef WINDOWS_SYSTEM
>   //look for translation file in DATA_PATH (unix-like systems only)
>   if (!translator->load(lang,QString(DATA_PATH)+"/lang")) {
> #endif
>    //Look in binary path - testing compilations and windows builds
>    if (!translator->load(lang,Settings::appPath()+"/lang")) {
>     warn("Translation file " + lang + " not found");
>     delete translator;
>     translator=NULL;
>     return false;
>    }
> #ifndef WINDOWS_SYSTEM
>   }
> #endif
>  }
>  qApp->installTranslator(translator);
>  return true;
> }
>
> In main, I then invoke it by this:
>
>  //Try LC_ALL, LC_MESSAGES and LANG - first one that is set is used
>  const char *env_lang=getenv("LC_ALL");
>  if (!env_lang) env_lang=getenv("LC_MESSAGES");
>  if (!env_lang) env_lang=getenv("LANG");
>  if (env_lang) {//LC_ALL/LC_MESSAGES/LANG variable is present in
> environment -> attempt to load localization
>   loadTranslation(env_lang);
>  }
>
> later, if I invoke the loadTranslation function with different
> language code, I get different translation (although I have to reload
> the strings in widgets when translation changes). It works ...
>
> Martin Petricek
>
> On 6/1/07, Fabio Giovagnini <fabio.giovagnini@xxxxxxxxxxxxxxx> wrote:
> > Hi all,
> >
> > I have such a fragment of code called by an event handler of my
> > application: if (glbexc.appl && glbexc.tor) {
> >         if (!strcmp(pRtView->getLanguage(),"ENG")) {
> >                 glbexc.appl->removeTranslator(glbexc.tor);
> >                 if (glbexc.tor->load( QString("ktool.") + QString("en"),
> > glbexc.CurrentWDir ));
> >                 else glbexc.tor->load( QString("ktool.") +
> > QTextCodec::locale(), "." ); glbexc.appl->installTranslator( glbexc.tor
> > );
> >                 }
> >         else if (!strcmp(pRtView->getLanguage(),"ITA")) {
> >                 glbexc.appl->removeTranslator(glbexc.tor);
> >                 if (glbexc.tor->load( QString("ktool.") + QString("it"),
> > glbexc.CurrentWDir ));
> >                 else glbexc.tor->load( QString("ktool.") +
> > QTextCodec::locale(), "." ); glbexc.appl->installTranslator( glbexc.tor
> > );
> >                 }
> >         }
> >
> >
> > my applicatio loiad the default traslator, and it ignore the eventual
> > changes required by the previous one.
> >
> > Does anyone know how to solve such a problem?
> >
> >
> > Cheers
> >
> >
> >
> >
> > --
> > Fabio Giovagnini
>
> --
> To unsubscribe - send a mail to qt-interest-request@xxxxxxxxxxxxx with
> "unsubscribe" in the subject or the body. List archive and information:
> http://lists.trolltech.com/qt-interest/

-- 
 [ signature omitted ]