Development/Tutorials/Localization/Language Change (de)

< Development‎ | Tutorials‎ | Localization
Revision as of 14:49, 21 December 2007 by DrSlowDecay (Talk | contribs) (Page created and first translations)

(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)
Jump to: navigation, search

Development/Tutorials/Localization/Language Change

Dealing with Language Changes
Anleitungsserie   Localization
Voriges Kapitel   Writing Applications With Localization in Mind
Nächstes Kapitel   n/a
Weiterführende Texte   n/a
Navigation   Deutsche Startseite

Eine Option zum Ändern der Sprache hinzufügen

It can be extremely useful for a user to be able to change the language of a specific application. It is fortunately extremely easy to add such functionality to your program.

If you are using the KXmlGuiWindow class for your main window, then you will already have an associated programname.rc file for it.

Simply modify this file to contain:

<Menu name="help">

   <Action name="switch_application_language"/>


And that is it! You will now have a Switch Application Language option in the Help menu.

However the user will need to restart the application for the changes to take effect.

Die GUI dynamisch ändern

One step better is to change the language on the fly, as soon as the user has selected a new language.

This can seem a fairly daunting task, as a typical program can contain many hundreds of translatable strings that will need to be retranslated, but fortunately a lot of the work is already done for, and just needs to be connected together.

When the language of an application is changed, a LanguageChanged QEvent is sent to every single widget. The idea is that in the top level window you will listen for this event, tell the GUI to retranslate, tell the toolbars to retranslate, and then let any custom widgets clean up themselves.

So in your class that inherits from KXmlGuiWindow, add to the header file:

 class MyWindow {
   void changeEvent( QEvent * event );
   void retranslateUi();
   KAction *mSomeAction;
   KAction *mQuitAction;

and to the source file: MyWindow::MyWindow() : KXmlGuiWindow( 0 ) {

 //setup the GUI.  There should be no i18n calls here, but instead
 //these calls should be in the retranslateUi() function
 //Set up the actions in the GUI
 mSomeAction = actionCollection()->addAction("something");
 connect(mSomeAction, SIGNAL(triggered(bool)), mWorkSpace, SLOT(newWorkSheet() ));
 //KStandardActions need to be handled careful.
 mQuitAction = NULL
 // Use the KXmlGuiWindow class to setup the GUI
 setupGUI(ToolBar | Keys | StatusBar | Create);


void MyWindow::changeEvent( QEvent * event ) {

 if (event->type() == QEvent::LanguageChange) {
   // This will setup the menu, toolbars etc again, using the new language
   setupGUI(ToolBar | Keys | StatusBar | Create);


void MyWindow::retranslateUi() {

 // Set the window title
 setPlainCaption( i18n( "My Window" ) );
 // For custom actions, just set the text again
 mMyAction->setText(i18n( "&Do Something..." ));
 if(mQuitAction) {
   //If we have already set this up before, create a new dummy action
   // and grab what info we need from it
   KAction *tmpQuitAction = KStandardAction::quit( NULL, NULL, NULL );
   delete tmpQuitAction;
 } else
   mQuitAction = KStandardAction::quit( this, SLOT( close() ), actionCollection() );


Although these is fairly complicated, that's about all that's needed. Just be careful whenever you add a new string somewhere that it can be translated.

The way it is done in Qt is to follow this pattern of having a retranslateUi() function that is called from both the constructor and from a LanguageChange event.