Use of MessageTools in code

Hi,
I am having trouble getting texts that are ‘generated’ code properly translated. Form the documentation it states one can use the MessageTools but I am unable to get it working. It seems that it does not find anything and as a result the texts are not translated.

Are there any examples using the MessageTools?

Link to documentation: https://doc.cuba-platform.com/manual-6.3/messageTools.html#messageTools

Any help appreciated.

1 Like

Hmm, again - after a lot of trying got it to work. Sorry for asking to quickly (but then again, you do provide excellent follow-up).

General solution:


    @Inject
    private MessageTools msgTools;

 ...

    someText = msgTools.loadString("msg://com.company.project.web.entity/MessageTag"); 

...

With an entry in the entity message.properties:


MessageTag = Some nice text to show

In fact, MessageTools.loadString() is more an internal tool, and you usually don’t need it. It’s for loading messages by keys specified in XML with “msg://” prefix or without it.

If you know a message pack and a key, use Messages:


@Inject
private Messages messages;
...
someText = messages.getMessage("com.company.project.web.entity", "MessageTag"); 

Messages has a lot of different variants of loading localized messages, see also JavaDocs.

In a screen controller, you can also use getMessage() method provided by the base class. It returns a message from a message pack specified for the screen.

Your last remark, which I only saw after rebuilding stuff to the use of Messages, actually is the solution that I was hoping for:

In a screen controller, you can also use getMessage() method provided by the base class. It returns a message from a message pack specified for the screen.

This greatly improves readability of the code and reduces coding effort. Thanks!