Qt Quick 国際化
Qt Quick アプリケーションの翻訳ファイル(TS と QM)を生成する。
サンプルアプリケーションを実行すると、QMLランタイムは自動的にシステム言語に基づいて、メインの.qmlファイルを含むディレクトリのi18n
サブディレクトリから翻訳を読み込みます。
翻訳可能なテキストをマークする
Main.qml
ファイルで、qsTr
コマンドを使用して、UI テキストを翻訳可能なものとしてマークします:
Text { text: qsTr("Hello")
翻訳を有効にする
main.cpp
ファイルで、QQmlApplicationEngine を使用して、メインの QML ファイルを含むディレクトリのi18n
というサブディレクトリから翻訳ファイルを自動的に読み込みます:
#include <QQmlApplicationEngine> int main(int argc, char *argv[]) { QGuiApplication app(argc, argv); QQmlApplicationEngine engine;
サブディレクトリi18n
の翻訳ファイル名には、qml_
というプレフィックスが必要です。例えば、qml_en_AU.ts
。
翻訳ファイルの作成
CMakeLists.txt
ファイルのfind_package
コマンドの値として、LinguistTools
モジュールを追加します:
find_package(Qt6 REQUIRED COMPONENTS Core Gui Quick Qml LinguistTools)
qt_add_translationsコマンドで生成する翻訳ファイルを定義します:
qt_add_translations(qmli18n RESOURCE_PREFIX /qt/qml/Translated/i18n TS_FILE_BASE qml TS_FILE_DIR i18n )
TSファイルとQMファイルを生成するためにアプリケーションをビルドします。
Note: Android では、QM ファイルをリソースとしてインクルードしてください。
翻訳の追加
新しい言語の翻訳を作成するには、i18n/base.tsをi18n/qml_<lang>.ts にコピーします。文字列を翻訳するために Qt Linguistを使って文字列を翻訳します。
© 2025 The Qt Company Ltd. Documentation contributions included herein are the copyrights of their respective owners. The documentation provided herein is licensed under the terms of the GNU Free Documentation License version 1.3 as published by the Free Software Foundation. Qt and respective logos are trademarks of The Qt Company Ltd. in Finland and/or other countries worldwide. All other trademarks are property of their respective owners.