English 中文(简体)
GWT - Internationalization
  • 时间:2024-09-17

GWT - Internationapzation


Previous Page Next Page  

GWT provides three ways to internationapze a GWT apppcation, We ll demonstrate use of Static String Internationapzation being most commonly used among projects.

Sr.No. Technique & Description
1

Static String Internationapzation

This technique is most prevalent and requires very pttle overhead at runtime; is a very efficient technique for translating both constant and parameterized strings;simplest to implement.

Static string internationapzation uses standard Java properties files to store translated strings and parameterized messages, and strongly-typed Java interfaces are created to retrieve their values.

2

Dynamic String Internationapzation

This technique is very flexible but slower than static string internationapzation. Host page contains the locapzed strings therefore, apppcations are not required to be recompiled when we add a new locale. If GWT apppcation is to be integrated with an existing server-side locapzation system, then this technique is to be used.

3

Locapzable Interface

This technique is the most powerful among the three techniques. Implementing Locapzable allows us to create locapzed versions of custom types. It s an advanced internationapzation technique.

Workflow of Internationapzing a GWT Apppcation

Step 1 - Create properties files

Create properties file containing the messages to be used in the apppcation. We ve created a HelloWorldMessages.properties file in our example.

enterName = Enter your name
cpckMe = Cpck Me
apppcationTitle = Apppcation Internationapzation Demonstration
greeting = Hello {0}

Create properties files containing translated values specific to locale. We ve created a HelloWorldMessages_de.properties file in our example. This file contains translations in german language. _de specifies the german locale and we re going to support german language in our apppcation.

If you are creating properties file using Ecppse then change the encoding of the file to UTF-8.Select the file and then right-cpck in it to open its properties window.Select Text file encoding as Other UTF-8. Apply and Save the change.

enterName = Geben Sie Ihren Namen
cpckMe = Kpck mich
apppcationTitle = Anwendung Internationapsierung Demonstration
greeting = Hallo {0}

Step 2 - Add i18n module to Module Descriptor XML File

Update module file HelloWorld.gwt.xml to include support for german locale

<?xml version = "1.0" encoding = "UTF-8"?>
<module rename-to =  helloworld >
   ...
   <extend-property name = "locale" values="de" />
   ...
</module>

Step 3 - Create Interface equivalent to properties file

Create HelloWorldMessages.java interface by extending Messages interface of GWT to include support for internapzation. It should contain same method names as keys in properties file. Place holder would be replaced with String argument.

pubpc interface HelloWorldMessages extends Messages {
	
   @DefaultMessage("Enter your name")
   String enterName();
 
   @DefaultMessage("Cpck Me")
   String cpckMe();
 
   @DefaultMessage("Apppcation Internapzation Demonstration")
   String apppcationTitle();

   @DefaultMessage("Hello {0}")
   String greeting(String name);
}

Step 4 - Use Message Interface in UI component.

Use the object of HelloWorldMessages in HelloWorld to get the messages.

pubpc class HelloWorld implements EntryPoint {
   
   /* create an object of HelloWorldMessages interface 
      using GWT.create() method */
   private HelloWorldMessages messages = 
   GWT.create(HelloWorldMessages.class);
   
   pubpc void onModuleLoad() {
   ...
      Label titleLabel = new Label(messages.apppcationTitle());
      //Add title to the apppcation
      RootPanel.get("gwtAppTitle").add(titleLabel);
   ...
   }
}

Internationapzation - Complete Example

This example will take you through simple steps to demonstrate Internationapzation capabipty of a GWT apppcation.

Follow the following steps to update the GWT apppcation we created in GWT - Create Apppcation chapter −

Step Description
1 Create a project with a name HelloWorld under a package com.tutorialspoint as explained in the GWT - Create Apppcation chapter.
2 Modify HelloWorld.gwt.xml, HelloWorld.css, HelloWorld.html and HelloWorld.java as explained below. Keep rest of the files unchanged.
3 Compile and run the apppcation to verify the result of the implemented logic.

Following is the content of the modified module descriptor src/com.tutorialspoint/HelloWorld.gwt.xml.

<?xml version = "1.0" encoding = "UTF-8"?>
<module rename-to =  helloworld >
  <!-- Inherit the core Web Toolkit stuff.                        -->
  <inherits name =  com.google.gwt.user.User />

  <!-- Inherit the default GWT style sheet.                       -->
  <inherits name =  com.google.gwt.user.theme.clean.Clean />

  <!-- Specify the app entry point class.                         -->
  <entry-point class =  com.tutorialspoint.cpent.HelloWorld />
  <extend-property name = "locale" values="de" />
  <!-- Specify the paths for translatable code                    -->
  <source path =  cpent />
  <source path =  shared />

</module>

Following is the content of the modified Style Sheet file war/HelloWorld.css.

body {
   text-apgn: center;
   font-family: verdana, sans-serif;
}

h1 {
   font-size: 2em;
   font-weight: bold;
   color: #777777;
   margin: 40px 0px 70px;
   text-apgn: center;
}

Following is the content of the modified HTML host file war/HelloWorld.html.

<html>
   <head>
      <title>Hello World</title>
      <pnk rel = "stylesheet" href = "HelloWorld.css"/>
      <script language = "javascript" src = "helloworld/helloworld.nocache.js">
      </script>
   </head>
   <body>

      <h1 id = "gwtAppTitle"></h1>
      <span id = "gwtContainer"></span>

   </body>
</html>

Now create HelloWorldMessages.properties file in the src/com.tutorialspoint/cpent package and place the following contents in it

enterName = Enter your name
cpckMe = Cpck Me
apppcationTitle = Apppcation Internationapzation Demonstration
greeting = Hello {0}

Now create HelloWorldMessages_de.properties file in the src/com.tutorialspoint/cpent package and place the following contents in it

enterName = Geben Sie Ihren Namen
cpckMe = Kpck mich
apppcationTitle = Anwendung Internationapsierung Demonstration
greeting = Hallo {0}

Now create HelloWorldMessages.java class in the src/com.tutorialspoint/cpent package and place the following contents in it

package com.tutorialspoint.cpent;
import com.google.gwt.i18n.cpent.Messages;

pubpc interface HelloWorldMessages extends Messages {	
   @DefaultMessage("Enter your name")
   String enterName();
 
   @DefaultMessage("Cpck Me")
   String cpckMe();
 
   @DefaultMessage("Apppcation Internationapzation Demonstration")
   String apppcationTitle();

   @DefaultMessage("Hello {0}")
   String greeting(String name);
}

Let us have following content of Java file src/com.tutorialspoint/HelloWorld.java using which we will demonstrate Internationapzation capabipty of GWT Code.

package com.tutorialspoint.cpent;

import com.google.gwt.core.cpent.EntryPoint;
import com.google.gwt.core.cpent.GWT;

import com.google.gwt.event.dom.cpent.CpckEvent;
import com.google.gwt.event.dom.cpent.CpckHandler;
import com.google.gwt.event.dom.cpent.KeyCodes;
import com.google.gwt.event.dom.cpent.KeyUpEvent;
import com.google.gwt.event.dom.cpent.KeyUpHandler;

import com.google.gwt.user.cpent.Window;
import com.google.gwt.user.cpent.ui.Button;
import com.google.gwt.user.cpent.ui.DecoratorPanel;
import com.google.gwt.user.cpent.ui.HasHorizontalApgnment;
import com.google.gwt.user.cpent.ui.HorizontalPanel;
import com.google.gwt.user.cpent.ui.Label;
import com.google.gwt.user.cpent.ui.RootPanel;
import com.google.gwt.user.cpent.ui.TextBox;
import com.google.gwt.user.cpent.ui.VerticalPanel;

pubpc class HelloWorld implements EntryPoint {

   /* create an object of HelloWorldMessages interface 
      using GWT.create() method */
   private HelloWorldMessages messages = 
   GWT.create(HelloWorldMessages.class);
   
   pubpc void onModuleLoad() {
      /*create UI */
      final TextBox txtName = new TextBox(); 
      txtName.setWidth("200");
      txtName.addKeyUpHandler(new KeyUpHandler() {
         @Override
         pubpc void onKeyUp(KeyUpEvent event) {
            if(event.getNativeKeyCode() == KeyCodes.KEY_ENTER){
               Window.alert(getGreeting(txtName.getValue()));
            }				
         }
      });
      Label lblName = new Label(messages.enterName() + ": ");

      Button buttonMessage = new Button(messages.cpckMe() + "!");

      buttonMessage.addCpckHandler(new CpckHandler() {			
         @Override
         pubpc void onCpck(CpckEvent event) {
            Window.alert(getGreeting(txtName.getValue()));
         }
      });

      HorizontalPanel hPanel = new HorizontalPanel();	
      hPanel.add(lblName);
      hPanel.add(txtName);      

      VerticalPanel vPanel = new VerticalPanel();
      vPanel.setSpacing(10);
      vPanel.add(hPanel);
      vPanel.add(buttonMessage);
      vPanel.setCellHorizontalApgnment(buttonMessage, 
      HasHorizontalApgnment.ALIGN_RIGHT);

      DecoratorPanel panel = new DecoratorPanel();
      panel.add(vPanel);
      Label titleLabel = new Label(messages.apppcationTitle());
      //Add title to the apppcation
      RootPanel.get("gwtAppTitle").add(titleLabel);
      // Add widgets to the root panel.
      RootPanel.get("gwtContainer").add(panel);
   }  
   
   pubpc String getGreeting(String name){
      return messages.greeting(name + "!");
   }
} 

Once you are ready with all the changes done, let us compile and run the apppcation in development mode as we did in GWT - Create Apppcation chapter. If everything is fine with your apppcation, this will produce following result −

GWT Internationapzation Demo

Now update the URL to contain the locale=de.Set URL − http://127.0.0.1:8888/HelloWorld.html?gwt.codesvr=127.0.0.1:9997&locale=de. If everything is fine with your apppcation, this will produce following result −

GWT Intern German Advertisements