Java resourcebundle.

Source: Internet
Author: User

Java resourcebundle is a useful tool that can be conveniently used to implement Software Internationalization and localization functions. Because resourcebundle can automatically search for resource files (. properties files) in a certain culture and region under a directory according to certain rules ). By providing different resource files for different cultures and regions, and then letting Java automatically read these resource files according to rules, the software localization is actually completed.

Let's take a look at a small example to create a test project. The directory structure is as follows:
Test/
Classmain. Java
Message_zh.properties
Message_en.properties

There are only one row of the two message *. properties files, respectively:
Message_zh.properties:
Msg0 = "Chinese locale"
Message_en.properties:
Msg0 = "English locale"

Classmain. Java is also simple, and the main program has only two rows:
Import java. util .*;

Public class classmain {
Public static void main (string ARGs [])
{
Resourcebundle RB = resourcebundle. getbundle ("message "--);
System. Out. println (RB. getstring ("msg0 "));
}
}

Compile javac classmain. java to generate classmain. class file, which runs Java classmain under locale and locale respectively. class, the output strings are "Chinese locale" and "English locale".

The key to looking at the above two lines of code is resourcebundle. getbundle ("message"). Obviously, this message is closely related to the two message *. properties files. After reading the JDK documentation, we can find that resourcebundle uses "message" as a basename and searches for basename _ * based on the current locale and country _*. properties file, so when the local is both Chinese and English, resourcebundle uses the message_zh.properties and message_en.properties files respectively. Then resourcebundle reads the found. properties file, and process the resources in it, which is not difficult to understand resourcebundle. the result of getstring ("msg0") is (msg0 = "** locale").

To verify the above analysis, I checked the Java source code again. getbundle actually calls getbundleimpl instead. getbundleimpl first determines the actually used locale, and then forms a search list based on the Search rules, it is then handed over to findbundle for processing, and then completed by findbundle. search for properties files. All the code is not listed in Java. util. resourcebundle. Java of JDK.

The implementation of eclipse internationalization and localization is actually implemented based on resourcebundle (of course, thanks to the powerful plug-in mechanism of eclipse ). I learned a little about it because of my work needs and wrote it down to prevent forgetting it.

I am a beginner in Java. If you have any mistakes, please criticize them and despise them.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.