开发者

How to internationalizate an application in Java?

开发者 https://www.devze.com 2023-02-12 06:39 出处:网络
I am going to internationalize my program written in Java. I expect that during installation of my program there will be some questions displayed as:

I am going to internationalize my program written in Java. I expect that during installation of my program there will be some questions displayed as:

  1. In which country you are
  2. Which language you preffer (here should be suggested lanuage(s) for selected country
  3. Other necessary questions

Ather that Program will place into memory all necessary information as:

开发者_开发问答
  1. Currency used
  2. Date and Time way of dispalying
  3. Other necessary (standard) information

It would be good also to have some editor enabling us to change texts showed in program interface to other languages.

A contribution of someone, who already performed such job for other program would be highly appreciated.


The most common way in java is to use a ResourceBundle.

You can have a properties file per language and put there all the text for the app. Adding a new language is a difficult as creating a new file with the correct name.

So to edit the these files, just use any text editor.


In Java, you normally can get the language and country information by the Locale.getLocale() call. This is what the user's system (or account) is configured to. Then you don't have to ask in the setup, or at least can provide a good default for your user to select.

Most information like "usual number format" (including currency for money) for lots of locales is already included in the Java runtime, and can be used by using different APIs like NumberFormat, DateFormat, PrintWriter, Formatter, Collator and similar. Look at the use page for class Locale to get an overview on what is already there for you (and what you still have to do yourself).


I assume that you meant desktop application (because of installer). In that case start thinking about i18n by reading this link.

As for what you wrote, it doesn't make sense to ask the user where he is. You also don't need to store anything in memory. Just use current locale settings, which are detectable via:

Locale currentLocale = Locale.getDefault();

When you have this object, you can easily format Date, Times, Currencies and Numbers by using appropriate format classes:

DateFormat dateFormatter = DateFormat.getDateInstance(DateFormat.DEFAULT, Locale.getDefault());
DateFormat dateTimeFormatter = DateFormat.getDateTimeInstance(
                 DateFormat.DEFAULT, DateFormat.DEFAULT, Locale.getDefault());
Date now = new Date();
String formattedDate = dateFormatter.format(now);
String formattedDateAndTime = dateTimeFormatter.format(now);

Etc, just read the link. You would probably also need to support locale-aware sorting (via so called Collators) and string externationalization (via ResourceBundle mentioned by Augusto). Also you would need to learn MessageFormat to format compound strings (instead of just concatenating them).

That is not all, i18n is actually huge topic, but this would be a good start.

0

精彩评论

暂无评论...
验证码 换一张
取 消