what is best practice to implement i18n usage java? [closed]

99封情书 提交于 2019-12-07 00:30:04

问题


what is best practice to implement i18n using java ?


回答1:


You can use ResourceBundle.getBundle( name ) which returns the correct bundle according to the user locale and get specific messages.

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 "

and

messages_es.properties
-----
greeting = "Hola "

and use it as follows.

... void main( ... . {
     ResourceBundle bundle = ResourceBundle.getBundle( "messages", userLocale );
     System.out.println( bundle.getString("greeting" )  + " Steve " );
 }

And it will print

Hello Steve

if user locale is english ( en ) , and

Hola Steve

if user locale is spanish ( 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.

See also:

ResourceBundle

Internationalization Quick Intro




回答2:


The Java Tutorials have an Internationalization trail. It covers the core language features for Internationalization.




回答3:


I'd recommend looking at ResourceBundles.

It's a complicated question, because if you have a database you'll want to I18N it as well.




回答4:


This is basically a matter of the domain. If you are on the web, most major frameworks will provide a way for I18N (in Spring, this works with .properties files and taglibs).

For desktop applications on contrary, resource injection might be an interesting option. The Spring Application Framework goes even further and lets you configure named Swing components (buttons, Labels,...) entirely from configuration files. With that you can set the colors and borders of components but also their texts.



来源:https://stackoverflow.com/questions/4034661/what-is-best-practice-to-implement-i18n-usage-java

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!