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
  *
  *      http://www.apache.org/licenses/LICENSE-2.0
  *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * 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.

AppMessages.java
 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";
 }
 

CustomComponent.java
 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);
   }
 }
 

Author(s):
eric.wittmann@redhat.com
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