PDA

View Full Version : QTranslator??



nthung
13th October 2011, 03:57
I am coding a program multilanguage, as
http://www.java2s.com/Code/Cpp/Qt/UsingQTranslator.htm
but I don't know how to make data. Could you create a data for me?
thanks

ChrisW67
13th October 2011, 05:48
I could create "a data" for you but then I would be the one "coding a program multilanguage"

It is all there in the docs: Linguist manual

nthung
13th October 2011, 07:44
I could create "a data" for you but then I would be the one "coding a program multilanguage"
It is all there in the docs: Linguist manual
This is may data, but it is still errors


<!DOCTYPE TS><TS>
<defaultcodec>iso8859-1</defaultcodec>
<context>
<name>QRadioButton</name>
<message>
<source>English</source>
<translation type="unfinished">Xin </translation>
</message>
<name>QRadioButton</name>
<message>
<source>Swedish</source>
<translation type="unfinished">Xin2 </translation>
</message>
</context>
</TS>

THanks

ChrisW67
13th October 2011, 07:51
Are we supposed to guess what you are doing with that file, what is generating an error message, and what that message is?

nthung
13th October 2011, 07:56
Are we supposed to guess what you are doing with that file, what is generating an error message, and what that message is?
When i open it by
Qt Linguist it has no error or warning.

ChrisW67
13th October 2011, 08:02
Uh huh. So what is the problem then?

nthung
13th October 2011, 10:25
Uh huh. So what is the problem then?
Sory

Could you give me an full example with its data?
Thanks

wysota
13th October 2011, 13:16
Sory

Could you give me an full example with its data?
Thanks

Example of a problem? :D

ChrisW67
14th October 2011, 00:37
I like this problem: Fermat's Last Theorem (http://en.wikipedia.org/wiki/Fermat%27s_Last_Theorem) Simple statement of problem, devilishly complex answer ;)

The pro file:


TEMPLATE = app
TARGET =
DEPENDPATH += .
INCLUDEPATH += .

# Input
SOURCES += main.cpp

TRANSLATIONS += \
simple_example_es.ts \
simple_example_fr.ts

The program:


#include <QtCore>
#include <QDebug>

int main(int argc, char *argv[])
{
QCoreApplication app(argc, argv);

QTranslator translator;
translator.load("simple_example_es");
// translator.load("simple_example_fr");
app.installTranslator(&translator);

qDebug() << QObject::tr("It is easy enough!");

return app.exec();
}

Then:


qmake
lupdate simple_example.pro
linguist simple_example_es.ts // input your translations to Spanish and mark as finished
linguist simple_example_fr.ts // ditto for French
lrelease simple_example.pro
make
./simple_example

Output:


"Es muy fácil!"

Try changing the loaded translation.

Apologies to any Spanish speakers if I have mangled your language... that's a Google Translate.

nthung
14th October 2011, 05:50
thanks so much.
Could you post data for me to run.?Because i don't see an example about data.
this is an example
http://doc.qt.nokia.com/latest/linguist-trollprint.html
but i can not run it as introduction.

wysota
14th October 2011, 10:02
What data do you want? Chris gave you a complete example with all the files.

nthung
14th October 2011, 10:45
What data do you want? Chris gave you a complete example with all the files.
I alway thanks you so much for good supports.
But I want to run one of examples in installed directory(C:\dev\Qt\2010.05\qt\examples\linguist) to understand about QTranslator.
I run them but none of them can run. I run lupdate, lrelease commands them raise erorrs. Maybe Qt installed incorrectly?
When i run
hellotr project and check

bool resultLaod=translator.load("hellotr_la");
then the resultLaod=false.
Thanks

wysota
14th October 2011, 10:55
What do you do step by step to build an example?

nthung
14th October 2011, 11:11
What do you do step by step to build an example?
Could you send me the one of examples then i run successfully?
Do you think that I installed Qt incorrectly?

wysota
14th October 2011, 11:15
No I couldn't. What good would it be if I send you the same files you already have? Just answer my previous question.

nthung
14th October 2011, 11:24
No I couldn't. What good would it be if I send you the same files you already have? Just answer my previous question.
thanks
I opened it by QtCreator then run
This is my project

/************************************************** **************************
**
** Copyright (C) 2010 Nokia Corporation and/or its subsidiary(-ies).
** All rights reserved.
** Contact: Nokia Corporation (qt-info@nokia.com)
**
** This file is part of the examples of the Qt Toolkit.
**
** $QT_BEGIN_LICENSE:BSD$
** You may use this file under the terms of the BSD license as follows:
**
** "Redistribution and use in source and binary forms, with or without
** modification, are permitted provided that the following conditions are
** met:
** * Redistributions of source code must retain the above copyright
** notice, this list of conditions and the following disclaimer.
** * Redistributions in binary form must reproduce the above copyright
** notice, this list of conditions and the following disclaimer in
** the documentation and/or other materials provided with the
** distribution.
** * Neither the name of Nokia Corporation and its Subsidiary(-ies) nor
** the names of its contributors may be used to endorse or promote
** products derived from this software without specific prior written
** permission.
**
** THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
** "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
** LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
** A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
** OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
** SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
** LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
** DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
** THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
** (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
** OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE."
** $QT_END_LICENSE$
**
************************************************** **************************/

#include <QApplication>
#include <QPushButton>
#include <QDebug>
//! [0]
#include <QTranslator>
//! [0]

//! [1] //! [2]
int main(int argc, char *argv[])
//! [1] //! [3] //! [4]
{
QApplication app(argc, argv);
//! [3]

//! [5]
QTranslator translator;
//! [5] //! [6]
bool x=translator.load("hellotr_la");
if(x==false)
qDebug()<<"Load fialed";
//! [6] //! [7]
app.installTranslator(&translator);
//! [4] //! [7]

//! [8]
QPushButton hello(QPushButton::tr("Hello world!"));
//! [8]
hello.resize(100, 30);

hello.show();
return app.exec();
}
//! [2]


!symbian: error(Only include this file for Symbian platforms)

RSS_RULES ="group_name=\"QtExamples\";"

vendorinfo = \
"; Localised Vendor name" \
"%{\"Nokia, Qt\"}" \
" " \
"; Unique Vendor name" \
":\"Nokia, Qt\"" \
" "

examples_deployment.pkg_prerules += vendorinfo
DEPLOYMENT += examples_deployment

isEmpty(ICON):ICON = $$QT_SOURCE_TREE/src/s60installs/qt.svg


#! [0]
SOURCES = main.cpp
#! [0] #! [1]
TRANSLATIONS = hellotr_la.ts
#! [1]

# install
target.path = $$[QT_INSTALL_EXAMPLES]/linguist/hellotr
sources.files = $$SOURCES *.pro
sources.path = $$[QT_INSTALL_EXAMPLES]/linguist/hellotr
INSTALLS += target sources

symbian: include($$QT_SOURCE_TREE/examples/symbianpkgrules.pri)


<?xml version="1.0" encoding="utf-8"?>
<!DOCTYPE TS>
<TS version="2.0" language="en_GB">
<context>
<name>QPushButton</name>
<message>
<source>Hello world!</source>
<translation>Xin chao</translation>
</message>
</context>
</TS>

wysota
14th October 2011, 11:26
Where did you put these files?

nthung
14th October 2011, 11:32
Where did you put these files?
The same directory
C:\dev\Qt\2010.05\qt\examples\linguist\hellotr

bool resultLaod=translator.load("hellotr_la");
then the resultLaod=false.

wysota
14th October 2011, 14:31
Copy them elsewhere, run qmake and make.

ChrisW67
15th October 2011, 00:28
Dead easy... I really cannot see the problem


$ cd /tmp
$ cp -r /usr/share/qt4/examples/linguist/hellotr .
$ cd hellotr
$ qmake
$ make

$ lupdate hellotr.pro
...
Updating 'hellotr_la.ts'...
Found 1 source text(s) (1 new and 0 already existing)
$ linguist hellotr_la.ts # translate the string
$ lrelease hellotr.pro
...
Updating '/tmp/hellotr/hellotr_la.qm'...
Generated 1 translation(s) (1 finished and 0 unfinished)
$ ./hellotr

nthung
15th October 2011, 02:20
Dead easy... I really cannot see the problem


$ cd /tmp
$ cp -r /usr/share/qt4/examples/linguist/hellotr .
$ cd hellotr
$ qmake
$ make

$ lupdate hellotr.pro
...
Updating 'hellotr_la.ts'...
Found 1 source text(s) (1 new and 0 already existing)
$ linguist hellotr_la.ts # translate the string
$ lrelease hellotr.pro
...
Updating '/tmp/hellotr/hellotr_la.qm'...
Generated 1 translation(s) (1 finished and 0 unfinished)
$ ./hellotr

thanks

bool loadReturn=translator.load(locale);
loadReturn=false. So it can translate.
Why is loadReturn =flase not true???
mycomputer can't run above commands.

ChrisW67
15th October 2011, 03:33
That code doesn't appear in the hellotr example. Anyway, upon reading the friendly manual (QTranslator::load()) I discover that:

Loads filename + suffix (".qm" if the suffix is not specified), which may be an absolute file name or relative to directory. Returns true if the translation is successfully loaded; otherwise returns false.


What is the value of the variable locale? Does it correspond to a *.qm file?