Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/git/25.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
带变量的Qt定位_Qt - Fatal编程技术网

带变量的Qt定位

带变量的Qt定位,qt,Qt,我想在我的代码中翻译天气条件,但有点麻烦 例如: 英语-多云 法语-Nuageux weatherString = hash["weather"]; weatherBytes = weatherString.toLocal8Bit(); weatherCharArray = weatherBytes.data(); qDebug() << "Current Weather: " << QObject::tr(weatherCharArray); 在我的翻译标签中,我应

我想在我的代码中翻译天气条件,但有点麻烦

例如:

英语-多云

法语-Nuageux

weatherString = hash["weather"];
weatherBytes = weatherString.toLocal8Bit();
weatherCharArray = weatherBytes.data();

qDebug() << "Current Weather: " << QObject::tr(weatherCharArray);

在我的翻译标签中,我应该怎么做?

以下是我实现此问题解决方案的方法之一

// 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;
}
您会注意到,使用此设置,它可以基于变量而不是仅基于
char*
查找翻译,并且可以将其正确地放入翻译文件中,而无需进行太多额外的工作或维护


希望能有所帮助。

我想过时的标签只意味着源文件中找不到
Cloudy
,所以Qt认为它被删除了(因此,它是过时的)。但我看不出标签是如何阻止翻译的。如果它困扰您,只需创建
QMap
,关键字是英文单词,值是
tr(key)
的结果。不幸的是,标记确实阻止了翻译。
lrelease
文档,它只编译标记为
已完成的翻译。因此,您应该在启动
lrealease
之前修改标记(例如,使用自定义实用程序和特定的翻译注释),或者以某种方式将来自服务器的所有字符串放入源代码中。是的,我可以添加来自服务器的所有字符串,因为我知道它生成的每个可能的字符串,我希望这样做。我可以手动将标记设置为“完成”吗?我试过了,它又回到了过时状态。它不会“返回”,因为它在源代码中找不到源字符串,所以更新它;我不知道如何强制它不这样做(除了完全删除手动添加的消息,使用适当的
tr()
调用向源添加字符串,并让
lupdate
完成它的工作)。
// setup (in my constructor before any use of mytr function
this->availableTranslations();
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));