Eclipse 提供了方便的国际化实现方式。
例子:
新建项目 i18n
package test;
public class I18NTest {
public I18NTest() {
String testString = "Hello ,I18N!";
System.out.println(testString);
}
public static void main(String[] args) {
new I18NTest() ;
}
}
|
在导航器里面右击该类,Source--->Externalize Strings...
<!--EndFragment-->
一直“下一步”,最后,会生成出这两个文件,而I18NTest这个类也改变了:
package test;
public class I18NTest {
public I18NTest() {
String testString = Messages.getString("I18NString.hello"); //$NON-NLS-1$
System.out.println(testString);
}
public static void main(String[] args) {
new I18NTest() ;
}
}
|
//$NON-NLS-1$ 这个注释是eclipse自己用的,最后的数字 1 表示该行的第几个数字是不用国际化的。
这个类的国际化就这样完成了。
想要这个类随着不同的语言环境得到不同的输出结果,可以在messages.properties的相同目录下建立message_区域代码.properties
在eclipse的运行设置里面添加-nl zh_cn 类似这样的信息,就可以调试不同地区的显示结果。
如果资源文件有多个参数,如:
I18NTest.TestString=My name is {0} ,today is {1} ,date is {2} .
|
程序可以改为:
package test;
import java.text.MessageFormat;
import java.util.Date;
public class I18NTest {
static String testString = Messages.getString("I18NTest.TestString"); //$NON-NLS-1$
public static void main(String[] args) {
String name = "Irving"; //$NON-NLS-1$
String today = "Monday"; //$NON-NLS-1$
Date date = new Date();
String result = MessageFormat.format(testString, new Object[] { name, today, date });
System.out.println(result);
}
}
|
如果想格式化时间,只需要修改资源文件为:
I18NTest.TestString=My name is {0} ,today is {1} ,date is {2,date,yyyy-MM-dd} .
|
注意,2之后不能有空格。
- 大小: 131.2 KB
- 大小: 1.8 KB
分享到:
相关推荐
eclipse 国际化插件eclipse 国际化插件
eclipse国际化插件可以帮助程序员轻松完成应用程序国际化的问题,它会根据程序员设置的配置文件进行自动的转换.
http://blog.csdn.net/liuhongjavaen/article/details/7655385 具体操作事项请查阅博客 struts2国际化源码 eclipse国际化插件
eclipse 国际化的插件 properties 分享一个不错的编写properties文件的Eclipse插件(plugin),有了它我们在编辑一些简体中文、繁体中文等 Unicode文本时,就不必再使用native2ascii编码了。您可以通过Eclipse中的...
Eclipse国际化插件,官网上说是给Eclipse3.5x用的,但经过测试,3.6也是可以的,做国际化挺方便的
这是我修改过的Properties Editor 解压到~eclipse/plugins下然后启动Eclipse,在.properties文件上点右键就可以看到Open with中的Property File GUI Editor
de.guhsoft.jinto_0.10.3.zip
把这个包解压缩,放到eclipse里面去,然后对于国际化的property文件的转码就能显示为汉字,添加新的字段的时候,也可以直接添加为汉字,会自动转化为编码的。
非常方便的国际化插件 Eclipse3.2和3.3都可以用
eclipse国际化插件JInto(de.guhsoft.jinto-0.13.5)