[Solved] Get translated string without switching the language



  • Hello,

    I am using Qt translator for my translations. (using ts, qm, tr, etc)

    I would like to expose the translations to the outside world. Meaning, I have a class which is outside of Qt application that will know the "Source string", "Language" and it needs the "Translated String".

    How can I implement this?

    Does Qt provides a better way to do this?

    Couple of thoughts in my mind for this:

    1. Switch the language to the language that the string to be translated. Retrieve the translated string, keep it temporarily and switch back to previous language and return the string - Problem with this way is that, it is a time consuming operation and not really efficient. And one unknown will be, when we switch the language, not sure if the application language will change as well.
    2. Since .ts file is a XML format, can implement the parser separately and the parse can have an API to do this job.

    Please me know the best way to do it..

    Thank you,
    Kumra


  • Lifetime Qt Champion

    Hi,

    How are you exposing it ?



  • @SGaist I would like to expose through API.

    Example:
    translatedString = GetTranslation("Hello", french);

    translatedString shall contain - "Bonjour"


  • Lifetime Qt Champion

    Wouldn't QTranslator and it's translate function do the thing ?



  • @SGaist Sam, not really. Because,

    QString QTranslator::translate(const char * context, const char * sourceText, const char * disambiguation = 0, int n = -1) const
    

    Takes sourceText, but it does not take the language to which the string to be translated.

    Meaning, QTranslator translates the sourceText to the current language, not to the preferred language.

    Am I right?


  • Lifetime Qt Champion

    I haven't tested it but AFAIK you can load the translator with the translation file you want then call this method. It should only return the translation for the file you loaded.

    You can use several translator in your application, but you always have one per translation file



  • @SGaist That's interesting.

    If I relate correctly, if I have this kind of snippet, should do the job, right?

    
    QTranslator m_localTranslator;
    
    QString translateThis(const char * context, const char * sourceText, const char * languageName)
    {
        if (languageName == "French")
        {
            m_localTranslator.load("french.ts");
        }
        else if (languageName == "German")
        {
            m_localTranslator.load("german.ts");
        }
        else if (languageName == "Tamil")
        {
            m_localTranslator.load("tamil.ts");
        }
        else
        {
        }
        
        QString desText = m_localTranslator.translate(context, sourceText);
        
        return desText;
    }
    

    Without calling installTranslator will be able to get the translations right?

    Thanks,
    Kumara



  • Hi,

    for performance reason I suggest to use different translators (one per language) and select the one to use in you function.

    QMap<QString, QTranslator> m_traslatorMap;
    // For each language insert a translator in the map
    ....
    m_traslatorMap[languageName].translate(context, sourceText);
    


  • @mcosta Not sure where the problem is:

    <message>
    <location filename="formdata.cpp" line="19"/>
    <location filename="formdata.cpp" line="92"/>
    <location filename="formdata.cpp" line="106"/>
    <location filename="formdata.cpp" line="120"/>
    <location filename="formdata.cpp" line="137"/>
    <location filename="formdata.cpp" line="151"/>
    <location filename="formdata.cpp" line="168"/>
    <location filename="formdata.cpp" line="185"/>
    <source>Language</source>
    <translation>langue</translation>
    </message>

    m_localTranslator = new QTranslator(this);
    m_localTranslator->load("../FormData/french");
    QString test = m_localTranslator->translate("", "Language");
    qDebug() << test;
    

    Application Output shows nothing ""

    Should I need to call installTranslator or it should be translating even without that?


  • Lifetime Qt Champion

    You need to pass the class name where the corresponding tr can be found, no need to install the translator.



  • @SGaist Oh ya! That's the mistake in my code.

    <context>
       <name>MyBooh</name>
        <message>
            <source>Formula</source>
            <translation>Formule</translation>
        </message>
    </context>
    

    In the above sample .ts file, I had context name. So the translations where closely bounded with the name "MyBooh".

    So in the code, I should be doing something like

    QString test = m_localTranslator->translate("MyBooh", "Formula");
    

    Having said that, I do not want to bind the translations particular to the context.

    So I have removed the <name> tag.

    <context>
        <message>
            <source>Formula</source>
            <translation>Formule</translation>
        </message>
    </context>
    

    With this, I was able to translate in this way:

    QString test = m_localTranslator->translate("", "Formula");
    

    You guys help me a lot. Thanks for that. Come to India, I am going to treat you ;) Or I will come to Lausanne (Especially for Sam)

    Added to this,
    Since I do not have the name tag, Can I also remove the <context> tag, which is of no use?

    Thanks,
    Kumara


  • Lifetime Qt Champion

    That I don't know, you need to try. But I'd say a context with no name is still a context as in "global"



  • @SGaist OK. Thanks for the answer. I'll give a try.



Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.