Java 国际化 - 使用 ResourceBundles 显示本地消息(转载,英文)-

转载 2008年04月30日 14:16:00
本 文可以帮助您理解像 Struts 资源文件为什么要那样编写 等国际化原理了, 可以说这是基础的国际化知识, Struts 的国际化也基于这个 J2SE 里面的功能进行开发的. 本站推出的 Java 属性文件编辑器, 完全可以用来编辑这些国际化的资源文件.

Java Internationalization: Localization with ResourceBundles
By John O'Conner
October 1998

If you've developed global software, you know that one of the great commandments of internationalization is to separate text, labels, messages, and other locale-sensitive objects from the core source code. This helps to maintain a single source code base for all language versions of your product. It also facilitates translations because all localizable resources are identified and isolated.

Depending on the operating system (OS) and programming language environments, developers can use a variety of mechanisms to identify and separate culturally sensitive or locale-dependent objects from their source code. Windows programmers have their resource (RC) files and resource-only dynamic link libraries (DLLs). Macintosh developers have their resource forks and similar concepts. Of course, Java developers have a method too--the resource bundle. 

This article explores the types, structure, creation, and usage of Java resource bundles. You'll learn how to use these files to create a localizable program. 

Resource Bundles

The most obvious type of localizable object is a text string, but resource bundles can store any Java object. Whatever you choose to store, you'll put it into some form of java.util.ResourceBundle

To create a concrete class you must subclass ResourceBundle and implement two methods: handleGetObjects and getKeys. Directly subclassing ResourceBundle like this is a distraction from the main purpose of a bundle, which is to store localizable objects, not to write a lot of code logic. Fortunately, the Java Development Kit (JDK) provides two subclasses, ListResourceBundle and PropertyResourceBundle, that allow you to concentrate on providing the data for the file instead of writing code. The differences between bundle types are explained later in this article, but first take a look at the common ground. 

What's in a Name?

For each language translation of  your product, you create a new version of your initial resource bundle. For example, if you create MyResource to store all English text, you will create a similarly named file to store the French text. Resource bundles use a naming convention that distinguishes the potentially numerous versions of essentially the same bundle. Each bundle name consists of a base name and an optional locale identifier. Together, these two pieces uniquely identify a bundle in a package. 

Using the above example, the French version of the resources should be named MyResource_fr_FR. The Canadian French version should be MyResource_fr_CA

Appending the locale name to the base name of the bundle effectively links the bundle's usage to a specific locale. When you ask for the bundle using ResourcBundle.getBundle("MyResource"), the getBundle method appends the default locale identifier to the base name and loads the appropriate bundle. If the locale is fr_CA, then a call to ResourceBundle.getBundle("MyResource") will load the MyResource_fr_CA bundle. 

Actually, getBundle is overloaded. So, not only can you just pass it a bundle name and expect it to return the bundle for the default locale, but you can also pass a specific locale. For example, if you want to explicitly load a Japanese bundle, you can call ResourceBundle.getBundle("MyResource", new Locale("ja", "JP"))

The getBundle method provides a graceful degradation algorithm that attempts to find the nearest matched bundle in cases where the specified bundle can't be found or doesn't exist. In the following search, each element of the search name is separated with an '_' (UNDERSCORE) character. The algorithm searches for bundle names in the following order: 

<baseclass>+<specific language>+
<specific country>+<specific variant>
<baseclass>+<specific language>+
<specific country>
<baseclass>+<specific language>
<baseclass>+<default language>+
<default country>+<default variant>
<baseclass>+<default language>+
<default country>
<baseclass>+<default language>

So, use a concrete example this time. Suppose the default locale determined from the operating system is en_US (U.S. English). However, you may want to load MyResource for the fr_CA (Canadian French) locale instead. The call to ResourceBundle.getBundle("MyResource", new Locale("fr","CA")) would produce the following search order:


The search would end, of course, as soon as any bundle is found along the way. If no bundle is found, getBundle throws a MissingResourceException

Accessing Objects

All ResourceBundle subclasses provide access methods to retrieve the locale-sensitive objects stored within themselves. The most basic access method is the following: 

public final Object getObject(String key)

The getObject method returns the object associated with the key. Because the return value is simply an Object, you may have to cast it to a more specific object type. For example, if you know the returned Object is a String, you can do the following: 

ResourceBundle res = 
String labelOK =
(String) res.getObject("OK_TEXT");

Casting the returned object is easy, but ResourceBundle provides a few convenience methods for returning common object types. Some examples follow: 

public final String getString(String key)
public final String[] getStringArray(String key)

These methods are convenient, but they don't do anything complex. If they save you some of the time or trouble necessary to cast the returned value, try them. The same code above becomes:

ResourceBundle res = 
String labelOK = res.getString("OK_TEXT");


Bundles can share the same <key, value> pairs if the bundles share the same base name. For example, imagine you have created MyResource_en, which will serve as your default bundle for all English speaking locales. If your largest customer base speaks U.S. English, you might choose to create this bundle using U.S. English. To create a British English version of this bundle, you create MyResource_en_GB, which should contain only the objects that are different from the contents in MyResource_en. Imagine that you have stored a <key, value> pair for "Hello, world!" in MyResource_en. You then decide that the British English version of the string is the same, so there's no need to reproduce the string in MyResource_en_GB. When you then ask for the "Hello, world!" string in MyResource_en_GB, the ResourceBundle class is smart enough to look in MyResource_en when it doesn't find the string in MyResource_en_GB.

The ResourceBundle class associates a parent to any bundle. If an object value cannot be found in the specified class, ResourceBundle searches the parent class in the relationship. You establish this relationship among bundles by giving them the same base name.

Figure 1 ResourceBundle Inheritance


The java.util.ListResourceBundle is a subclass of java.util.ResourceBundle. It implements both handleGetObjects and getKeys for you. The purpose of this bundle is to allow you to define localizable elements as a two-dimensional array of <key, value> pairs. This bundle is easy to use and requires only minimal code, which allows you to focus on providing data in the bundle instead. 

You can create your own ListResourceBundle by subclassing java.util.ListResourceBundle. You must implement a single method, getContents(), which should return an array of <key, value> pairs. The following code shows the structure of a simple bundle:

import java.util.ListResourceBundle;

import java.awt.Button;

public class MyResource extends
java.util.ListResourceBundle {

public Object getContents() {
return contents;

static Object[][] contents = {
"Hello, world!" },
"Goodbye everyone!" },
new Button("Cancel") },
new AddressPanel()}

ListResourceBundle allows you to store any class of object. Notice the definition of CANCEL_BUTTON above. CANCEL_BUTTON is an AWT Button object that gets instantiated inside the resource bundle itself. Although value objects can be any type, keys are always a String object in both ListResourceBundle and PropertyResourceBundles.

ListResourceBundles are classes, so you store them in a .java file. Of course, you must compile them to byte-code to use them.


A PropertyResourceBundle is definitely the easiest bundle to implement. It is nothing more than a text file. A PropertyResourceBundle can have multiple lines, and each line is either a comment, blank line, or a <key>=<value> entry. Property bundles should follow the same naming conventions as those used by a ListResourceBundle. However, the differences are that property bundles are not compiled and that they have a properties extension. So, the bundle below could be named

The following example illustrates the structure and contents of the above resource bundle implemented as a property file:

HELLO_TEXT=Hello, world!
GOODBYE_TEXT=Goodbye everyone!

// We'd like to say new AddressPanel() here but
// property bundles are restricted in that area

// Other objects cannot be stored in a
// PropertyResourceBundle
// Only strings can live here.

Unfortunately, property files of this type have a limitation on the objects that can be stored. You are limited to storing String <value> entries. However, this limitation is tolerable, especially since there is a workaround. You can create an object instance if you know its class name. Imagine that you need to create an AddressPanel object as indicated in the above property file. The following code shows how to handle both the "Cancel" button and the panel.

ResourceBundle res =
// we get text in the same way
String strHello = res.getString(
String strGoodbye = res.getString(
// but we need to create the button ourselves
Button button = new Button();
// and set the text too
// creating a class from a text
// string is a little trickier
// but is still easy
Class addrPanelClass =
AddressPanel addrPanel =

Other Resource Bundles

You can create additional resource bundle types yourself by subclassing the java.util.ResourceBundle class. There are plenty of reasons why you might want to do this. Maybe you'd like to read all text from a database. Or maybe you want to have a different type of key for locating text objects. Or perhaps you want to create bundle classes that specialize in types of data access for performance reasons. It's up to you, and if you find a reason why the available bundles don't work for you, you can modify and extend their behavior and performance.

Inprise, Inc. provides a good example of a new ResourceBundle type in their JBuilder product. JBuilder is an integrated development environment (IDE) for Java, and it includes a new class named borland.jbcl.util.ArrayResourceBundle. ArrayResourceBundle is interesting because it implements keys as integer offsets into arrays of objects. The motivation for creating this new bundle is performance, because array offsets can be used quickly and more efficiently than Strings.

So, just how much better is performance? The results are surprising. Inprise's international software development team for JBuilder helped evaluate the performance differences among these three classes:

  • java.util.PropertyResourceBundle
  • java.util.ListResourceBundle
  • borland.jbcl.util.ArrayResourceBundle
The JBuilder international team placed the 201 strings in the borland.jbcl.dataset package into a ListResourceBundle, a PropertyResourceBundle, and an ArrayResourceBundle. They started a new virtual machine (VM) for each bundle type and read each string 10,000 times for each test. Then, they averaged the access times over five independent tests. The results are in Table 1.

Table 1 ResourceBundle Performance

Class NameSize (bytes)Average  Run Time (seconds)
PropertyResourceBundle 13114 9.37
ListResourceBundle 19041 6.10
ArrayResourceBundle 13585 0.90

Compiling Non-ASCII Bundles

Java uses Unicode as its internal character set. However, most operating system, editors, and other tools still use regional code pages to represent text. In most cases, this means that you will create your *.java files in a regional encoding, not Unicode. Of course, the Java compiler must compensate for the difference by converting your native code page into Unicode. Several character converters are included in the JDK. You can see a partial list in Table 2.

The Java compiler automatically assumes that your resource files are written in the default code page of your operating system. So, if you're using a U.S. English version of Solaris, the compiler will probably assume your character set is ISO 8859-1. Although 8859-1 supports many languages, it doesn't represent everything. So, if you need to compile a Cyrillic language bundle, you would get incorrect results. Fortunately, the compiler allows a command line directive that specifies the character set of the source file. Using the appropriate converter, you can direct the compiler to accurately convert the *.java source into Unicode. The following example shows the command line you would use to compile a Japanese resource bundle that's written in Shift JIS, a popular Japanese encoding: 
javac -encoding SJIS 

Table 2 Partial List of Supported Languages and Converters 

Arabic (ar) 8859_6
Byelorussian (be) 8859_5
Czech (cs) 8859_2
Dutch (nl) 8859_1, Cp1252
English (en) 8859_1, Cp1252
German (de) 8859_1, Cp1252
Greek (el) 8859_7
Hebrew (iw) 8859_8
Japanese (ja) SJIS, JIS, EUCJIS
Macedonian (mk) 8859_5
Maltese (mt) 8859_3
Norwegian (no) 8859_1, Cp1252
Polish (pl) 8859_2
Russian (ru) KOI8_R, 8859_5
Turkish (tr) 8859_9, Cp1254
Ukrainian (uk) 8859_5

Sun Microsystems continually updates its list of supported character sets. If you need one that's not already supported, chances are that it's in progress. Of course, it wouldn't hurt to submit a request at the JDC Bug Database either.

You'll need to understand another caveat regarding property bundles; you must create them in ASCII, or use Unicode escape codes to represent Unicode characters. Since you don't compile a property file to byte codes, there is no way to tell the Java1 Virtual Machine (JVM) which character set to use. So you must explicitly stick with ASCII or use the escape sequences. You can represent a Unicode character in any Java file using /uXXXX, where XXXX is the hexidecimal representation of the character. If you don't know the hexidecimal value for a character, just use the native2ascii tool provided in the JDK. This tool will convert a file written in your native encoding to one that represents non-ASCII characters as Unicode escape sequences. A partial listing of the %JDK_HOME%/lib/ file provides an example of these escape sequences:

# AWT Font default Properties for Japanese Windows
dialog.1=/uff2d/uff33 /u30b4/u30b7/u30c3/u30af,
dialoginput.0=Courier New,ANSI_CHARSET
dialoginput.1=/uff2d/uff33 /u30b4/u30b7/u30c3/u30af,

Graceful Degradation

Java searches for bundles by first looking for the fully qualified bundle name including a complete locale. If it can't find that file, it repeatedly chops off or replaces pieces of the locale identifier. The search algorithm is useful, since it provides graceful degradation when the requested bundle cannot be found.


Resource bundles are a great way to isolate translatable text or localizable objects from your core source code. They're both easy to create and easy to use. Moreover, many of the good integrated development environments are including resource-bundle creation and management tools. Make sure you use resource bundles in your next application. Better yet, explore ways to incorporate them into your existing applications. They will definitely make localizing your product easier.

1 As used on this web site, the terms "Java virtual machine" or "JVM" mean a virtual machine for the Java platform.

John O'Conner teaches software internationalization topics and consults for global development projects. He also enjoys speaking Japanese, playing softball, and spending time with his family.

Java for Web学习笔记(七五):国际化i18n(3)异常显示的国际化

问题和解决方法 App因某种原因会抛出异常,我们会记录在log中,同时也要告知用户出了问题,一些错误信息需要显示给用户,这就有国际化的需求。我们不应简单将原始的Exception直接显示给用户: 异...
  • flowingflying
  • flowingflying
  • 2017年08月06日 11:18
  • 1066


一.在页面显示错误消息:如要显示email的错误或显示全部的错误 二.错误消息的国际化 1.配置国际化资源文件 2.在springmvc.xml中配置R...
  • baidu_21578557
  • baidu_21578557
  • 2016年05月24日 17:43
  • 4400

iOS 国际化 没有国际化的语言显示默认语言

iOS 国际化 没有国际化的语言显示默认语言我们公司只做了简体,繁体,英文国际化 需求是如果没有国际化的语言默认显示英语。...
  • sunjie886
  • sunjie886
  • 2016年07月27日 17:15
  • 929


【0】README1) 本文部分文字描述转自 core java volume 2 , 测试源代码均为原创, 旨在理解 java国际化——消息格式化+文本文件和字符集 的基础知识 ;【1】消息格式化1...
  • PacosonSWJTU
  • PacosonSWJTU
  • 2016年02月04日 19:06
  • 942


  • baggio785
  • baggio785
  • 2006年02月22日 02:16
  • 4410

spring 国际化demo遇到的问题

这是我第一次写博客,我写博客的原因主要是,在遇到的问题解决之后,记录下来,以防以后要用到,所以文笔比较粗俗,供大家借鉴 1.在这里,我用的是idea 工具,创建maven的工程(这个在这里不...
  • jun522450654
  • jun522450654
  • 2017年05月19日 09:23
  • 218


1、    提示信息国际化  基础知识          概念 国际化英文单词为:Internationalization,又称I18N,I为因为单词的第一个字母,18为这个单词的长度,而N代表...
  • u012373815
  • u012373815
  • 2015年10月31日 23:31
  • 615


[正文] froward 软件编写和文档通常使用英文,这便于世界各地的程序员交流,但是并非所有用户都懂英文,所以程序能显示为用户的母语,能使软件用户群更大,且方便了普通...
  • senlin1202
  • senlin1202
  • 2016年11月10日 11:49
  • 1610


import org.junit.jupiter.api.Test; import java.text.NumberFormat; import java.util.Locale; /** * Cr...
  • wushuang3625
  • wushuang3625
  • 2017年03月29日 01:46
  • 226

Android 国际化适配之中英文模式切换

前言 最近在做一个app,要求有中文模式与英文模式,其实就是国际化适配了,这个不难但是感觉非常麻烦呀,先看下我写的简单例子,这样比较直观点: 接下来我介绍下如何去实现它: ...
  • imSunLight
  • imSunLight
  • 2018年01月11日 23:10
  • 262
您举报文章:Java 国际化 - 使用 ResourceBundles 显示本地消息(转载,英文)-