Important: Please read the Qt Code of Conduct -

[solved] How to get the translated text without loading the translator file?

  • (Reposting in right place)

    Hello all,

    I have an interesting problem to solve.

    In my application, I want to get the translated text without loading the translator. Is that possible? If yes, Please let me know.

    For better explanation:
    I will have translator files (.ts & .qm).
    But I do not want to use QApplication::tr(”“); to do the translation.

    I want something like this:
    // translatedText = GetTranslatedText(“Hello”);
    // Use translatedText

    Appreciate your feedback..


  • Moderators

    sure...look at the QTranslator and/or QObject::tr() source code and copy the needed passages.

    But at the end i have to ask: why?!
    Why can't you just use QTranslator to "load": a qm-File and "translate": it? You do not necessarily install the QTranslator in your application, just use it when needed.

  • Thanks for your response Raven.

    This is basically to "test" the translation..

    Am writing an API, which will take "language" and "text" as arguments and return the translated text.

    std::string translatedString = GetTranslatedString(language, string);

    I will have listed of translated string with me, and use this API and get translated string from Qt, and compare.

    The main expectation from this API is, it should not load the translator file to the "application".

    Your answer makes sense to me.

    I can write this API in this way:
    std::string GetTranslatedString(int nLanguage, std::string &inputStr)
    if (nLanguage == GERMAN)
    QTranslator translator;
    return translate(inputStr);
    Basic idea is, for each language, get the string and verify.

    And, if there is any other better way where i can have some automated tests for translation, please suggest me.

    Thank you,

  • Moderators

    don't you trust QTranslator implementation to be correct? ^^

    But yes, the code you've posted is basically the idea i was talking about.

  • Raven,

    I trust Qt :)

    But also I would like to have a test framework to verify it once...

    Thanks for clarifying me :)


Log in to reply