Kadir Erdem Demir Kadir Erdem Demir - 1 year ago 49
C++ Question

How to differentiate between translation did not exist or the translation is the same as source?

I am using QCoreApplication::translate() to translate Text.

I am trying to understand there exist a translation.

Qt documentation states:

If none of the translation files contain a translation for sourceText
in context, this function returns a QString equivalent of sourceText.

The problem I am facing is that I am getting results similar to this:


Where source and translation are the same.

In many languages, the translation is indeed same as the source. But I don't know what did the library mean when it returned the same text.

How can I differentiate between the two cases: When the return value(translation) is the same as source or when the translation didn't exist (A Text same as source will be returned as well)?

Answer Source

AFAIK there is no way to differentiate between the two cases through a QTCoreApplication::translate call.

Anyway, QTranslator::translate returns a null QString when the key is not found (QT 5). So one option would be to keep a container around with all of the QTranslators you've added through installTranslator(). (since QTCoreApplication doesn't have a way to get those back) Then looping through that container, calling QTranslator::translate() on each instance in the container and assigning it to a QString, then finally checking if the QString is a null string and if it is, you know the key doesn't exist in any of the QTranslators.


bool hasTranslation(const char* key)
  QString result;
    return false;

  for(const auto& translator : translators)
    result = translator->translate("context", key);
  return !result.isNull();