@Documented @Target(value=FIELD) @Retention(value=RUNTIME) public @interface TranslationKey
An annotation that provides a mechanism for the developer to declare translation strings from within the GWT application code. This extends the Errai i18n support so that translations can be used from Java code (not just from templates).
How does it work? I'm glad you asked. The developer must annotate a field which represents the
translation key. This key will then map to a value in the translation bundle file. Once the field
is annotated appropriately, the developer must directly invoke the TranslationService
's
format
method. This method call will perform a lookup in the translation service of
the value mapped to the provided key. Note that value substitution is supported via the {N}
format. See below for some examples.
package org.example.ui.client.local; public class AppMessages { @TranslationKey(defaultValue = "I guess something happened!") public static final String CUSTOM_MESSAGE = "app.custom-message"; @TranslationKey(defaultValue = "Hey {0}, I just told you something happened!") public static final String CUSTOM_MESSAGE_WITH_NAME = "app.custom-message-with-name"; }
package org.example.ui.client.local; @Dependent @Templated public class CustomComponent extends Composite { @Inject private TranslationService translationService; @Inject @DataField private Button someAction; @EventHandler("someAction") private void doLogin(ClickEvent event) { // do some action that may require a notification sent to the user String messageToUser = translationService.format(AppMessages.CUSTOM_MESSAGE); Window.alert(messageToUser); String username = getCurrentUserName(); // hand wave, hand wave String messageToUserWithName = translationService.format(AppMessages.CUSTOM_MESSAGE_WITH_NAME, username); Window.alert(messageToUserWithName); } }
Modifier and Type | Required Element and Description |
---|---|
String |
defaultValue
Indicates the default value to use if no translation is available for the current locale.
|
public abstract String defaultValue
Copyright © 2013-2015 JBoss, a division of Red Hat. All Rights Reserved.