Internationalization in Java

Source: Internet
Author: User

Internationalization, English is called internationalization word too long, and is referred to as i18n (take the head to take the middle of the tail 18 letters) without shouting, this is OK! Then look at internationalization, which means that the product or program can adapt to the needs of different languages and regions without having to make any changes. Also greet in China you will say "Hello", in the United States you will say "Hello", you see, you are already i18n.

Internationalization in Java is primarily a tool class resourcebundle, the core idea is to provide a different resource file for different locales. So let's take a look at how the resource file is defined.

Define the corresponding resource file, we put the resource file in a resource bundle (which is the usual package), each resource file in a resource bundle must have a common base name. In addition to the base name, the name of each file must also have an additional part that identifies its local information, for example: the base name of a resource bundle is "message", the resource filenames corresponding to the Chinese (Chinese), English (United States) environment are:
Message_zh_cn.properties
Message_en_us.properties

It is important to note that the resource file is usually in the form of a key-value pair, and the resource file is in the properties format file, so all the characters in the file must be ASCLL code, cannot be saved in Chinese, Java provides the NATIVE2ASCLL tool to convert Chinese is the ASCLL code. So when writing the properties file is written in Chinese, a return to the car is automatically encoded.

Here is the specific code, in the Java API provides a resourcebundle class to describe a resource bundle, and the ResourceBundle class provides a corresponding static method Getbundle, which can be automatically bound according to the country region of the visitors corresponding The resource file.

ImportJava.util.Locale;ImportJava.util.ResourceBundle; Public classi18n{ Public Static voidTest () {//set up a custom language country codeLocale Locale1 =NewLocale ("en_US"); Locale Locale2=Locale.getdefault (); //Get resource FilesResourceBundle RB = Resourcebundle.getbundle ("Message.i18n.message", Locale2); //get the corresponding key valueString greeting = rb.getstring ("greeting"); String UserInfo= rb.getstring ("name");        System.out.println (greeting);     System.out.println (UserInfo); }         Public Static voidMain (string[] args) {test (); }}//Hello//Yu 6 Road

The above is the internationalization of some fixed text, fixed text including the menu name, navigation bar, system information, etc., are manually configured in the properties file, but some data, for example, value, currency, time, date, etc. because it may be generated dynamically during the operation of the program, it can not be as simple as text To separate them from the application and require special handling. Java provides an API to address these issues.

Let's just say the Locale class, the English introduction is like this

A Locale object represents a specific geographical, political, or cultural region. An operation, requires a locale to perform it task is called Locale-sensitive and uses the locale to tailor Informati On for the user.

The Locate object represents a particular geographical, political, or cultural area. Operations that require Locate settings to perform tasks are called sensitive areas (classes that contain Locate are sensitive areas) and use Locate to customize information for users.

On the internationalization of the date we use the tool class DateFormat and look at the structure diagram of the class.

DateFormat has a lot of different constructors, and in the constructor can also specify a different time display mode, I do not show each one, the following shows an example code.

 Public classDateformattest { Public Static voidMain (string[] args)throwsparseexception {Date Date=NewDate (); DateFormat DF=dateformat.getdateinstance (Dateformat.default, Locale.china); String Str=Df.format (date); System.out.println (str); //2018-8-6DF=dateformat.gettimeinstance (Dateformat.long, Locale.china); System.out.println (Df.format (date)); //07:58 P.M. 26 secDF=dateformat.getdatetimeinstance (Dateformat.full, Dateformat.short, Locale.china); System.out.println (Df.format (date)); //August 6, 2018 Monday afternoon 7:58//get the default DateFormatDF =dateformat.getinstance (); System.out.println (Df.format (date)); //18-8-6 pm 7:58//format a string as a Date object using DateFormat reverseString datestring = "August 6, 2018 Monday 7:58"; DateFormat DF2=dateformat.getdatetimeinstance (Dateformat.full, Dateformat.short, Locale.china); Date Date2= Df2.parse (datestring);//There may be exceptions, and the format of the string and the format of the DateFormat setting will be an error. System.out.println (DATE2);//Mon 19:58:00 CST 2018    }}

Dynamic text internationalization using the tool class Messageformat, Messageformat allows developers to replace sensitive data in strings (that is, internationalized related data) in strings with {0} {1} {2} {3} as placeholders.

 Public classMessageformattest { Public Static voidMain (string[] args) {/*test Data greeting=welcome name=yjk age=18 pattern={0},{1},your is {2}. */ResourceBundle RB= Resourcebundle.getbundle ("Message.i18n.message_en_US", locale.us); String Greeting= rb.getstring ("greeting"); String name= rb.getstring ("name"); String Age= Rb.getstring ("Age"); //Get pattern stringString pattern = rb.getstring ("message"); //instantiate the Messageformat object and load the corresponding pattern stringMessageformat format =NewMessageformat (pattern); //The format method requires an array of Object. Object[] params ={Greeting, name, age}; //formatting a pattern string, specifying a placeholder for the corresponding replacement object in the parameter arrayString string =Format.format (params);    System.out.println (string); }}//Welcome,yjk,your is.

The NumberFormat class can format a numeric value string that conforms to a country's local customs, or it can parse a numeric string that conforms to a country's locale to a corresponding numeric value. The corresponding methods are format and parse respectively.

 Public classNumberformattest { Public Static voidMain (string[] args)throwsparseexception{intPrice = 18; //NumberFormat nf = numberformat.getcurrencyinstance (Locale.china);//¥18.00//NumberFormat nf = numberformat.getcurrencyinstance (locale.us);//$18.00//NumberFormat nf = numberformat.getcurrencyinstance (Locale.japan);//¥18//get the NumberFormat instance object for processing currencyNumberFormat NF = numberformat.getcurrencyinstance (locale.france);//18,00€System.out.println (Nf.format (price)); String s= "18,00€"; NF=numberformat.getcurrencyinstance (locale.france); //The result of parsing 18,00€ in French currency is 18.0 .System.out.println (Nf.parse (s). Doublevalue ()); DoubleD = 0.1; //Gets an NumberFormat instance object that processes integers. NF = numberformat.getintegerinstance ();//0//get a NumberFormat instance object that processes percent valuesNF =numberformat.getpercentinstance (); System.out.println (Nf.format (d)); //10%    }}

Related Article

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.

Tags Index: