Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to realize internationalization of Java

2025-01-17 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/02 Report--

This article mainly explains "how to internationalize Java". Interested friends may wish to have a look. The method introduced in this paper is simple, fast and practical. Let's let the editor take you to learn how to internationalize Java.

Internationalization in Java

Internationalization means that the project can be supported by the languages of all countries. Since Java is a cross-platform language, it must be used in a variety of different language environments. In order to solve this problem, Java provides us with a tool class ResourceBundle to help us achieve the internationalization of Java. The core idea is to provide a different resource file for different languages.

Steps for Java to achieve internationalization:

1. Write the main program to achieve internationalization.

two。 Define the resource file, and note that the definition of the resource file should conform to certain specifications.

The specification is as follows: if we name the default resource file message.properties, then the corresponding resource file name for other languages is the message_ language code _ country code.properties. For example: our language Chinese language code is zh, the country code is CN, so the simplified Chinese resource file name is: message_zh_CN.properties, English language code is en, the United States country code is US, so the American English resource file is: message_en_US.properties.

3. Obtain the corresponding resource files according to the environment of the software.

4. Get the corresponding value of key in the obtained resource file.

Specific code examples are as follows:

Import java.text.MessageFormat

Import java.util.Date

Import java.util.Locale

Import java.util.ResourceBundle

/ * *

* demonstrate the internationalization of Java

*

, /

Public class ParamFormat {

/ * *

* @ param args

, /

Public static void main (String [] args) {

/ / Test the default resource file

TestCustom ()

/ / testing American English resource files

TestDefault ()

}

Public static void testDefault () {

/ / get the default system area

Locale locale = Locale.getDefault ()

/ / obtain the resource file

ResourceBundle rb = ResourceBundle.getBundle ("message", locale)

/ / obtain the corresponding key value

String greeting = rb.getString ("greeting")

String userInfo = rb.getString ("userinfo")

String name= "liky"

Int age = 18

Date birth = new Date ()

/ / format parameters, note that it is an array of Object, which means that any type of object can be formatted into the template.

Object [] params = {name,age,birth}

/ / format the parameter and return the formatted string

String result = MessageFormat.format (userInfo, params)

System.err.println (greeting + result)

}

Public static void testCustom () {

/ / set the custom language country code

Locale locale = new Locale ("en_US")

/ / obtain the resource file

ResourceBundle rb = ResourceBundle.getBundle ("message", locale)

/ / obtain the corresponding key value

String greeting = rb.getString ("greeting")

String userInfo = rb.getString ("userinfo")

String name= "liky"

Int age = 18

Date birth = new Date ()

/ / format parameters, note that it is an array of Object, which means that any type of object can be formatted into the template.

Object [] params = {name,age,birth}

/ / format the parameter and return the formatted string

String result = MessageFormat.format (userInfo, params)

System.err.println (greeting + result)

}

}

The default resource file message.properties (here native2ascii is used to convert Chinese to ISO-8859-1 encoding.)

Greeting=/u6B22/u8FCE/u60A8/u7684/u5230/u6765

Userinfo=/u59D3/u540D/: {0}, / u5E74/u9F84/: {1}, / u751F/u65E5/: {2}.

American English resource file message_en_US.properties

Greeting=Welcome my boy

Userinfo=name: {0}, age: {1}, birthday: {2}.

At this point, I believe you have a deeper understanding of "how to internationalize Java". You might as well do it in practice. Here is the website, more related content can enter the relevant channels to inquire, follow us, continue to learn!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report