实现i18n使用java的最佳实践是什么? [英] what is best practice to implement i18n usage java?
问题描述
使用java实现i18n的最佳做法是什么?
what is best practice to implement i18n using java ?
推荐答案
您可以使用 ResourceBundle.getBundle (name)
根据用户区域设置返回正确的包,并获取特定的消息。
You can use ResourceBundle.getBundle( name )
which returns the correct bundle according to the user locale and get specific messages.
ResouceBundle类的工作方式是,尝试使用本地化消息加载 bundle (通常是.properties文件)。例如,您可以:
The way the ResouceBundle class works, is, try to load a bundle ( usually a .properties file ) with the localized messages. For instance you can have:
messages_en.properties
-----
greeting = "Hello "
和
messages_es.properties
-----
greeting = "Hola "
并按如下方式使用。
... void main( ... . {
ResourceBundle bundle = ResourceBundle.getBundle( "messages", userLocale );
System.out.println( bundle.getString("greeting" ) + " Steve " );
}
它将打印
Hello Steve
如果用户区域设置是英语(en),并且
if user locale is english ( en ) , and
Hola Steve
如果用户区域设置是西班牙语
if user locale is spanish ( es )
方法 ResouceBundle.getBundle(),不仅加载.properti es文件,如果可用,它也可以加载一个类,而这个类又可以从数据库加载消息。
The method ResouceBundle.getBundle(), not only loads .properties files, if available it may also load a class which in turn may load message from a database.
参见:
这篇关于实现i18n使用java的最佳实践是什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!