PDA

View Full Version : Qt Creator Translation of Shortcuts



SvenA
11th June 2015, 21:42
Hello!

I have a problem with the translation of shortcuts in the QCreator.

I can shortcuts to menu entries in the designer of the QtCreator. This works
and I can use the shortcut in the program.

But if I add a translation to the shortcut (in Germany "Ctrl-P" is "Strg-P") using
the "qt-linguist" tool, the displayed shortcut vanishes from the menu and they
do not work any more.

If I deactivate the translation, it will work again.

I even tried to load the "Qt translations" (as mentioned in another post) by using
the following code, but it did not help:


qt_translator.load(QLibraryInfo::location(QLibrary Info::TranslationsPath)+"/qt_de");
installTranslator(&qt_translator);

Does anyone know a solution for this problem?

Regards
Sven

SvenA
23rd June 2015, 12:36
I'm still interested in a solution for this problem...
If nobody else have this problem, what do I'm wrong?!

mikag
19th July 2015, 22:58
To be honest I don't think translations for shortcuts is there for translated UI texts. It's only there so you can change the keys used by shortcuts in different languages.

Looking at the source generated by Creator it looks like the text returned from translations goes directly into QAction::setShortcut and it invokes the QKeySequence::QKeySequence QString constructor. And from that I guess you can see where this is going...

There is no way that I can see where you can setup a separate display text for a shortcut sequence. But looking at QKeySequence::toString I think you'll be able to see a solution. There must be a German translation for "Ctrl" in your resource files (in the "QShortcut" context) and then things might work as you expect. Just make sure NOT to check the translatable option for your shortcuts unless you need to change the actual keys pressed in other languages. And the translations in that case must still use the default Alt+, Shift+, Ctrl+ format or the QKeySequence constructor will fail to create a shortcut.

FYI. Just guessing from reading the docs, not tested for real yet. But I'm going there my self in the near future...

Ginsengelf
20th July 2015, 08:03
Hi, maybe QKeySequence (Qt::CTRL | Qt::Key_P) works.

Ginsengelf

jmg227
24th March 2017, 15:37
I just saw this question and I have had the same problem. The problem is resolved by installing multiple translators - one for your application and one for qt. I created a setLanguage() method for my application class that loads both of these translators:


void DwbApplication::setLanguage(QLocale locale)
{
if (m_qtXlator.load(locale, QLatin1String("qt"), QLatin1String("_"), QLatin1String(":/qtTranslations")))
{
installTranslator(&m_qtXlator);
}
if (m_dwbXlator.load(locale, QLatin1String("dwb_lang"), QLatin1String("_"), QLatin1String(":/translations")))
{
installTranslator(&m_dwbXlator);
}
}

In my case, I support English and German and I included qt_de.qm and qt_en.qm in a qtTranslations folder in my resource file. What's most important is that the application can find the file at runtime when it loads the translator.

The qt_xx.qm files can be found in the translations folder under your compiler toolchain folder, e.g. C:\Qt\Qt5.8.0\5.8\mingw53_32\translations in Windows (when Qt is installed in C:\Qt) or /opt/Qt/5.8/gcc_64/translations in Linux (when Qt is installed in /opt/Qt).

I hope this helps.

Jim

jmg227
24th March 2017, 15:51
There is one thing I forgot to mention in my previous post and this may be your issue since you are loading the Qt UI files.

If you look at the Qt translation files, (I'm running on windows so they are in C:\Qt\Qt5.8.0\5.8\mingw53_32\translations), you will see that qt_de.qm is suspiciously small. In fact, the Qt Linguist Manual (http://doc.qt.io/qt-5/linguist-programmers.html) states:
In Qt 5, the .qm files were split up by module and there is a so-called meta catalog file which includes the .qm files of all modules. The name of the meta catalog file is identical to the name of Qt 4's monolithic .qm file so that existing loader code works as before provided all included .qm files are found.

A solution is to use lconvert to build your own qt_de.qm that contains the strings for the modules that you need, for example,

lconvert -o installation_folder/qt_de.qm qtbase_de.qm qtdeclarative_de.qm
This can then be added to your resource file and QTranslator can load it without any problem.

An alternate solution is to put all of the .qm files for Qt (assistant_cs.qm, assistant_da.qm, assistant_de.qm, ...) into your resource file. Then when you load the meta-file, qt_de.qm, it will load fine because it will find all the files it references.