HbQt about QT translation, translate, tr and trUtf8

299 views
Skip to first unread message
Assigned to jonc...@gmail.com by me

Luigi Ferraris

unread,
Aug 9, 2013, 9:59:45 AM8/9/13
to qtcon...@googlegroups.com
Hi Pritpal,
I'm try to understand and use QT translation.

I assume we want write multi language programs, so QLocale and QTranslator are known about their usage...
anyway everyone can find useful https://groups.google.com/forum/?hl=it&fromgroups#!searchin/qtcontribs/translation/qtcontribs/_VCn0rKbamQ/s-LMEureujcJ

As I understand there are (basically) three way:
a) QApplication:translate( const char * context, const char * sourceText, const char * disambiguation, Encoding encoding, int n )
b) QObject:tr( const char * sourceText, const char * disambiguation = 0, int n = -1 )
c) QObject:trUtf8( const char * sourceText, const char * disambiguation = 0, int n = -1 )

Everyone of us can write his custom translation file .ts, below a little example:
<?xml version="1.0" encoding="utf-8"?>
<!DOCTYPE TS>
<TS version="2.0" language="es">
<context>
    <name>Global</name>
    <message>
        <source>File</source>
        <translation>Fichero</translation>
    </message>
</context>
</TS>

About a) using HbQt can be like this:
   LOCAL oLabel
   WITH OBJECT oLabel := QLabel()
      :setText( QApplication():translate( "Global", "File" ) )
      ....
   END WITH

About b) or c) using HbQt can be like this:
   LOCAL oLabel
   WITH OBJECT oLabel := QLabel()
      :setText( :tr( "File" ) ) or :setText( :trUtf8( "File" ) )
      ....
   END WITH
  
But about b) and c) there is a big problem:
AFAIK, QT uses the object className as reference / context, infact there isn't "const char * context" as parameter. In this scenario, with my .ts the translator fails.

Following QT docs, I must change context as below
<context>
    <name>QLabel</name>

   
But if you have more QLabel objects your program (most probably) shows wrong lable text.
Perhaps using <comment> tag this problem can be solved... but reading QT docs they often write "...using subclass...".

In this scenario, the HbQt class inheritance must work very fine. Why?

1) I write .ts file as
<?xml version="1.0" encoding="utf-8"?>
<!DOCTYPE TS>
<TS version="2.0" language="es">
<context>
    <name>myLabel1</name>
    <message>
        <source>File</source>
        <translation>Fichero</translation>
    </message>
</context>
<context>
    <name>OtherLabel2</name>
    <message>
        <source>File</source>
        <translation>Salir</translation>
    </message>
</context>
</TS>


2) I use classes
CREATE CLASS myLabel1 INHERIT hb_QLabel
...
CREATE CLASS OtherLabel2 INHERIT hb_QLabel
...


3) I can write my program as
   LOCAL oLabel1, oLabel2

   WITH OBJECT oLabel1 := myLabel1():New()
      :setText( :tr( "File" ) )  <<==>> you will see "Fichero"
      ....
   END WITH

   WITH OBJECT oLabel2 := OtherLabel2():New()
      :setText( :tr( "File" ) )  <<==>> you will see "Salir"
      ....
   END WITH

  
First of all: can you confirm this behaviour?

If yes, we can have problems because (I remember) some objects have class inheritance problem (ie QTabWidget when inserTab used and the object is inherited from QWidget: always QWidget object is inserted and loose user object class name).

I hope is read as costructive post.

Cheers

Luigi Ferraris

Luigi Ferraris

unread,
Aug 13, 2013, 11:40:26 AM8/13/13
to qtcon...@googlegroups.com
Hi Pritpal,
can be activated this method QLocale::nativeLanguageName() ?

Cheers
Luigi Ferraris

Pritpal Bedi

unread,
Aug 13, 2013, 3:44:11 PM8/13/13
to qtcon...@googlegroups.com
Luigi

I'm try to understand and use QT translation.


I hope is read as costructive post.


Yep this is constructive post, no doubt.
I am extremely busy with job assignments and finding it hard to 
spare time to study and then, if I could, implement your idea.
May be next month I get some time to concentrate on this subject.


Pritpal Bedi 

Luigi Ferraris

unread,
Aug 14, 2013, 5:39:14 AM8/14/13
to qtcon...@googlegroups.com
Il 13/08/2013 21.44, Pritpal Bedi ha scritto:
> Yep this is constructive post, no doubt.
> I am extremely busy with job assignments and finding it hard to
> spare time to study and then, if I could, implement your idea.
> May be next month I get some time to concentrate on this subject.
>
>
> Pritpal Bedi
Hi Pritpal, many thanks for your answer. Ok.
When you will have time, I'm at your disposal to clarify my doubts.
Have a good job / time.

Cheers
Luigi Ferraris

Luigi Ferraris

unread,
Aug 28, 2013, 12:39:40 PM8/28/13
to qtcon...@googlegroups.com
Hi to everyone.

I'm happy to show an example of the same window translated using QT tran() method and .ts file only "switching" QT language


This is a (little) tribute to Pritpal for great job: many thanks!

Luigi Ferraris

Luigi Ferraris

unread,
Sep 5, 2013, 7:23:19 AM9/5/13
to qtcon...@googlegroups.com
Hi to everyone

I found powerful and useful QT translation system; I'm using QApplication:translate( context, textToBeTrasnlated ).

AFAIK this way follow this logic
1) find context AND textToBeTranslated text within a XML structure
2) return translation.

I think is fast when textToBeTranslated is short and not complex (I'm thinking when there are a lot of translations).
a) this can be light and fast: QApplication:translate( "TheContext", "&Press return" )
b) this can be heavy and slow: QApplication:translate( "TheContext", "&Insert a valid value and press return" )
The related .ts file will be
<context>
   <name>TheContext</name>
   <message>
      <source>&amp;Press return</source>
      <translation>&amp;Premi return</translation>
   </message>
   <message>
      <source>&amp;Insert a valid value and press return</source>
      <translation>&amp;Inserire un valore valido e premere return</translation>
   </message>


Please, pay attention to "&" character... to understand me when I say "complex" or "mismatching".

Another way is to use QObject:trId( "ID001" ) I think can be more light and fast and doesn't create problem with mismatching, but it's not implemented yet on HbQt.
The related .ts file (based upon previous example) will be
<context>
   <message id="ID001">
      <source>&amp;Press return</source>
      <translation>&amp;Premi return</translation>
   </message>
   <message id="ID002">
      <source>&amp;Insert a valid value and press return</source>
      <translation>&amp;Inserire un valore valido e premere return</translation>
   </message>


Can be enabled the QObject:trId method?

Cheers

Luigi Ferraris

Pritpal Bedi

unread,
Sep 5, 2013, 3:22:55 PM9/5/13
to qtcon...@googlegroups.com
Hi Luigi



Can be enabled the QObject:trId method?


QObject doe snot have this method. From where you picked it ?
Qt provides a standard function  
QString qtTrId ( const char * id, int n = -1 )
is this is what you are referring to ?


Pritpal Bedi

Luigi Ferraris

unread,
Sep 6, 2013, 9:25:53 AM9/6/13
to qtcon...@googlegroups.com
Il 05/09/2013 21.22, Pritpal Bedi ha scritto:
> QObject doe snot have this method. From where you picked it ?
> Qt provides a standard function
> QString qtTrId ( const char * id, int n = -1 )
> is this is what you are referring to ?
>
>
> Pritpal Bedi
Hi Pritpal.
I think yes... I look at this page
http://qt-project.org/wiki/QtInternationalization where is a brief
explanation related with a QLabel: for this reason I write QObject:...
On the other hand, I've not found anything related with QtTrId on
internet, so I can only suppose can be at QApplication level...
I read something here
https://github.com/Blizzard/qt/blob/master/src/corelib/global/qglobal.h,
but I don't know if solve your answer...

Cheers
Luigi Ferraris

Reply all
Reply to author
Forward
0 new messages