Message Bundles: Message with reference to other message for localization
Added by Stefan Kurek over 1 year ago
I want to create messages that create forms and refer to other messages. Imagine I have a button which can be labeled with "Play!" or "Spielen!", so I write two messages:
<!-- Within the English message-bundle: -->
<message id="myButton">
<button>Play!</button>
</message>
<!-- Within the German message-bundle: -->
<message id="myButton">
<button>Spielen!</button>
</message>
In case of something simple like the example it's easy enough to do it that way. However, I'd like to separate the structure of a form from the texts used for the form. Therefore I'd like to do something like this:
<!-- Within the message-bundle for the structure: -->
<message id="myButton">
<button>${myButtonLabel}</button>
</message>
<!-- Within the English message-bundle: -->
<message id="myButtonLabel">
Play!
</message>
<!-- Within the German message-bundle: -->
<message id="myButtonLabel">
Spielen!
</message>
I'd do this, except I don't want to have to substitute myButton's variable with WTemplate::bindString("myButtonLabel", Wt::WString::tr("myButtonLabel"))
for every reference I feel the api could take care of themselves.
Is there syntax for this "message-in-message-reference"-case? I couldn't find anything, neither in the examples, nor the widget gallery, nor the documentation.
Replies (4)
RE: Message Bundles: Message with reference to other message for localization - Added by Matthias Van Ceulebroeck over 1 year ago
Hello Stefan,
you can take a look here. Specifically the 2nd section, about functions. This allows you to define special functions in a XML template like ${functionname:}
. The `WTemplate will then resolve this function if it is defined (by WTemplate::addFunction()).
There are three functions that are defined in Wt already, id
, tr
and block
.
id
allows you to retrieve theid
of certain widgets (handy for labeling / event handling)tr
allows you to embed localized strings by their id in templates <- I believe this is what you are looking forblock
allows you to embed other templates in templates (allowing for parameters to be passed)
I hope this helps!
Best,
Matthias
RE: Message Bundles: Message with reference to other message for localization - Added by Stefan Kurek over 1 year ago
Hello Matthias,
thank you for your help. This indeed seems to be what I'm looking for, but I can't get it working.
Consider this example of what I'm trying:
<message id="lbl.Txt">
Label-Text
</message>
<message >
<label for="${id:Some.Widget.Bound.Via.Code}">${tr:lblTxt}</label>
${Some.Widget.Bound.Via.Code}
</message>
In the definition of the label, the two placeholders are resolved to "??id:Some.Widget.Bound.Via.Code??" and "??tr:lbl.Txt??" respectively. Despite this, the widget is properly bound and useable.
I must be doing something wrong, I just can't figure out what it is.
Best regards
Stefan
RE: Message Bundles: Message with reference to other message for localization - Added by Stefan Kurek over 1 year ago
Update: I've figured out that, even though it says here that id
and tr
are predefined, one still has to manually add those functions as shown in the example: t->addFunction("tr", &Wt::WTemplate::Functions::tr);
Thanks for your help!
RE: Message Bundles: Message with reference to other message for localization - Added by Matthias Van Ceulebroeck over 1 year ago
Glad you got it working.
They are predefined in the sense that they exist, but they indeed are still to be added to the templates you wish to use them on.