[Solved] Get translated string without switching the language
-
Hi,
How are you exposing it ?
-
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?
-
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? -
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 -
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"