Home

OFF-SOFT.net

OFF-SOFT.net

This site is support & information site of WEB,and Software. This site might help you that create software or Web Site…perhaps?[:]

Qt (5) Check QtLinguist and the ts file

Published on| May 26th, 2009 | No Comment.
Description:
Qt provides international support as standard. (Multi-lingual and has a function)
This feature, wxWidget just like the po file. In addition, C + + Builder (VCL) is appropriate multilingual support (internationalization) and similar functions.

VC + +, enabling it to switch between files and resources, Qt, that XML file (ts file) edit the binary (pm file) to use a change. From a text file to convert this file to po wxWidget sense and is the same.

Features from XML source code will automatically print the file, C + + Builder (VCL) is similar to. C + + Builder (VCL) provides, GUI I was able to clean, Qt, and then run from the command line.

In this article, Qt written for the screen to switch from English to Japanese application.

Related Sites: http://doc.trolltech.com/4.5/i18n.html

Sample source code you used here:


Multilingual instructions
Multilingual steps, perform the following steps.

  • The string to support multi-language, or edit the source code
  • Source code (project) from the ts file (translation) and the output, translating
  • ts file (translation) to convert from pm file, copy it to the same directory as the executable

So, immediately, from editing source code, but I'll try.

The string to support multi-language, or edit the source code
For multilingual, you must do the following:.
  • Specify the string to convert to multi-lingual
  • The main routine, so as to execute the instructions to translate this application
Use the source code, Qt (2) Use a screen of menus used in the study the signals and slots.

The original source code was something like the following.
A time to view the menu, "Exit" menu items that alter the display is disabled.

[mainwindow.h]
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
#ifndef MAINWINDOW_H
#define MAINWINDOW_H
 
#include <QtGui>
 
class MainWindow : public QWidget
{
	Q_OBJECT
 
public:
	MainWindow();
 
private slots:
	void updateMenus();
 
private:
	QMenuBar *m_menu;
	QMenu *m_fileMenu;
	QAction *m_exit_act;
 
	bool m_active;
};
#endif

[mainwindow.cpp]
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
#include "mainwindow.h"
 
MainWindow::MainWindow()
{
	m_menu = new QMenuBar( this );
 
	m_fileMenu = m_menu->addMenu(tr("&File"));
 
	m_exit_act = m_fileMenu->addAction("E&xit" );
	m_exit_act->setShortcut(tr("Ctrl+Q"));
	m_exit_act->setStatusTip(tr("Exit the application"));
 
	connect(m_exit_act, SIGNAL(triggered()), 
	         qApp, SLOT(closeAllWindows()));
	connect(m_fileMenu, SIGNAL(aboutToShow()),
	         this, SLOT(updateMenus()));
 
    setWindowTitle(tr("SampleWindow"));
 
	m_active=true;
}
 
void MainWindow::updateMenus()
{
	m_exit_act->setEnabled(m_active);
	m_active=!m_active;
}

[mainwindow.cpp]
1
2
3
4
5
6
7
8
9
10
11
#include <QApplication>
 
#include "mainwindow.h"
 
int main(int argc, char *argv[])
{
    QApplication app(argc, argv);
    MainWindow mainWin;
    mainWin.show();
    return app.exec();
}

Actually, this is in the source code contains the code for some of the already multilingual.

mainwindow.cpp line of 7 "tr (" & File ")" there. The "tr", is one code for multiple languages.
This is to do the following.
  • Specify the string to convert to multi-lingual

It's easy. Every character you want to translate "tr ()" I should囲E.

If the translated text, QObject inheritance and if not, the code above is a compilation error.
In that case, "QObject:: tr ()" or, in the class definition, "Q_DECLARE_TR_FUNCTIONS" webmaster install it, like "tr ()"囲EMASU.
Below, Q_DECLARE_TR_FUNCTIONS embedded example.

1
2
3
4
5
6
7
8
 class MyClass
 {
     Q_DECLARE_TR_FUNCTIONS(MyClass)
 
 public:
     MyClass();
     ...
 };

In addition, namespace if you use does not translate correctly埋ME込MANAI following comments to the top of the source code.

1
/* TRANSLATOR NAMESPACE::CLASSNAME */

NAMESPACE: namespace name on
CLASSNAME: Name class

For example, if MyClass above, and below it.
1
2
3
4
5
6
7
8
9
10
11
#include "MyClass.h"
/* TRANSLATOR TESTNAME::MyClass */
 
using namespace TESTNAME;
 
MyClass::MyClass()
{
   :
}
   :
   :


This will be designated for a translated string.
	"&File"	
	"Ctrl+Q"	
	"Exit the application"	
	"SampleWindow"	
	
Here, the following text, please note that not be translated.
	"E&xit"

Then, in the main routine, try to translate the instructions to make a run this application.

[mainwindow.cpp]
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
#include <QApplication>
 
#include "mainwindow.h"
 
int main(int argc, char *argv[])
{
    QApplication app(argc, argv);
 
 
 
 
 
    MainWindow mainWin;
    mainWin.show();
    return app.exec();
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
#include <QApplication>
 
#include "mainwindow.h"
 
int main(int argc, char *argv[])
{
	QApplication app(argc, argv);
 
	QTranslator myappTranslator;
	myappTranslator.load("test_ja");
	app.installTranslator(&myappTranslator);
 
	MainWindow mainWin;
	mainWin.show();
	return app.exec();
}

From line 9 or 10 lines of code are added.

Now that I, in this application "test_ja.pm" and specify that the translation of the original file.

Automatically determines the language that TSUKAっthe user to display in that language, the 9-10 following the line.

1
2
3
4
5
6
7
8
	QTranslator qtTranslator;
	qtTranslator.load("qt_" + QLocale::system().name(),
	     QLibraryInfo::location(QLibraryInfo::TranslationsPath));
	app.installTranslator(&qtTranslator);
 
	QTranslator myappTranslator;
	myappTranslator.load("test_" + QLocale::system().name());
	app.installTranslator(&myappTranslator);

First part, Qt is a declaration to convert the characters to use.
"QLocale:: system (). Name ()" means that the language used by the user.
For example, in Japan, "ja_JP" output string.
The second part, "test_" the letter after the language, and specify that file to convert.

Specified in the case of the Japanese environment, test_ja.pm, test_ja_JP.pm automatically find you.

This code is included in the sample.

Source code (project) from the ts file (translation) and the output, translating

To embed source code (project) from the ts file (translation) to output.
Way, simply input the following command.
※ Before you launch this command, the project file (pro file) must be created.

(The following is just for reference, the command to create a project file)
1
2
3
4
C:\Program Files\Microsoft Visual Studio 9.0\VC> cd C:\temp
C:\temp> qmake -project
C:\temp> qmake
C:\temp> 

(Below, ts from the project file before you create a command file)
1
2
C:\temp> lupdate -pro temp.pro -ts test_ja.ts
C:\temp> 

You can only project files and command of the above parameters.
Then appended to the file of the project.

When you save it, you can only do the same in the following command parameters.
TRANSLATIONS += test_ja.ts

1
2
C:\temp> lupdate temp.pro
C:\temp> 
In addition, this work should have a go, the next re qmake project files are in test_ja.ts, automatically adds to the project file.
In other words, appended to the above project file, just a lot.

However, lupdate command, the additional characters (text), and additional characters have already ts files (text), Do nothing. That is, it overrides the translation.

The following XML file (test_ja.ts) and the output.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
<?xml version="1.0" encoding="utf-8"?>
<!DOCTYPE TS>
<TS version="2.0">
<context>
    <name>MainWindow</name>
    <message>
        <location filename="mainwindow.cpp" line="8"/>
        <source>&File</source>
        <translation type="unfinished"></translation>
    </message>
    <message>
        <location filename="mainwindow.cpp" line="11"/>
        <source>Ctrl+Q</source>
        <translation type="unfinished"></translation>
    </message>
    <message>
        <location filename="mainwindow.cpp" line="12"/>
        <source>Exit the application</source>
        <translation type="unfinished"></translation>
    </message>
    <message>
        <location filename="mainwindow.cpp" line="17"/>
        <source>SampleWindow</source>
        <translation type="unfinished"></translation>
    </message>
</context>
</TS>

Should only埋MERE between the translation of the following tags.
1
<translation type="unfinished"></translation>

For example, the following should webmaster install.
1
2
3
        <location filename="mainwindow.cpp" line="8"/>
        <source>&File</source>
        <translation type="unfinished">ファイル(&F)(</translation>

This time, QtLinguist so there's that, let's use it.
QtLinguist tools that I have the following directory.
Let's start by double clicking.
% Qt installation directory% \ qt \ bin \ linguist.exe



Immediately created to try to load the ts file.


Thus, it appears to specify the source and destination language translation language, specify the following.
Former Language Translation: English
Target language: Japanease
This designation is so little influence.
But now, because I do not know how to affect how safe you think you have set.


It displays the screen above.
?: Guest content (like the section name and class name)
Here, MainWindow class is not only one.

?: Characters for content translation.

?: The translated text for content, you have to see where the source code.
If you are in the screen, the screen image will appear.

?: Edit the text of the translation of the letter corresponding to the translated content.
Here, the specified character is displayed on the screen as a result of translation.

Here, we translate the following text.
	"&File"		-->	ファイル(&F)
	"Ctrl+Q"	-->	none
	"Exit the application"	-->	アプリケーションを終了します。
	"SampleWindow"	-->	サンプルウィンドウ

? When you complete the form, you save.

ts file (translation) to convert from pm file, copy it to the same directory as the executable
QtLinguist from the target, pm and then converted to a file.

From the menu [File] - [Release] When you click, automatically, test_ja.pm file.
You can convert from the command line as follows.
1
2
C:\temp> lrelease test_ja.ts
C:\temp> 

Test_ja.pm converted the file and copy it to the same directory as the executable.

Let's compile and run it.
I was converted to the Japanese title and the following menu.


The source code of the sample, MainWindow to display the status bar, we can see the translation.
	"Exit the application"	-->	アプリケーションを終了します。

"tr ()" macros used to enclose other way, there are ways to use the dynamic table.
But use it if there is a sense, the question remains.
The contents of this macro is just the string to create a simple table.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
//	transfer language map
struct __greeting3 {
	const char *source; const char *comment;
};
 
static __greeting3  transfer_strings3[] =
{
	 QT_TRANSLATE_NOOP3("classname_sample3", "Hello3", "hello"),
	 QT_TRANSLATE_NOOP3("classname_sample3", "Hello3", "goodbye")
};
 
static const char *transfer_strings2[] = {
     QT_TRANSLATE_NOOP("MainWindow", "Hello2"),
     QT_TRANSLATE_NOOP("MainWindow", "Goodbye2")
};
 
static const char *transfer_strings1[] = {
     QT_TR_NOOP("Hello1"),
     QT_TR_NOOP("Goodbye1")
};

To use this table, you need a method to convert.
The following is an example.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
//	transfer language method
QString MainWindow::transfer1(int type)
{
	return tr(transfer_strings1[type]);
}
QString MainWindow::transfer2(int type)
{
	return tr(transfer_strings2[type]);
}
QString MainWindow::transfer3(int type)
{
	return tr(transfer_strings3[type].source,
	    transfer_strings3[type].comment );
}

The use of this, may be if you want to manage and to prevent the insertion of the same character in the whole class and application.

But personally, I think I can use less.
At first glance, some of the samples, such as those above含MEMASHITA. I can also verify if you have trouble with various scopes.

I was like. This article is part of the source code and how to use, easy判RI, and the changes in detail, the most relevant sites are listed.
Very basic, so it was important, I have ventured to the article.

Related Sites: http://doc.trolltech.com/4.5/i18n.html

If you have noticed something, I would appreciate comments.

Comments

Leave a Reply







  • はてなブックマークへ追加する
  • Facebookでシェアする
  • twitter でつぶやく
  • Google Plusでシェアする
  • Pocketでシェアする
ページトップへ