What is the alternative for String.format() in GWT?
gwt
Solution
One elegant solution is using SafeHtml templates. You can define multiple such templates in an interface like:
public interface MyTemplates extends SafeHtmlTemplates {
@Template("The answer is - {0}")
SafeHtml answer(int value);
@Template("...")
...
}
And then use them:
public static final MyTemplates TEMPLATES = GWT.create(MyTemplates.class);
...
Label label = new Label(TEMPLATES.answer(42));
While this is a little bit more work to set up, it has the enormous advantage that arguments are automatically HTML-escaped. For more info, see https://developers.google.com/web-toolkit/doc/latest/DevGuideSecuritySafeHtml
If you want to go one step further, and internationalize your messages, then see also https://developers.google.com/web-toolkit/doc/latest/DevGuideI18nMessages#SafeHtmlMessages
Problem
While GWT is not emulate all java's core, what can be used as alternative for: ``` String.format("The answer is - %d", 42)? ``` What is the ellegant and efficient pattern to inject arguments to message in GWT?