[英]How to differentiate between translation did not exist or the translation is the same as source?
I am using QCoreApplication::translate()
to translate text. 我正在使用
QCoreApplication::translate()
来翻译文本。
I am trying to understand whether a string has a translation. 我试图了解字符串是否有翻译。
Qt documentation states: Qt文档说明:
If none of the translation files contain a translation for
sourceText
in context, this function returns aQString
equivalent ofsourceText
.如果没有翻译文件在上下文中包含
sourceText
的翻译,则此函数返回与sourceText
等效的QString
。
The problem I am facing is that I am getting results similar to this: 我面临的问题是我得到的结果与此类似:
<message>
<source>Side</source>
<translation>Side</translation>
</message>
Where source and translation are the same. 来源和翻译是相同的。
In many languages, the translation is indeed same as the source. 在许多语言中,翻译确实与来源相同。 But if
translate("Side")
returns "Side"
, I can't tell whether the translation was exactly "Side"
or whether the translation was empty. 但如果
translate("Side")
返回"Side"
,我无法判断翻译是否完全是"Side"
或翻译是否为空。
How can I differentiate between the two cases? 如何区分这两种情况?
AFAIK there is no way to differentiate between the two cases through a QTCoreApplication::translate
call. AFAIK无法通过
QTCoreApplication::translate
调用区分这两种情况。
However, QTranslator::translate
returns a null QString
when the key is not found (Qt 5). 但是,当找不到密钥时,
QTranslator::translate
返回空QString
(Qt 5)。 So one option would be to keep a container around with every QTranslator
you've added through installTranslator()
(since QCoreApplication
doesn't have a way to get those back). 因此,一种选择是通过
installTranslator()
添加每个QTranslator
来保持容器(因为QCoreApplication
没有办法让它们回来)。 Then, loop through that container, calling QTranslator::translate()
on each instance in the container. 然后,遍历该容器,在容器中的每个实例上调用
QTranslator::translate()
。 When you get a non-empty result, you found a translation; 当你得到一个非空的结果时,你找到了一个翻译; if no translator succeeded, then you know the key doesn't exist in any
QTranslator
you have. 如果没有翻译成功,那么你知道你所拥有的任何
QTranslator
中都不存在密钥。
bool hasTranslation(const char* key)
{
QString result;
if(!translators.size())
return false;
for(const auto& translator : translators)
{
result = translator->translate("context", key);
if(!result.isNull())
break;
}
return !result.isNull();
}
AFAIK, there is no way. AFAIK,没有办法。 The
translate
function works in the following way: it iterates over all the translators that are available for the application, and tries to translate the source text with each of them. translate
函数以下列方式工作:它遍历可用于应用程序的所有翻译器,并尝试使用每个翻译器翻译源文本。 If it succeeds, it immediately breaks out of the loop and returns the translated text. 如果成功,它会立即突破循环并返回已翻译的文本。 If the aforementioned loop has finished, but the translation has not been found, it returns the source text in a form of a QString:
如果上述循环已完成,但尚未找到转换,则它以QString的形式返回源文本:
QString QCoreApplication::translate(const char *context, const char *sourceText,
const char *disambiguation, Encoding encoding, int n)
{
QString result;
if (!sourceText)
return result;
if (self && !self->d_func()->translators.isEmpty()) {
QList<QTranslator*>::ConstIterator it;
QTranslator *translationFile;
for (it = self->d_func()->translators.constBegin(); it != self->d_func()->translators.constEnd(); ++it) {
translationFile = *it;
result = translationFile->translate(context, sourceText, disambiguation, n);
if (!result.isEmpty())
break;
}
}
if (result.isEmpty()) {
#ifdef QT_NO_TEXTCODEC
Q_UNUSED(encoding)
#else
if (encoding == UnicodeUTF8)
result = QString::fromUtf8(sourceText);
else if (QTextCodec::codecForTr() != 0)
result = QTextCodec::codecForTr()->toUnicode(sourceText);
else
#endif
result = QString::fromLatin1(sourceText);
}
replacePercentN(&result, n);
return result;
}
If you really need to be able to know if the translation could be found, you would have to subclass QTranslator
class and override it's translate()
function (since the translate()
in QCoreApplication
is non-virtual). 如果你真的需要知道是否可以找到翻译,你必须
QTranslator
类并覆盖它的translate()
函数(因为QCoreApplication
的translate()
是非虚拟的)。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.