zaphod.b
6th January 2014, 16:37
(Sry made a mistake while editing the post. This is a reconstruction.)
My app loads modules which themselves are responsible to load their translations. I wanted to delegate translation loading to a translatable module's parent class ctor in order to share code and, more importantly, ensure they are loaded before any widgets are created.
The following doesn't work - _action's text is untranslated "Welcome":
class Translatee
{
public:
Translatee(const QString &libname)
{
QString path("/path/to/qm/files/");
QTranslator translator;
bool ok = translator.load(libname + "_fr", path);
ok &= qApp->installTranslator(&translator);
//ok == true
}
}
class FooModule : public QObject
, public Translatee
{
Q_OBJECT
QAction *_action;
public:
FooModule(QObject *parent)
: QObject(parent)
, Translatee(metaObject()->className().toLower())
{
_action = new QAction(tr("Welcome"), this);
}
}
This does work - _action's text is "Bienvenue" as expected:
class FooModule : public QObject
{
Q_OBJECT
QAction *_action;
public:
FooModule(QObject *parent)
: QObject(parent)
{
QString libname(metaObject()->className().toLower());
QString path("/path/to/qm/files/");
QTranslator translator;
bool ok = translator.load(libname + "_fr", path);
ok &= qApp->installTranslator(&translator);
_action = new QAction(tr("Welcome"), this);
}
}
Supplement:
Must be a scope issue rather than an execution order issue:
class Translatee
{
public:
static bool load(const QString &libname)
{
QString path("/path/to/qm/files/");
QTranslator translator;
bool ok = translator.load(libname + "_fr", path);
ok &= qApp->installTranslator(&translator);
return ok;
}
}
class FooModule : public QObject
{
Q_OBJECT
QAction *_action;
public:
FooModule(QObject *parent)
: QObject(parent)
{
bool ok = Translatee::load(metaObject()->className().toLower());
_action = new QAction(tr("Welcome"), this);
}
}
This doesn't work either.
Why is this? Is there a way to delegate translation loading to before the module's ctor body is executed?
My app loads modules which themselves are responsible to load their translations. I wanted to delegate translation loading to a translatable module's parent class ctor in order to share code and, more importantly, ensure they are loaded before any widgets are created.
The following doesn't work - _action's text is untranslated "Welcome":
class Translatee
{
public:
Translatee(const QString &libname)
{
QString path("/path/to/qm/files/");
QTranslator translator;
bool ok = translator.load(libname + "_fr", path);
ok &= qApp->installTranslator(&translator);
//ok == true
}
}
class FooModule : public QObject
, public Translatee
{
Q_OBJECT
QAction *_action;
public:
FooModule(QObject *parent)
: QObject(parent)
, Translatee(metaObject()->className().toLower())
{
_action = new QAction(tr("Welcome"), this);
}
}
This does work - _action's text is "Bienvenue" as expected:
class FooModule : public QObject
{
Q_OBJECT
QAction *_action;
public:
FooModule(QObject *parent)
: QObject(parent)
{
QString libname(metaObject()->className().toLower());
QString path("/path/to/qm/files/");
QTranslator translator;
bool ok = translator.load(libname + "_fr", path);
ok &= qApp->installTranslator(&translator);
_action = new QAction(tr("Welcome"), this);
}
}
Supplement:
Must be a scope issue rather than an execution order issue:
class Translatee
{
public:
static bool load(const QString &libname)
{
QString path("/path/to/qm/files/");
QTranslator translator;
bool ok = translator.load(libname + "_fr", path);
ok &= qApp->installTranslator(&translator);
return ok;
}
}
class FooModule : public QObject
{
Q_OBJECT
QAction *_action;
public:
FooModule(QObject *parent)
: QObject(parent)
{
bool ok = Translatee::load(metaObject()->className().toLower());
_action = new QAction(tr("Welcome"), this);
}
}
This doesn't work either.
Why is this? Is there a way to delegate translation loading to before the module's ctor body is executed?