这是我实施解决此问题的方法之一。
// setup (in my constructor before any use of mytr function
this->availableTranslations();
在我的 cpp 文件中...
QMap <QString, QString> SettingsWidget::trMap;
void SettingsWidget::availableTranslations()
{
if(trMap.size() != 0)
return;
trMap["true"] = tr("True","settings option");
trMap["false"] = tr("False","settings option");
trMap["Auto"] = tr("Auto","settings option");
trMap["None"] = tr("None","settings option");
trMap["smallest"] = tr("Smallest","settings option");
trMap["very small"] = tr("Very Small","settings option");
trMap["small"] = tr("Small","settings option");
trMap["medium"] = tr("Medium","settings option");
trMap["large"] = tr("Large","settings option");
trMap["very large"] = tr("Very Large","settings option");
trMap["Advanced"] = tr("Advanced","settings option");
trMap["Basic"] = tr("Basic","settings option");
}
QString SettingsWidget::mytr(QString s)
{
if(trMap.contains(s))
return trMap[s];//qApp->translate("SettingsWidget",qPrintable(s));
else
return s;
}
然后,当我动态使用上述内容时,它看起来像这样:
// in use
mytr(list.at(currIndex));
您会注意到,使用此设置,它可以根据变量而不是仅查找翻译char *
,并且无需太多额外工作或维护即可将其正确放入翻译文件中。
希望有帮助。