summaryrefslogtreecommitdiffstats
path: root/doc/src/ja_JP/examples/hellotr.qdoc
blob: 747fda6cfca8a4345e8b6ec86edcd54ee42f707b (plain)
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
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
/****************************************************************************
**
** Copyright (C) 2012 Digia Plc and/or its subsidiary(-ies).
** Contact: http://www.qt-project.org/legal
**
** This file is part of the documentation of the Qt Toolkit.
**
** $QT_BEGIN_LICENSE:FDL$
** Commercial License Usage
** Licensees holding valid commercial Qt licenses may use this file in
** accordance with the commercial license agreement provided with the
** Software or, alternatively, in accordance with the terms contained in
** a written agreement between you and Digia.  For licensing terms and
** conditions see http://qt.digia.com/licensing.  For further information
** use the contact form at http://qt.digia.com/contact-us.
**
** GNU Free Documentation License Usage
** Alternatively, this file may be used under the terms of the GNU Free
** Documentation License version 1.3 as published by the Free Software
** Foundation and appearing in the file included in the packaging of
** this file.  Please review the following information to ensure
** the GNU Free Documentation License version 1.3 requirements
** will be met: http://www.gnu.org/copyleft/fdl.html.
** $QT_END_LICENSE$
**
****************************************************************************/

/*!
    \example linguist/hellotr
    \title サンプル: こんにちは tr()

    これは、小さな Hello World プログラムを日本語に翻訳する例です。
    以下のスクリーンショットは英語版です。

    \image linguist-hellotr_en.png

    Qt アプリケーションの翻訳に関する詳細は、\l{Qt Linguist manual}
    をご覧ください。

    \section1 各行の簡単な解説

    \snippet examples/ja_JP/linguist/hellotr/main.cpp 0

    この行では、 QTranslator クラスの宣言を読み込みます。
    QTranslator クラスのオブジェクトは、
    ユーザに表示する文字列を翻訳します。

    \snippet examples/ja_JP/linguist/hellotr/main.cpp 5

    親をもたない QTranslator オブジェクトを作成します。

    \snippet examples/ja_JP/linguist/hellotr/main.cpp 6

    プログラムで使用するソーステキストの日本語の翻訳を含む、
    \c hellotr_ja.qm ( \c .qm というファイル拡張子は省略します)
    と呼ばれるファイルを読み込みます。
    ファイルが見つからなくてもエラーは発生しません。

    \snippet examples/ja_JP/linguist/hellotr/main.cpp 7

    \c hellotr_ja.qm の翻訳を、プログラムで使用する翻訳プールに追加します。

    \snippet examples/ja_JP/linguist/hellotr/main.cpp 8

    "Hello world!" を表示するプッシュボタンを作成します。
    検索した \c hellotr_ja.qm に "Hello world!" の翻訳が含まれている場合、
    その翻訳が表示されます。
    含まれていない場合、翻訳前のテキストがそのまま表示されます。

    QObject を継承するすべてのクラスには、\c tr() 関数が含まれます。
    QObject クラスのメンバ関数内では、\c QPushButton::tr("Hello world!")
    や \c QObject::tr("Hello world!") の代わりに、シンプルに
    \c tr("Hello world!") を使います。

    \section1 英語版のアプリケーションを実行する

    翻訳ファイル \c hellotr_ja.qm の作成が終わっていないため、
    以下のアプリケーションを起動したときに元の文字列が表示されます。

    \image linguist-hellotr_en.png

    \section1 日本語のメッセージファイルを作成する

    最初のステップは、プロジェクトのすべてのソースファイルを列挙する
    \c hellotr.pro を作成することです。
    プロジェクトファイルは、qmake プロジェクトファイルまたは、通常の
    makefile である可能性があります。
    以下の記述を含むプロジェクトファイルを作成してください。

    \snippet examples/ja_JP/linguist/hellotr/hellotr.pro 0
    \snippet examples/ja_JP/linguist/hellotr/hellotr.pro 1

    \c TRANSLATIONS は、管理するメッセージファイルを指します。
    この例では、日本語の翻訳のみ管理します。

    ファイル拡張子は、\c .qm ではなく、\c .ts であることにご注意ください。
    \c .ts は翻訳のソースファイルのフォーマットであり、
    アプリケーションの開発時に使用します。
    プログラマーまたはリリースマネージャーは \c lupdate プログラムを実行し、
    ソースコードから抽出したソーステキストを使用して
    TS ファイルの生成と更新を行います。
    翻訳者は、 \e {Qt Linguist} を使用して TS ファイルの読み取りと更新を行い、
    翻訳の追加と編集を行います。

    TS の形式は、ユーザが直接閲覧可能な XML 形式であるため、
    直接Eメールで送信したり、簡単にバージョン管理の対象にすることが出来ます。
    このファイルを手動で編集する場合、XML の既定のエンコードは UTF-8 で、
    Latin1(ISO 8859-1)ではないことに気をつけてください。
    '\oslash'(ノルウェー語の o にスラッシュが付いたもの)などの
    Latin1 文字を入力する1つの方法は、XML エンティティ "ø"
    を使用することです。
    これはすべての Unicode 4.0 文字に対して有効です。

    翻訳が完了したら、\c lrelease プログラムを使用して、
    TS ファイルを QM ファイル(Qt Message ファイル)形式に変換します。
    QM 形式は、極めて高速な検索性能を実現するようにデザインされた
    コンパクトなライブラリ形式です。
    \c lupdate と \c lrelease はどちらも、
    プロジェクト全体のソースファイルとヘッダーファイル
    (プロジェクトファイルの HEADERS および SOURCES 行で指定されている)
    を読み取り、\c tr() 関数呼び出しの際に表示される文字列を抽出します。

    \c lupdate は、メッセージファイル(この場合は \c hellotr_ja.ts)
    の作成と更新を行い、これらをソースコードと同期させるために使用します。
    \c lupdate にはデータの削除機能がないため、
    \c lupdate はいつでも安全に実行できます。
    例えば、ソースが変更されるたびに TS ファイルが更新されるよう、
    makefile に記述できます。

    それでは、以下のように \c lupdate を実行してみましょう:

    \snippet doc/src/ja_JP/snippets/code/doc_src_examples_hellotr.qdoc 0

    (\c -verbose オプションは、操作を説明するメッセージを表示するよう
    \c lupdate に指示します。)
    現在のディレクトリに、以下の内容で
    \c hellotr_ja.ts ファイルが作成されていると思います:

    \snippet doc/src/ja_JP/snippets/code/doc_src_examples_hellotr.qdoc 1

    ツール (\c lupdate、 \e {Qt Linguist}、\c lrelease)
    を使用して読み取りと更新を行うため、
    ファイル形式を理解する必要はありません。

    \section1 Qt Linguist を使用して日本語に翻訳する

    XML やテキストエディタを使用して、TS ファイルを翻訳することも出来ますが、
    ここでは \e {Qt Linguist} を使用して翻訳を行います。

    \e {Qt Linguist} を起動するには、以下を入力します。

    \snippet doc/src/ja_JP/snippets/code/doc_src_examples_hellotr.qdoc 2

    左上のペインに "QPushButton" が表示されるはずです。
    これをダブルクリックし、次に "Hello world!" をクリックして、
    \gui Translation ペイン (ウィンドウ右中央)に
    "こんにちは、世界!" と入力します。
    感嘆符(!)を忘れないように付けてください!

    \gui{完了} チェックボックスをオンにして、
    メニューバーから \gui{ファイル|保存} を選択します。
    TS ファイルから、以下の記述がなくなります。

    \snippet doc/src/ja_JP/snippets/code/doc_src_examples_hellotr.qdoc 3

    その代わりに以下が含まれます。

    \snippet doc/src/ja_JP/snippets/code/doc_src_examples_hellotr.qdoc 4

    \section1 日本語版のアプリケーションを実行する

    日本語版のアプリケーションを実行する前に、
    TS ファイルから QM ファイルを生成する必要があります。
    QM ファイルは、\e {Qt Linguist}(単一の TS ファイルの場合)のメニューから、
    もしくは、コマンドラインプログラム \c lrelease を使用して生成できます。
    \c lrelease を使用する場合、
    プロジェクトファイルに列挙されている TS ファイルごとに
    1 つの QM ファイルを作成することが出来ます。
    \e {Qt Linguist} のメニューバーから \gui{ファイル|リリース} を選択し、
    ポップアップ表示される \gui{ファイルの保存} ダイアログで\gui{保存}を選択し、
    \c hellotr_ja.ts から \c hellotr_ja.qm を生成します。
    今すぐ \c hellotr プログラムを再実行してみましょう。
    これで、ボタンに "こんにちは、世界!" と表示されます。

    \image linguist-hellotr_ja.png
*/