Start line:  
End line:  

Snippet Preview

Snippet HTML Code

Stack Overflow Questions
  * Copyright 2013 JBoss Inc
  * Licensed under the Apache License, Version 2.0 (the "License");
  * you may not use this file except in compliance with the License.
  * You may obtain a copy of the License at
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * See the License for the specific language governing permissions and
 * limitations under the License.
package org.jboss.errai.ui.shared.api.annotations;

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;
 public class CustomComponent extends Composite {
   private TranslationService translationService;
   private Button 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);
     String username = getCurrentUserName(); // hand wave, hand wave
     String messageToUserWithName = translationService.format(AppMessages.CUSTOM_MESSAGE_WITH_NAME, username);

public @interface TranslationKey {

Indicates the default value to use if no translation is available for the current locale.
New to GrepCode? Check out our FAQ X