13,005
回編集
編集の要約なし |
|||
| 223行目: | 223行目: | ||
<br> | <br> | ||
このマクロで囲まれた文字列は、翻訳ツールによって抽出可能になるが、実行時には通常の文字列として扱われる。<br> | このマクロで囲まれた文字列は、翻訳ツールによって抽出可能になるが、実行時には通常の文字列として扱われる。<br> | ||
つまり、QT_TR_NOOPマクロは、文字列を直接翻訳せずに、後で<code>tr</code>メソッドを使用して翻訳できるようにするために使用する。<br> | |||
<br> | <br> | ||
実際に翻訳を行う場合は、<code>QObject::tr</code>メソッド (現在のロケールに基づいて適切な翻訳を返す) を使用する。<br> | |||
<br> | |||
以下の例では、は実際の翻訳ファイル(.qmファイル)をロードするため、QTranslatorクラスを使用している。<br> | |||
InternationalWidget::showMessagesメソッドを実行して、翻訳されたメッセージを表示している。<br> | |||
<br> | |||
<syntaxhighlight lang="c++"> | |||
// InternationalWidget.hファイル | |||
#include <QCoreApplication> | |||
#include <QTranslator> | |||
#include <QLocale> | |||
#include <QDebug> | |||
class InternationalWidget : public QObject | |||
{ | |||
Q_OBJECT | |||
public: | |||
InternationalWidget(QObject *parent = nullptr) : QObject(parent) {} | |||
void showMessages() | |||
{ | |||
// QT_TR_NOOPを使用して、翻訳が必要な文字列を標識付けします | |||
const char* message1 = QT_TR_NOOP("Hello, World!"); | |||
const char* message2 = QT_TR_NOOP("Welcome to Qt!"); | |||
const char* message3 = QT_TR_NOOP("Goodbye!"); | |||
// 実際に翻訳を行う際は、QObject::tr()を使用します | |||
qDebug() << "Translated messages:"; | |||
qDebug() << tr(message1); | |||
qDebug() << tr(message2); | |||
qDebug() << tr(message3); | |||
} | |||
}; | |||
</syntaxhighlight> | |||
<br> | |||
<syntaxhighlight lang="c++"> | |||
// main.cppファイル | |||
#include "InternationalWidget.h" | |||
int main(int argc, char *argv[]) | |||
{ | |||
QCoreApplication app(argc, argv); | |||
// トランスレータを設定 (実際には、翻訳ファイルが必要) | |||
QTranslator translator; | |||
if (translator.load(QLocale(), QLatin1String("myapp"), QLatin1String("_"), QLatin1String(":/translations"))) { | |||
QCoreApplication::installTranslator(&translator); | |||
} | |||
InternationalWidget widget; | |||
widget.showMessages(); | |||
return app.exec(); | |||
} | |||
</syntaxhighlight> | |||
<br> | |||
==== Q_TRANSLATE_NOOP ==== | ==== Q_TRANSLATE_NOOP ==== | ||
国際化 (i18n) のために使用するマクロである。<br> | 国際化 (i18n) のために使用するマクロである。<br> | ||