目录

GWT - 国际化( Internationalization)

GWT提供了三种方式来实现GWT应用程序的国际化。我们将演示静态字符串国际化在项目中最常用的用法。

Sr.No. 技术与描述
1

Static String Internationalization

这种技术最为普遍,在运行时只需要很少的开销; 是一种非常有效的翻译常量和参数化字符串的技术;最简单的实现。

静态字符串国际化使用标准Java属性文件来存储已翻译的字符串和参数化消息,并创建强类型的Java接口以检索其值。

2

Dynamic String Internationalization

这种技术非常灵活,但比静态字符串国际化要慢。 主机页面包含本地化字符串,因此,当我们添加新的语言环境时,不需要重新编译应用程序。 如果要将GWT应用程序与现有服务器端本地化系统集成,则应使用此技术。

3

Localizable Interface

这种技术是三种技术中最强大的。 实现Localizable允许我们创建自定义类型的本地化版本。 这是一种先进的国际化技术。

国际化GWT应用程序的工作流程

第1步:创建属性文件

创建包含要在应用程序中使用的消息的属性文件。 我们在示例中创建了一个HelloWorldMessages.properties文件。

enterName = Enter your name
clickMe = Click Me
applicationTitle = Application Internationalization Demonstration
greeting = Hello {0}

创建包含特定于语言环境的翻译值的属性文件。 我们在示例中创建了一个HelloWorldMessages_de.properties文件。 此文件包含德语翻译。 _de指定德语区域设置,我们将在我们的应用程序中支持德语。

如果要使用Eclipse创建属性文件,请将文件的编码更改为Other UTF-8 。选择该文件,然后在其中右键单击以打开其属性窗口。选择文本文件编码为Other UTF-8 。 应用并保存更改。

enterName = Geben Sie Ihren Namen
clickMe = Klick mich
applicationTitle = Anwendung Internationalisierung Demonstration
greeting = Hallo {0}

步骤2:将i18n模块添加到模块描述符XML文件

更新模块文件HelloWorld.gwt.xml以包含对德语语言环境的支持

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

第3步:创建等效于属性文件的接口

通过扩展GWT的Messages接口来创建HelloWorldMessages.java接口,以包括对内部化的支持。 它应包含与属性文件中的键相同的方法名称。 占位符将替换为String参数。

public interface HelloWorldMessages extends Messages {
   @DefaultMessage("Enter your name")
   String enterName();
   @DefaultMessage("Click Me")
   String clickMe();
   @DefaultMessage("Application Internalization Demonstration")
   String applicationTitle();
   @DefaultMessage("Hello {0}")
   String greeting(String name);
}

第4步:在UI组件中使用消息接口。

HelloWorldMessages中使用HelloWorld对象来获取消息。

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

国际化 - 完整的例子

此示例将引导您完成演示GWT应用程序的国际化功能的简单步骤。

按照以下步骤更新我们在GWT - Create Application的GWT应用程序GWT - Create Application章节 -

描述
1com.iowiki包下创建一个名为HelloWorld的项目,如GWT - Create Application一章中所述。
2 修改HelloWorld.gwt.xmlHelloWorld.cssHelloWorld.htmlHelloWorld.java ,如下所述。 保持其余文件不变。
3 编译并运行应用程序以验证实现的逻辑的结果。

以下是修改后的模块描述符src/com.iowiki/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.iowiki.client.HelloWorld'/>
  <extend-property name = "locale" values="de" />
  <!-- Specify the paths for translatable code                    -->
  <source path = 'client'/>
  <source path = 'shared'/>
</module>

以下是修改后的样式表文件war/HelloWorld.css

body {
   text-align: center;
   font-family: verdana, sans-serif;
}
h1 {
   font-size: 2em;
   font-weight: bold;
   color: #777777;
   margin: 40px 0px 70px;
   text-align: center;
}

以下是修改后的HTML主机文件war/HelloWorld.html

<html>
   <head>
      <title>Hello World</title>
      <link rel = "stylesheet" href = "HelloWorld.css"/>
      <script language = "javascript" src = "helloworld/helloworld.nocache.js">
      </script>
   </head>
   <body>
      <h1 id = "gwtAppTitle"></h1>
      <div id = "gwtContainer"></div>
   </body>
</html>

现在在src/com.iowiki/client包中创建HelloWorldMessages.properties文件并在其中放置以下内容

enterName = Enter your name
clickMe = Click Me
applicationTitle = Application Internationalization Demonstration
greeting = Hello {0}

现在在src/com.iowiki/client包中创建HelloWorldMessages_de.properties文件并在其中放置以下内容

enterName = Geben Sie Ihren Namen
clickMe = Klick mich
applicationTitle = Anwendung Internationalisierung Demonstration
greeting = Hallo {0}

现在在src/com.iowiki/client包中创建HelloWorldMessages.java类并将以下内容放入其中

package com.iowiki.client;
import com.google.gwt.i18n.client.Messages;
public interface HelloWorldMessages extends Messages {	
   @DefaultMessage("Enter your name")
   String enterName();
   @DefaultMessage("Click Me")
   String clickMe();
   @DefaultMessage("Application Internationalization Demonstration")
   String applicationTitle();
   @DefaultMessage("Hello {0}")
   String greeting(String name);
}

让我们有以下Java文件src/com.iowiki/HelloWorld.java ,我们将使用它来演示GWT Code的国际化能力。

package com.iowiki.client;
import com.google.gwt.core.client.EntryPoint;
import com.google.gwt.core.client.GWT;
import com.google.gwt.event.dom.client.ClickEvent;
import com.google.gwt.event.dom.client.ClickHandler;
import com.google.gwt.event.dom.client.KeyCodes;
import com.google.gwt.event.dom.client.KeyUpEvent;
import com.google.gwt.event.dom.client.KeyUpHandler;
import com.google.gwt.user.client.Window;
import com.google.gwt.user.client.ui.Button;
import com.google.gwt.user.client.ui.DecoratorPanel;
import com.google.gwt.user.client.ui.HasHorizontalAlignment;
import com.google.gwt.user.client.ui.HorizontalPanel;
import com.google.gwt.user.client.ui.Label;
import com.google.gwt.user.client.ui.RootPanel;
import com.google.gwt.user.client.ui.TextBox;
import com.google.gwt.user.client.ui.VerticalPanel;
public class HelloWorld implements EntryPoint {
   /* create an object of HelloWorldMessages interface 
      using GWT.create() method */
   private HelloWorldMessages messages = 
   GWT.create(HelloWorldMessages.class);
   public void onModuleLoad() {
      /*create UI */
      final TextBox txtName = new TextBox(); 
      txtName.setWidth("200");
      txtName.addKeyUpHandler(new KeyUpHandler() {
         @Override
         public 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.clickMe() + "!");
      buttonMessage.addClickHandler(new ClickHandler() {			
         @Override
         public void onClick(ClickEvent 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.setCellHorizontalAlignment(buttonMessage, 
      HasHorizontalAlignment.ALIGN_RIGHT);
      DecoratorPanel panel = new DecoratorPanel();
      panel.add(vPanel);
      Label titleLabel = new Label(messages.applicationTitle());
      //Add title to the application
      RootPanel.get("gwtAppTitle").add(titleLabel);
      // Add widgets to the root panel.
      RootPanel.get("gwtContainer").add(panel);
   }  
   public String getGreeting(String name){
      return messages.greeting(name + "!");
   }
} 

一旦准备好完成所有更改,让我们像在GWT - 创建应用程序章节中那样在开发模式下编译和运行应用程序 。 如果您的应用程序一切正常,这将产生以下结果 -

GWT国际化演示

现在更新URL以包含locale = de.Set URL - http://127.0.0.1:8888/HelloWorld.html?gwt.codesvr=127.0.0.1:9997 &locale=de 。 如果您的应用程序一切正常,这将产生以下结果 -

GWT实习德语
↑回到顶部↑
WIKI教程 @2018