`
xidajiancun
  • 浏览: 456156 次
文章分类
社区版块
存档分类
最新评论

How do I use locales and resource bundles to internationalize my application?

 
阅读更多
How do I use locales and resource bundles to internationalize my application?
Author: Deron Eriksson
Description: This Java tutorial describes how to use locales and resource bundles to internationalize an application.
Tutorial created using: Windows XP || JDK 1.5.0_09 || Eclipse Web Tools Platform 2.0 (Eclipse 3.3.0)


This tutorial demonstrates how to use locales and resource bundles to internationalize a JavaSWapplication. We'll use a project with the following structure:

'testing' project

The ResourceBundle.getBundle() method allows us to get a set of properties from a .properties file by passing in the name of the bundle file (without its extension). We can also pass in a Locale object as a second parameter to get a resource bundle for a particular language in a particular country. In the project structure seen above, we have TestBundle.properties (default properties), TestBundle_en_US.properties (US English properties), and TestBundle_sv_SE.properties (Swedish properties). These bundles allow us to set our application to display different languages.

After getting a resource bundle, we can call getString() on the bundle with a property name to get the corresponding property value from the bundle.

To see some of the nuances involved with resource bundles and locales, I created the InternationalizationTest class.

InternationalizationTest.java

package test;

import java.util.Locale;
import java.util.ResourceBundle;

public class InternationalizationTest {

	public static void main(String[] args) throws Exception {

		ResourceBundle bundle1 = ResourceBundle.getBundle("TestBundle");
		displayValues(bundle1);

		Locale defaultLocale = Locale.getDefault();
		ResourceBundle bundle2 = ResourceBundle.getBundle("TestBundle", defaultLocale);
		displayValues(bundle2);

		Locale swedishLocale = new Locale("sv", "SE");
		ResourceBundle bundle3 = ResourceBundle.getBundle("TestBundle", swedishLocale);
		displayValues(bundle3);

		Locale nonexistentLocale = new Locale("xx", "XX");
		ResourceBundle bundle4 = ResourceBundle.getBundle("TestBundle", nonexistentLocale);
		displayValues(bundle4);

	}

	public static void displayValues(ResourceBundle bundle) {
		System.out.println("hello message:" + bundle.getString("my.hello"));
		System.out.println("goodbye message:" + bundle.getString("my.goodbye"));
		System.out.println("question message:" + bundle.getString("my.question"));
		System.out.println();
	}

}

InternationalizationTest gets four resource bundles and displays all the property values for each bundle. The first bundle is obtained by calling ResourceBundle.getBundle() with no locale specified. Next, we get the default locale and get the second bundle by including the default locale in the call to getBundle(). Next, we create a Swedish/Sweden locale and get the third bundle by calling getBundle() with the Swedish locale. Last of all, we create a locale with a language and a country that don't exist, and then call getBundle() with this locale.

The various properties files are shown here:

TestBundle.properties

my.hello=Hello (default)
my.goodbye=Bye (default)
my.question=Do you speak English? (default)

TestBundle_en_US.properties

my.hello=Hello
my.goodbye=Bye
my.question=Do you speak English?

TestBundle_sv_SE.properties

my.hello=Hejsan
my.goodbye=Hejd?
my.question=Pratar du engelska?

Executing InternationalizationTest on my machine generates the following results:

hello message:Hello
goodbye message:Bye
question message:Do you speak English?

hello message:Hello
goodbye message:Bye
question message:Do you speak English?

hello message:Hejsan
goodbye message:Hejdå
question message:Pratar du engelska?

hello message:Hello
goodbye message:Bye
question message:Do you speak English?

Notice that the #1, #2, and #4 bundles all use TestBundle_en_US.properties and the #3 bundle uses the TestBundle_sv_SE.properties. None of the bundles used TestBundle.properties. Even the bundle obtained with the nonexistent locale used TestBundle_en_US.properties.

Let's try changing the name of TestBundle_en_US.properties to NO_TestBundle_en_US.properties and re-run the InternationalizationTest class and see what happens.

changing name of TestBundle_en_US.properties to NO_TestBundle_en_US.properties

The execution of InternationalizationTest after this modification to the project is shown below.

hello message:Hello (default)
goodbye message:Bye (default)
question message:Do you speak English? (default)

hello message:Hello (default)
goodbye message:Bye (default)
question message:Do you speak English? (default)

hello message:Hejsan
goodbye message:Hejdå
question message:Pratar du engelska?

hello message:Hello (default)
goodbye message:Bye (default)
question message:Do you speak English? (default)

Now, the #1, #2, and #4 bundles use the TestBundle.properties since the TestBundle_en_US.properties file, which matches my default locale, can't be found. The #3 bundle returns Swedish results, as expected.


分享到:
评论

相关推荐

    The C IO Streams and Locales_clocales_

    Standard C++ IOStreams and Locales: Advanced Programmer's Guide and Reference

    OCP Java SE 8 Programmer II Exam Guide (Exam 1Z0-809)

    OCP Java SE 8 Programmer II Exam ...Dates, times, locales, and resource bundles I/O and NIO Generics and collections Inner classes Lambda expressions and functional interfaces Streams Threads Concurrency

    Java2核心技术卷I+卷2:基础知识(第8版) 代码

    Resource Bundles 329 A Complete Example 333 Chapter 6: Advanced Swing 351 Lists 352 Tables 370 Trees 405 Text Components 442 Progress Indicators 479 Component Organizers 492 Chapter 7: ...

    Laravel开发-locales

    Laravel开发-locales Laravel的区域设置管理包。

    convmv-1.15.tar.gz

    All this comes in very handy when one wants to switch over from old 8-bit locales to UTF-8 locales. It is also possible to convert directories to UTF-8 which are already partly UTF-8 encoded. convmv ...

    Exploring C++ 11

    Understand how to use Custom types, virtual functions and objects to structure your code Build your own function templates, namespaces and containers from the ground up. Put everything together to ...

    JS前台框架.rar

    level interaction and animation, advanced effects and high-level, themeable widgets, built on top of the jQuery JavaScript Library, that you can use to build highly interactive web applications. ...

    开源项目-go-playground-locales.zip

    开源项目-go-playground-locales.zip,locales - a set of locales generated from the CLDR Project which can be used independently or within an i18n package

    Visual C++ 编程资源大全(英文源码 表单)

    (71KB)<END><br>86,step0.zip A series of articles that resulted from experimenting with adding Drag and Drop features to my existing application. (16KB)<END><br>87,undo.zip Easily add Undo/Redo ...

    A Simple Performance Counter Application

    I've read elsewhere that the actual performance counter names used as constructor parameters are localized, so this code will need to be changed appropriately and recompiled for non-English locales.

    locales.7z

    Metabase(33.4)本地化语言包,包含14个语言的json文件。 zh.po文件转换成json文件可以根据开头的模式进行处理转换,这里我也提供一份已经转换好的json文件。

    electerm-locales:电子产品的多语言文件

    to ../electerm/node_modules/electerm-locales/locales/ for test# and add pre-push hook, do lint and test before every push测试npm run test# test only make sure all locale file keys and orders are match

    long_double_float_explicit.rar_Separated

    Code for available locales, separated out other .cpp files that then do not depend on resource bundle code and res_index bundles.

    PhotoShop cs4汉化包Locales.rar

    把解压后的文件复制到安装文件对应的文件夹,然后打开ps - edit - 最后一项 - 第二项 修改语言即可。

    cJSON-master.zip

    It also installs files for pkg-config to make it easier to detect and use an existing installation of CMake. And it installs CMake config files, that can be used by other CMake based projects to ...

    CSharp 3.0 With the .NET Framework 3.5 Unleashed(english)

    A Quick Introduction to Reference Types and Value Types 108 The Unified Type System 109 Reference Type and Value Type Memory Allocation 114 Reference Type and Value Type Assignment 117 More ...

    The GNU C Library

    * 7 Locales and Internationalization * 8 Message Translation * 9 Searching and Sorting * 10 Pattern Matching * 11 Input/Output Overview * 12 Input/Output on Streams * 13 Low-Level Input/Output ...

    CefSharp单独目录启动【vb.net版 】.7z

    相关说明太少了,做了个vb.net版的例子给大家,希望大家喜欢,其实方法也蛮...setting.LocalesDirPath = Application.StartupPath & "\cef\locales\" setting.ResourcesDirPath = Application.StartupPath & "\cef\

    jdk-9.0.1_doc-all 最新版

    Defines APIs for the JavaFX / Swing interop support included with the JavaFX UI toolkit, including SwingNode (for embedding Swing inside a JavaFX application) and JFXPanel (for embedding JavaFX inside...

Global site tag (gtag.js) - Google Analytics