Can a plugin use Translations from MuseScore?

• Dec 26, 2015 - 22:39

I'm working on a plugin where the user should select the voice on which the plugin applies. To do so, I've created a combobox/dropdown with the voices for a previously selected instrument. It would be great if I can use the same texts in the labels on that combobox as used in the tooltips of the voices, but it seems that's not possible?

On a side-question-note: Is there an easy way to know if a track contains notes? Most of the scores I work with mostly only contain notes in one or two voices, so it would be optimal if I could filter my plugins dropdown list to show only the used voices for the selected instrument.


Comments

I'm at this moment not quite understanding why this doesn't work, and it is very likely a lack of my understanding of Qt.
The plugin-specific translations are loaded/applied in mscorePlugins.cpp L:109 using the installTranslator method. Qt-Docs indicate that "Multiple translation files can be installed. Translations are searched for in the reverse order in which they were installed, so the most recently installed translation file is searched first and the first translation file installed is searched last." So in effect this should augment the existing translations of the qApp referenced there.
Looking up qApp reveals that only one application object should exist.

This leads me into thinking that a plugin somehow does have its own qApp instance, but that seems highly unlikely to me (as from what I found a plugin is loaded into a QmlPlugin, which inherits QQuickItem),
OR
more likely, MuseScore has found a way to separate their Translations from those of the plugins.

Perhaps the following could be a feature request: Instead of loading the translations for the plugin itself only, always first load the MuseScore main translation file as well, this should allow the plugin to leverage existing translations, but it should also prevent a plugin from overwriting texts in MuseScore itself (which is very much undesired behavior).

In reply to by jeetee

After doing some reading catch-up on translations in Qt, it seems that each translationKey is by default bound to the class it resides in. This is the reason that on Transifex a key like 'Voice: %1' is shown twice: once for its usage in 'note.cpp' and once for 'rest.cpp'.

It is possible in C++ to reference translations from another context, but I haven't found yet whether that is possible from QML as well..

In reply to by jeetee

Success!

In QML one can use qsTranslate(context, key) instead of qs(key) to use translations from other classes.

The translations from MuseScore are accessible from within a plugin, the only difficulty is finding out in which context the existing translationKey is defined. To do that, I've ran lupdate within libmscore, which resulted in a .ts-file (of currently 622 items).
That way I found the key 'Voice: %1' belonging to the context 'Ms::Note'. I used it in my plugin like this:
text: qsTranslate("Ms::Note", "Voice: %1").arg(5);

The plugin then doesn't need to include this translation in its own files.

In reply to by jeetee

Excellent! I guess I'll start using it in the notenames plugin.
Another (easier) way to find the context is to look up the string on Transifex.

Edit: here's an attempt to use the MuseScore Translation in the notenames plugin

Edit 2: my Freelinking: Unknown plugin indicator just got merged, so this will show up in the next nightly build and as such the next release too ;-)

Attachment Size
notenames.qml 10.74 KB

Do you still have an unanswered question? Please log in first to post your question.