June 28, 2011

Leon Leon
Robot Herder
400 posts

How can my program have 2 languages?

Page  
1

Hi! In my program i have an option at the preferences and you can change the language to English->Greek or Greek->English. I have a bool that when the language is Greek it is true and when it is English it is false. So what i am doing is at every message box or dialog i first check if the bool is greek or english and if it is true ( greek ) i translate the dialog to greek or if it is a messagebox the text is at greek.. So i am sure that this isn’t the correct way to do that.. Can you tell me what is the correct way to provide my program with more than the the english language?

I am using Ubuntu.

25 replies

June 28, 2011

loladiro loladiro
Lab Rat
596 posts

There’s a good overview at http://doc.qt.nokia.com/4.7/internationalization.html

June 28, 2011

Vass Vass
Hobby Entomologist
738 posts

Qt have good integrated system for it.
See QTranslator [developer.qt.nokia.com]

 Signature 


Vasiliy

June 28, 2011

Joey Dumont Joey Dumont
Lab Rat
116 posts

Just make sure that when you write user-visible stuff (like the title of a dialog) you put the option tr(“Insert text here”) enabled. This will ease the translation process.

I know you have accents in Greek, so it may be better to use

  1. trUtf8("Insert text here")

 Signature 

Joey Dumont

June 28, 2011

Andre Andre
Area 51 Engineer
6031 posts

Also, you might want to re-think your design for switching. What if your application becomes a best-seller, and you’d like to expand to Turkey? Or to Italy? Even if in the UI you keep the choice simple, I’d make sure it is easy to add other languages as well in the rest of your application.

 Signature 

Looking for Qt developers to join our team @ i-Optics: https://qt-project.org/forums/viewthread/25393/

June 28, 2011

Leon Leon
Robot Herder
400 posts

Joey Dumont wrote:
Just make sure that when you write user-visible stuff (like the title of a dialog) you put the option tr(“Insert text here”) enabled. This will ease the translation process.

I know you have accents in Greek, so it may be better to use

  1. trUtf8("Insert text here")

That’s what am i using..

Vass wrote:
Qt have good integrated system for it. See QTranslator [developer.qt.nokia.com]

Could i have some tips at this? I don’t know nothing about QTranslator.. Any example with a simple layer saying “Hello”, i don’t know something more than just a Class Reference :/

EDIT: I am now reading Qt Linguist Manual. I hope it will help me..

June 28, 2011

sigrid sigrid
Lab Rat
144 posts

The following FAQ [developer.qt.nokia.com] contains an example that can be useful.

June 28, 2011

Leon Leon
Robot Herder
400 posts

I just created my first translation file.. I will try to do it my own.. I will check the FAQ later.. Thanks :)

June 28, 2011

Joey Dumont Joey Dumont
Lab Rat
116 posts

If you consider your problem as solved, could you please the thread title and add [SOLVED].

Thanks!

 Signature 

Joey Dumont

June 28, 2011

soroush soroush
Dinosaur Breeder
781 posts

You should read documentations carefully and you will have no problem.

In case that you couldn’t managed to do the stuff, do these:

1- In your qt project file (.pro) add this line:

  1. TRANSLATIONS += program_gr.ts

Of course names and number of translations are optional.

2- start lupdate tool (which is an executable installed with Qt SDK) given your project file name. for example:

  1. lupdate ./program.pro

this scans your source code tree and determines translatable strings, then generates a XML-based translation file. in above example generates program_gr.ts.

3- Now you can perform actual translation! open your translation file with Qt Linguist and translate strings. then save the file and exit. It’s easy !

4- Now that you have translated strings, you should make binary translation file. just call lrealease and give your ts file name:

  1. lrealease program_gr.ts
this will generate profram_gr.qm that you can load in your program.

5- when application starts, load your qm file using QTranslator class. in a widget-based application it may look like this:

  1. #include <QtGui/QApplication>
  2. #include <QTranslator>
  3. #include "mainwindow.h"
  4.  
  5. int main(int argc, char *argv[])
  6. {
  7.     QApplication a(argc, argv);
  8.     QTranslator* translator;
  9.     translator->load(a.applicationDirPath()+"/program_gr.qm"); // or other place that you put your .qm file    
  10.     a.installTranslator(translator);
  11.     MainWindow w;
  12.     w.show();
  13.     return a.exec();
  14. }

note that you can load translations dynamically and use them whenever you need. but you should be careful about calling translation method for all of dialogs.

June 28, 2011

Leon Leon
Robot Herder
400 posts

Basically i saw a tutorial here ( http://www.youtube.com/watch?v=V9Gep6-r8ns ). It is the same with yours except that i can’t understant what the guy is saying at Indonesia language..

Also at step 5 i have this

  1. QTranslator translator;
  2.     translator.load("program_gr");
  3.     a.installTranslator(&translator);

Ok so i create the gm file. BUT. As i said my program is for Ubuntu so i will make a deb file ( like setup.exe files at Windows.. ).. How can i provide the greek translation file and how after the program is installed users can translate the app to greek? ( Install my greek translation file so they can see the app translated at greek )

June 28, 2011

soroush soroush
Dinosaur Breeder
781 posts

Ok, it looks like an “Installation and Deployment” problem…

In linux directories used for configuration files are:
/etc/program and /home/user_name/.program and /usr/share/program/

Due to linux file system standards I suggest put your translation files in /usr/share/program/translations.

June 28, 2011

Leon Leon
Robot Herder
400 posts

Ok thanks for the info! I will try it.. When everything is ok i will edit my post to [SOLVED] :D

June 28, 2011

Leon Leon
Robot Herder
400 posts

The translations at /usr/share/program/translations must be at .ts format or .qm format?

June 28, 2011

Vass Vass
Hobby Entomologist
738 posts
Leon wrote:
The translations at /usr/share/program/translations must be at .ts format or .qm format?

.qm

.ts format for developers (translation source)
.qm format for end users (translation binary)

 Signature 


Vasiliy

June 28, 2011

soroush soroush
Dinosaur Breeder
781 posts
Leon wrote:
The translations at /usr/share/program/translations must be at .ts format or .qm format?

You need only qm files in runtime. ts files are used to translate. at runtime you want to load translated data.
Also notice that there is no magic with /usr/share/program/translations. that was just a suggestion. you could put your translations in every other location…

Page  
1

  ‹‹ icons for pushbutton      [ENDED]set tooltip on an icon ››

You must log in to post a reply. Not a member yet? Register here!