Reputation: 111
I am new to Qt creator and I learnt recently how to use Qt linguist. When I want to load the translation, I need to specify a directory which isn't a problem on my computer. But when I will give this Qt project to somebody else the directory is changes and the translation doesn't appear.
I tried to use different methods such as QDir::currentPath() but it gave me the path to the build folder that contains the app. What I need to access is the folder where the project is with the translated files and translated binaries (file_fr.qm, file_es.qm).
Thank you in advance for your help :-)
Upvotes: 0
Views: 1534
Reputation: 111
I managed to do it with Qt resource system, if you are looking for a short tutorial you can find one that explains how to set the resources by following the link.
Here is my code as well, in case it can help some of you:
#include <QApplication>
#include "ClassGenerator.h"
#include "ClassCode.h"
int main(int argc, char *argv[])
{
QApplication app(argc, argv);
QString locale = QLocale::system().name().section('_', 0, 0);
QTranslator translator;
translator.load(":/Translation/TPClassGenerator_"+locale+".qm");
app.installTranslator(&translator);
ClassGenerator Window;
Window.show();
return app.exec();
}
As you can see the language of the system is retrieved and use to pick up the right translation within the folder resources.
Thank you for the advice :-)
Upvotes: 1
Reputation: 4404
In order to get the path to your executable, use this method:
QCoreApplication::applicationDirPath()
You are deploying translation files which are located in the lang
folder of your executable path:
const QString LANGPATH(QCoreApplication::applicationDirPath() + "/lang");
QTranslator *translator = new QTranslator(this);
translator->load("file_fr.qm", LANGPATH);
QCoreApplication::installTranslator(translator);
You were using QDir::currentPath()
, but its result is the current working directory, not the directory containing your executable.
Upvotes: 1