1 /****************************************************************************
3 ** Copyright (C) 2015 The Qt Company Ltd.
4 ** Contact: http://www.qt.io/licensing/
6 ** This file is part of the documentation of the Qt Toolkit.
8 ** $QT_BEGIN_LICENSE:FDL$
9 ** Commercial License Usage
10 ** Licensees holding valid commercial Qt licenses may use this file in
11 ** accordance with the commercial license agreement provided with the
12 ** Software or, alternatively, in accordance with the terms contained in
13 ** a written agreement between you and The Qt Company. For licensing terms
14 ** and conditions see http://www.qt.io/terms-conditions. For further
15 ** information use the contact form at http://www.qt.io/contact-us.
17 ** GNU Free Documentation License Usage
18 ** Alternatively, this file may be used under the terms of the GNU Free
19 ** Documentation License version 1.3 as published by the Free Software
20 ** Foundation and appearing in the file included in the packaging of
21 ** this file. Please review the following information to ensure
22 ** the GNU Free Documentation License version 1.3 requirements
23 ** will be met: http://www.gnu.org/copyleft/fdl.html.
26 ****************************************************************************/
29 \example linguist/hellotr
30 \title サンプル: こんにちは tr()
32 これは、小さな Hello World プログラムを日本語に翻訳する例です。
35 \image linguist-hellotr_en.png
37 Qt アプリケーションの翻訳に関する詳細は、\l{Qt Linguist manual}
42 \snippet examples/ja_JP/linguist/hellotr/main.cpp 0
44 この行では、 QTranslator クラスの宣言を読み込みます。
45 QTranslator クラスのオブジェクトは、
48 \snippet examples/ja_JP/linguist/hellotr/main.cpp 5
50 親をもたない QTranslator オブジェクトを作成します。
52 \snippet examples/ja_JP/linguist/hellotr/main.cpp 6
54 プログラムで使用するソーステキストの日本語の翻訳を含む、
55 \c hellotr_ja.qm ( \c .qm というファイル拡張子は省略します)
57 ファイルが見つからなくてもエラーは発生しません。
59 \snippet examples/ja_JP/linguist/hellotr/main.cpp 7
61 \c hellotr_ja.qm の翻訳を、プログラムで使用する翻訳プールに追加します。
63 \snippet examples/ja_JP/linguist/hellotr/main.cpp 8
65 "Hello world!" を表示するプッシュボタンを作成します。
66 検索した \c hellotr_ja.qm に "Hello world!" の翻訳が含まれている場合、
68 含まれていない場合、翻訳前のテキストがそのまま表示されます。
70 QObject を継承するすべてのクラスには、\c tr() 関数が含まれます。
71 QObject クラスのメンバ関数内では、\c QPushButton::tr("Hello world!")
72 や \c QObject::tr("Hello world!") の代わりに、シンプルに
73 \c tr("Hello world!") を使います。
75 \section1 英語版のアプリケーションを実行する
77 翻訳ファイル \c hellotr_ja.qm の作成が終わっていないため、
78 以下のアプリケーションを起動したときに元の文字列が表示されます。
80 \image linguist-hellotr_en.png
82 \section1 日本語のメッセージファイルを作成する
84 最初のステップは、プロジェクトのすべてのソースファイルを列挙する
85 \c hellotr.pro を作成することです。
86 プロジェクトファイルは、qmake プロジェクトファイルまたは、通常の
88 以下の記述を含むプロジェクトファイルを作成してください。
90 \snippet examples/ja_JP/linguist/hellotr/hellotr.pro 0
91 \snippet examples/ja_JP/linguist/hellotr/hellotr.pro 1
93 \c TRANSLATIONS は、管理するメッセージファイルを指します。
96 ファイル拡張子は、\c .qm ではなく、\c .ts であることにご注意ください。
97 \c .ts は翻訳のソースファイルのフォーマットであり、
99 プログラマーまたはリリースマネージャーは \c lupdate プログラムを実行し、
100 ソースコードから抽出したソーステキストを使用して
102 翻訳者は、 \e {Qt Linguist} を使用して TS ファイルの読み取りと更新を行い、
105 TS の形式は、ユーザが直接閲覧可能な XML 形式であるため、
106 直接Eメールで送信したり、簡単にバージョン管理の対象にすることが出来ます。
107 このファイルを手動で編集する場合、XML の既定のエンコードは UTF-8 で、
108 Latin1(ISO 8859-1)ではないことに気をつけてください。
109 '\oslash'(ノルウェー語の o にスラッシュが付いたもの)などの
110 Latin1 文字を入力する1つの方法は、XML エンティティ "ø"
112 これはすべての Unicode 4.0 文字に対して有効です。
114 翻訳が完了したら、\c lrelease プログラムを使用して、
115 TS ファイルを QM ファイル(Qt Message ファイル)形式に変換します。
116 QM 形式は、極めて高速な検索性能を実現するようにデザインされた
118 \c lupdate と \c lrelease はどちらも、
119 プロジェクト全体のソースファイルとヘッダーファイル
120 (プロジェクトファイルの HEADERS および SOURCES 行で指定されている)
121 を読み取り、\c tr() 関数呼び出しの際に表示される文字列を抽出します。
123 \c lupdate は、メッセージファイル(この場合は \c hellotr_ja.ts)
124 の作成と更新を行い、これらをソースコードと同期させるために使用します。
125 \c lupdate にはデータの削除機能がないため、
126 \c lupdate はいつでも安全に実行できます。
127 例えば、ソースが変更されるたびに TS ファイルが更新されるよう、
130 それでは、以下のように \c lupdate を実行してみましょう:
132 \snippet doc/src/ja_JP/snippets/code/doc_src_examples_hellotr.qdoc 0
134 (\c -verbose オプションは、操作を説明するメッセージを表示するよう
137 \c hellotr_ja.ts ファイルが作成されていると思います:
139 \snippet doc/src/ja_JP/snippets/code/doc_src_examples_hellotr.qdoc 1
141 ツール (\c lupdate、 \e {Qt Linguist}、\c lrelease)
145 \section1 Qt Linguist を使用して日本語に翻訳する
147 XML やテキストエディタを使用して、TS ファイルを翻訳することも出来ますが、
148 ここでは \e {Qt Linguist} を使用して翻訳を行います。
150 \e {Qt Linguist} を起動するには、以下を入力します。
152 \snippet doc/src/ja_JP/snippets/code/doc_src_examples_hellotr.qdoc 2
154 左上のペインに "QPushButton" が表示されるはずです。
155 これをダブルクリックし、次に "Hello world!" をクリックして、
156 \gui Translation ペイン (ウィンドウ右中央)に
158 感嘆符(!)を忘れないように付けてください!
160 \gui{完了} チェックボックスをオンにして、
161 メニューバーから \gui{ファイル|保存} を選択します。
162 TS ファイルから、以下の記述がなくなります。
164 \snippet doc/src/ja_JP/snippets/code/doc_src_examples_hellotr.qdoc 3
168 \snippet doc/src/ja_JP/snippets/code/doc_src_examples_hellotr.qdoc 4
170 \section1 日本語版のアプリケーションを実行する
172 日本語版のアプリケーションを実行する前に、
173 TS ファイルから QM ファイルを生成する必要があります。
174 QM ファイルは、\e {Qt Linguist}(単一の TS ファイルの場合)のメニューから、
175 もしくは、コマンドラインプログラム \c lrelease を使用して生成できます。
177 プロジェクトファイルに列挙されている TS ファイルごとに
178 1 つの QM ファイルを作成することが出来ます。
179 \e {Qt Linguist} のメニューバーから \gui{ファイル|リリース} を選択し、
180 ポップアップ表示される \gui{ファイルの保存} ダイアログで\gui{保存}を選択し、
181 \c hellotr_ja.ts から \c hellotr_ja.qm を生成します。
182 今すぐ \c hellotr プログラムを再実行してみましょう。
183 これで、ボタンに "こんにちは、世界!" と表示されます。
185 \image linguist-hellotr_ja.png