したがって、ドキュメントをさらに読んだ後、うまくいくと思われる解決策を見つけ、今後の参考のために調査結果を投稿したいと思いました。
1) 以下を使用して、文字列を翻訳用にマークできます。QT_TRANSLATE_NOOP(context, text)
2) 明示的に翻訳を取得するにはQCoreApplication::translate()
、c++ およびqsTranslate()
QML を使用します。次に、定義されたコンテキスト内で適切な翻訳が翻訳ファイルから検索されます。一致するものが見つからない場合、またはこれら 2 つの関数が使用されなかった場合は、元のテキストが返されます。
質問に投稿した例を次に示します。
QString MyClass::getError()
{
QString errorText = QT_TRANSLATE_NOOP("errorContex", "hardError1");
return errorText;
}
qDebug()<< getError(); //this will give you the original string
qDebug()<< QCoreApplication::translate("errorContex", getError()); //this will give you the translation of the string according to the set language
console.log(qsTranslate("errorContex", myclass.getError())) //this will give you the translation of the string in QML