how to clear QTranslator
-
Hi,
How are you setting up your translators ?
-
@SGaist said in how to clear QTranslator:
Hi,
How are you setting up your translators ?
Thanks for the reply.
From main.cpp, i created a translator object and installed it if it isn't English.
Then from another class i have another translator object, upon its instantiation i load the currently installed translation file into translator. Once the language option changes, I checked if it is not empty and remove translator. Then load again another file if the language selected is not English and install it.
I wanted to clear the translator after the removal so that only one translator will be loaded always.
I encountered a failure to remove the translator if by default another language was preloaded. I checked the location of the file, they're actually the same but failed to remove it.
-
@literA2 said in how to clear QTranslator:
From main.cpp, i created a translator object and installed it if it isn't English.
Do you really need to do it? just run lupdate and lrelease to create the English translation file that will not alter any string inside tr().
i.e. Use an untranslated .qm to make your program default to the original string -
@VRonin said in how to clear QTranslator:
can you show us the code where you add and remove the translator?
loadTranslator is being called also upon instantiation of the class to load the currently installed translator.
void loadTranslator (QString language) { if (language === "German") { translator.load("translation_de_DE", location); } else if (language === "French") { translator.load("translation_fr_FR", location); } else if (language === "Spanish") { translator.load("translation_es_ES", location); } } void changeLanguage(QString language) { if (!translator.isEmpty()) { qApp -> removeTranslator(&translator); // this returns false once there is an existing translator installed // if possible i wanted to clear here the translator } loadTranslator(language); if (!translator.isEmpty() && (language != "English")) { qApp -> installTranslator(&translator); } }
-
Are you re-creating the translator at some point ?
-
@SGaist said in how to clear QTranslator:
Are you re-creating the translator at some point ?
yes, as i mentioned i have a translator from the main and created another translator from another class. In another class i loaded the file that is currently installed based on the stored language.
-
Then aren't you trying to remove a translator that wasn't installed in the first place ?
-
@VRonin said in how to clear QTranslator:
The problem is if
&translator
used in installTranslator is the same as&translator
used in removeTranslator. where does translator live. if it's a local variable you are doing it wrongShouldn't that be the case? if you remove the previously installed translator, that is also what you need to pass for it to uninstall?
The translator in other class is private.
-
What I mean is:
Wrong:
{ QTranslator translator; translator.load("translation_de_DE", location); qApp -> installTranslator(&translator); } { QTranslator translator; qApp -> removeTranslator(&translator); }
Right:
QTranslator translator; { translator.load("translation_de_DE", location); qApp -> installTranslator(&translator); } { qApp -> removeTranslator(&translator); }
We'd need to see where
translator
is declared and from where do you call those functions to help you better -
@VRonin osrry, it isn't declared locally on the function. I have declared the it in the header file.
//Settings.h class Settings : public QObject { QTranslator translator; } // Settings.cpp Settings::Settings(QObject *parent) : QObject (parent) { loadTranslator(Profile::Language); }
-
You should also check that
translator.load
runs successfully