Java.util.ResourceBundle Usage Explanation

Source: Internet
Author: User

The first knowledge of internationalization and resourcebundle is mainly used to solve internationalization and localization problems. Internationalization and localization are not two concepts, both of which appear together. It can be said that the purpose of internationalization is to achieve localization, detailed introduction can be seen in the end of this article. For example, "Cancel", in Chinese we use "Cancel" to express, and in English we use "Cancel". If our program is international (this is a trend of software development), then the use of the population is necessarily a multi-lingual environment, to achieve internationalization is very necessary. And ResourceBundle can help us do this easily: when a program needs a locale-specific resource, such as a String, the program can load it from a resource bundle that is appropriate for the current user locale (in most cases, the. properties file). This allows you to write program code that is largely independent of the user's locale, isolating most, if not all, of the language-specific information in the resource bundle.

This enables the written program to:

    • Easily localize or translate to different languages
    • Working with multiple locales at once
    • can be easily modified in the future in order to support more locales
Testing and validation let's simulate a multi-lingual environment. Define Four resource files: res_en_us.properties, Res_zh_cn.properties, Res_zh.properties, Res.propertiesres_en_ Us.properties:cancelkey=cancelres_zh_cn.properties:cancelkey=\u53d6\u6d88 (Cancel)
Res_zh.properties:cancelkey=\u53d6\u6d88zh (cancel en)
Res.properties:cancelkey=\u53d6\u6d88default (cancel default)

Naming rules follow: Resource name +_ language _ Country. Properties, where localized information is defined in each resource file, how does the system fetch the corresponding resource file?
ResourceBundle bundle = Resourcebundle.getbundle ("res", New Locale ("zh", "CN"));

Where new Locale ("zh", "CN"), this object tells the program your localization information, take this to say: The program first will go to classpath under Search res_zh_cn.properties if not exist, then will go to find Res_ Zh.properties, if still does not exist, will go to look for res.properties, if still can't find, then should throw exception: MissingResourceException We can write a test program to verify:
Package Bundle.test;import Java.util.locale;import Java.util.resourcebundle;public class Bundletest {public static void Main (String args[]) {ResourceBundle bundle = Resourcebundle.getbundle ("res", New Locale ("zh", "CN")); String Cancel = bundle.getstring ("Cancelkey"); System.out.println (cancel); bundle = Resourcebundle.getbundle ("res", locale.us); cancel = bundle.getstring ("Cancelkey "); System.out.println (cancel); bundle = Resourcebundle.getbundle ("res", Locale.getdefault ()); cancel = bundle.getstring ( "Cancelkey"); System.out.println (cancel); bundle = Resourcebundle.getbundle ("res", Locale.german); cancel = bundle.getstring (" Cancelkey "); System.out.println (cancel);}}

Output:
Cancel Cancel Cancellation Cancel

Here the first three are in our expected range, but the last German, should go to use res.properties this resource bundle it? How did you use the res_zh_ch.properties? The original ResourceBundle provided us with a fallback (that is, a fallback scheme), which is the localized information based on the current system's locale. So if can not find German, then will go to find China, so found res_zh_ch.properties this resource package I also see the source code to understand, the following is affixed to some key source:
ResourceBundle basebundle = null;for (locale targetlocale = locale;     Targetlocale! = null;     Targetlocale = Control.getfallbacklocale (BaseName, Targetlocale)) {//This is the way to get the backup plan    //Do something we don't care for the moment}

Trace Control.getfallbacklocale (baseName, targetlocale) See what the backup plan is?
Public Locale Getfallbacklocale (String baseName, locale locale) {    if (baseName = = null) {throw new Nullpointerexceptio n ();    }    Locale Defaultlocale = Locale.getdefault ();    Return Locale.equals (Defaultlocale)? Null:defaultlocale;}
When explicitly defined localization information is not the current system localization information, if not explicitly defined to find the resource bundle, then go through the current system of localization information to find ~ Finally put a little bit of knowledge ~
Internationalization and LocalizationInternationalization (internationalization) is the process of designing an application for multiple languages and regions. The implication for multiple languages and regions is that when users use the application in different languages and in different regions, the application must display the information in a language that they can understand and conform to their cultural habits. Internationalization is sometimes referred to as i18n, since there are 18 letters between the letters I and N of an internationalized English word.
An internationalized program usually has the following characteristics:
    • There is an additional localized data (localized) and the ability to execute in every region of the world.
    • Elements of text, such as state information or the lables of the GUI section, are not written directly (hardcoded) in the program, but are stored in localized data and can be used dynamically by the program.
    • When you support a new language, you do not need to modify the program, and you do not need to recompile.
    • Data on cultural differences, such as dates and currencies, must be presented in different formats according to the language and habits of the advocates.
    • Can be quickly localized.
Localization (Localization) is the process of adapting an application to different languages and regions by adding locally described artifacts (locale-specific components) and text translation work. Localization is sometimes abbreviated as L10N, which should be between the letters L and N with 10 letters in the localized English word. Usually localization is the most time-consuming work should be text translation. Localization workers have to create new formats for data such as dates, numbers, and currencies, based on the specific needs of the region. Other types of data, such as sound, image, etc., also need to be localized according to the specific needs.


Java.util.ResourceBundle Usage Explanation

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.