kuix教程4:国际化

来源:互联网 发布:js设置input必填项 编辑:程序博客网 时间:2024/04/30 09:11

教程4:国际化

Requirement需求

Please read the Tutorial 3 first.请阅读教程3首。

Internationalize the application国际化应用

下载源代码 Download this tutorial step. 下载本教程的步骤。

I18n concepts i18n的概念

Internationalization (or i18n) with Kuix is achieve by using localized resources files.国际化(i18n的或与Kuix是使用文件实现本地化的资源)。 Kuix provides a static method that looks for a translation for a given label and return the result as a string. Kuix提供了一个静态方法,查找一个给定的标签翻译和作为一个字符串返回结果。 This method uses the device local to select the appropriate resource file and if it cannot be found, it uses the default one.这种方法使用的设备来选择合适的本地资源文件,如果它不能被发现,它使用默认之一。

Resource files are (by default) in the /i18n directory and should comply with the following naming convention:资源文件(默认情况下)在/ i18n的目录,并应遵守以下命名约定:

messages.en.properties messages.en.properties
where "en" stands for the language string 其中,“恩”是指语言中的字符串

Several "system" labels - like "Ok", "Cancel", etc. - have a default translation.几个“系统”标签 - 如“好”,“取消”,等等 - 有一个默认的转换。 So if you use some message box with buttons, you will have a default english translation for them.因此,如果你使用一些按钮的消息框,您将有一个默认的英语为他们翻译。

The label translation method:该标签翻译方法:

Thefollowing command, will make Kuix to look for a translation for thegiven label in a localized resource file or by default in/i18n/messages.properties.下面的命令,将Kuix寻找一个在一个本地化的资源文件中给出的标签,或在/ i18n/messages.properties默认的转换。 If an entry is found, it returns the translated text otherwise it is the string of the entry.如果一个项目被发现,它返回翻译文本,否则它是该项目的字符串。

Kuix.getMessage("OK"); 

Ifyou want to distribute your application in 2 languages - english andfrench for instance -, you will have to create at least 2 files :messages.fr.properties and a default english file messages.properties.如果您要分发您的应用程序中的2种语言 - 英语和法语,例如 - 你必须创建至少2个文件:messages.fr.properties和默认英文文件messages.properties。

The translation file syntax翻译文件的语法

The syntax of the translation file is as simple as:由翻译文件的语法是非常简单的:

 LABEL=translation 

Create the labels and translate them:创建标签和翻译它们:

create file messages.properties and set the following content:创建文件messages.properties并设置以下内容:

 YES=Yes NO=No OK=Ok HELLOWORLD=Hello World! ABOUT=About EXIT=Exit MORE=more... CREDITS=This helloworld is powered by Kuix! EXIT_CONFIRM=Are you sur you want to quit? 

Create the file messages.fr.properties and set the following content:创建文件messages.fr.properties并设置以下内容:

 YES=Oui NO=Non OK=OK HELLOWORLD=Bonjour tout le monde ! ABOUT=A propos EXIT=Quitter MORE=plus... CREDITS=Ce Helloworld a été créé avec Kuix! EXIT_CONFIRM=Etes-vous sûr de vouloir quitter? 

The 3 first labels are used for the popup windows and should work immediately. 3首标签用于弹出窗口,并应立即开展工作。 But we will need to reference the 3 last ones.但是,我们需要参考去年的3。

Your project structure must be now like this :您的项目结构必须是现在这样的:

I18n folder

编辑框使用的翻译标签

你只需要打开HelloWorldFrame类,并以各自的标签的文本:

 if ("about".equals(identifier)) { // display a popup message Kuix.alert(Kuix.getMessage("CREDITS"), KuixConstants.ALERT_OK); return false; } if ("exitConfirm".equals(identifier)) { // display a popup message Kuix.alert(Kuix.getMessage("EXIT_CONFIRM"), KuixConstants.ALERT_YES | KuixConstants.ALERT_NO, "exit", null); return false; } 

引用在XML文件翻译的文字

Sinceit is not possible to call the Kuix.getMessage() method from the XMLfile, the fallowing syntax has been integrated to the parser so thatyou can reference a translated label directly:因为它是无法调用Kuix.getMessage()从XML文件的方法,在休耕语法已经集成到分析器,让您可以直接引用翻译标签:

 %LABEL% 

For performance considerations, variables cannot be used in the attribute but only from the XML values.出于性能的考虑,变量不能使用的属性,但只从XML值。 As a consequence, if you want to use some translated text, you can use one of the following syntax:因此,如果你想使用一些翻译的文本,你可以使用下面的语法之一:

  • Any text alone is interpreted as a Text node:单是任何文字解释为一个文本节点:

     %LABEL% 
  • Any text in a Text node, is always associated to this node:在任何一个文本节点的文本,这始终是相关联的节点:

     <text>%LABEL%</text> 
  • Ina more general way, to set a node's attribute to a variable value, opena _ATTRIBUTENAME node and set its value between its opening and itsclosing tag:在更一般的方式,设置一个节点的属性变量的值,打开一个_ATTRIBUTENAME节点,并设置其之间的开幕式和闭幕式标记的值:

     <text><_text>%LABEL%</_text></text> 

编辑XML使用标签

You should get something like:你应该是这样的:

 <screen title="helloworld"> <container style="layout:inlinelayout(false,fill);align:center"> %HELLOWORLD% <picture src="logo_community.png" /> <button onAction="about" shortcuts="1">%ABOUT%</button> <button onAction="exitConfirm" shortcuts="back|delete|0">%EXIT%</button> </container> <screenFirstMenu onAction="exit">%EXIT%</screenFirstMenu> <screenSecondmenu> %MORE% <menuPopup> <menuItem onAction="about"> %ABOUT% </menuItem> <menuItem onAction="exitConfirm"> %EXIT% </menuItem> </menuPopup> </screenSecondmenu> </screen> 

Start 启动

Start the i18n midlet

Translation parameters翻译参数

Note that a translation label can contain one or more parameters that will be used in the translation.请注意,翻译标签可以包含一个或多个将在翻译中使用的参数。 This feature is especially useful when you want to mix translated and untranslated texts in the same widget.此功能是特别有用当你想混合翻译和未翻译的文本在同一部件。

  • The syntax in the translation file is given by the example below:翻译文件中的语法,给出了下面的例子:

     VALUES=Values: {0} {1} 
  • In the XML file:在XML文件:

     %VALUES(female,male)% 
The comma is the only valid parameter delimiter.逗号是唯一有效的参数分隔符。 Avoid using it in your parameters values.避免使用它在你的参数值。
  • And finally in Java ME:最后在Java ME:

     String param1 = "female"; String param2 = "male"; Kuix.getMessage("VALUES", new String[] {value1, value2}); 


原创粉丝点击