QObject::tr() not translating language properly - qt

QLocale systemLocale;
LOG_ERROR() << "SYSTEM LANGUAGE:" << systemLocale.languageToString(systemLocale.language());
LOG_ERROR() << QObject::tr("Welcome");
The second line prints the correct language, when I change the language from the phone settings, however, "Welcome" doesn't get translated to the current system language. What could be the issue with this?

The translation is probably not loaded. Here's how you can load a file:
QTranslator translator;
QString locale_string = QLocale().name();
QString filename = QString("my_app_%1").arg(locale_string);
if (translator.load(filename, "app/native/qm")) {
app.installTranslator(&translator);
}
This would try to load translations from app/native/qm/my_app_fr.qm on a french device, for example.
Note that by default, you'll have to restart the application after changing the device language. You can use a LocaleHandler to update the translation when the phone language changes. Listen to onSystemLanguageChanged() signal, remove the old translator, then load the new one (same code as above).

Related

openDDS not generating typesupportc header

Im running into an issue with getting TAO_idl not generating typesupportc header. the dds_TAOv2_all.sln builds just fine and all the examples generate their respectful typesupport files including the typesupportc.h file that is necessary for the typesupport_var in my IDL file.
module X {
#pragma DCPS_DATA_TYPE "X::packet"
#pragma DCPS_DATA_KEY "X::packet from"
typedef sequence<octet> binary;
struct packet {
string from;
long packet_id;
long count;
long timer;
binary mydata;
};
};
the Xtypesupportc.h was genereated before, but ever since I had to reload DDS(DDS is compiled configured etc) when I run tao_idl and openDDS_idl with the x.idl file the xtypesupportc and xtypesupports don't get created and thus I can't register the type. any obvious thing that I am doing wrong? thank you.

QTranslator checking

How to check which file with translation was loaded ? (Current loaded translation)
I load translation in main.c and I would like to check in MainWindow Class which translation was loaded.
I don't think you can do it, using Qt's methods.
The best way of doing it would be to write a wrapper around QTranslator and store all loaded translation files in it (you can load more than one translation file at a time).
Much worse, but easier way is to use a fake translation. Something like this:
const QString check = tr("lang");
if (check == "en") {
// it's english
} else if (check == "fr") {
// it's french
}
...
According to Internationalization with Qt, you anyway get the system locale to load the appropriate translation file. Just check the value of the locale string:
#include <QLocale>
QString locale = QLocale::system().name();
For example, for English, it is "en", for German - "de".

New in 4.7.4: QDir::homePath() gives out empty string

QString path = QDir::homePath(); // <-- "path" is always ""
Is this a new bug in 4.7.4? use to work well in 4.7.3
I’m on OSX Lion (4.7.3 worked on Lion well).
The “Clear System Environment” probably cleared the HOME environment variable too.
Basically, QDir::homePath() returns QFile::decodeName(QByteArray(::getenv("HOME"))) almost unchecked. And that's an empty string, if there is no HOME variable.
Hmmm It seems the problem is resolved if I don’t use the “Clear System Environment”. I unchecked it, then re-built and it worked fine. could it be the “SHELL” definitions? I can’t think of anything other than that that’s remotely related to this. I guess something caused Qt to have QT_NO_FSFILEENGINE defined and thus to return an empty string:
// from Qt source file: QDir.cpp
QString QDirPrivate()
{
#ifdef QT_NO_FSFILEENGINE
return QString();
#else
return cleanPath(QFSFileEngine::homePath());
#endif
}

Is there a way to output "hello world" in the application output window in Qt Creator when programm is run?

I'm trying to make something like plugin that outputs certain text when Qt creator runs any program. Does anyone have any idea what should I use to achieve that?
edit:
I need to make plugin that checks whether user has used appropriate style of programming. (Has put spaces where required for example) But I'm not sure how it is done so I try to start with some simple output while building.
You can use one of the debug statements:
qDebug() << "some debug info";
qWarning() << "a warning";

loading qm file using QTranslator

I'm trying to use translation files. I went through all the procedures:
created ts file, translated it, but when I run the application, the language is still the same as before.
I worked on the Nokia example, just like in the instructions.
What could be my problem?
int main(int argc, char *argv[])
{
QApplication app(argc, argv);
QTranslator* translator=new QTranslator(0);
if(QFile::exists("hellotr_la.qm"))
qWarning("failed-no file");
if(! translator->load("hellotr_la.qm"))
qWarning("failed loading"); //the warning appears ****
app.installTranslator(translator);
}
Where are the .qm files located? Your code is attempting to load the file from the current working directory, which can be anything during runtime. Specify a directory path in the call to QTranslator::load:
QTranslator* translator = new QTranslator();
if (translator->load("hellotr_la", "/path/to/folder/with/qm/files")) {
app.installTranslator(translator);
}
Translations can be loaded from Qt resources, so it is a good idea to bundle them inside your executables. Then you would load them somewhat like this:
QTranslator* translator = new QTranslator();
if (translator->load("hellotr_la", ":/resources/translations")) {
app.installTranslator(translator);
}
The answer was already given in a comment, but I want to point it out clearly.
The first warning uses a wrong condition:
if(QFile::exists("hellotr_la.qm"))
qWarning("failed-no file");
It should be:
if(!QFile::exists("hellotr_la.qm"))
qWarning("failed-no file");
Since you only saw the second warning, but not the first one, the problem is indeed that the file was not found. Make sure that the working directory is what you expect it to be or (better) use the resource system as explained by andref.
Based on the example, can you simply try this :
QTranslator translator;
translator.load("hellotr_la");
app.installTranslator(&translator);
Hope it will fix your problem !
Note 1 : No pointer here.
Note 2 : No extension in your filename.

Resources