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

On the use of language internationalization ResourceBundle in java1.8

2025-01-31 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >

Share

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

For example, if you want to write a software, it should support both Chinese display and operation as well as English display and operation. How to solve?

Method 1, write two sets of software separately; method 2, one set of software, two sets of configuration, make two descriptions of all the fields to be displayed, one in Chinese and one in English, when the program starts, choose the description in Chinese or English according to the operating language.

Java provides the api function of method 2, ResourceBundle, which simplifies the development process and improves the development efficiency.

It is also easy to use, create multiple configuration files, and then choose which configuration to use in the program according to the user's choice.

Figure out the path problem.

We create the Message_zh_CN.properties file and Message_en_CA.properties, which are placed under the src path of the project, and the default format is ISO-8859-1. As long as the file name conforms to xxx_xxx_xxx.properties format, the preceding part can be replaced.

The contents of the Message_en_CA.properties file, where {0} {1} {0} is the use of parameter binding, which, as you can see in the following example, allows you to dynamically bind what you need.

Info= hello {0} {1} {0}

Message_zh_CN.properties

Info=\ u4f60\ u597d\ uff01 {0} {1} {0} Code calls package baseAPI;import java.util.ResourceBundle;import java.text.MessageFormat;import java.util.Locale;public class useResourceBundle {public static void main (String [] args) {/ / configure the configuration file Message_en_CA.properties Locale zhLoc = new Locale ("en", "CA"); ResourceBundle rb = ResourceBundle.getBundle ("Message", zhLoc) / / get the field value System.out.println ("content:" + rb.getString ("info")); String str1= "content:" + rb.getString ("info"); / / the {0} {1} System.out.println (MessageFormat.format (str1, "one", "two") of the previous configuration file is used here / / Message_zh_CN.properties Locale zhLoc1 = new Locale ("zh", "CN"); rb = ResourceBundle.getBundle ("Message", zhLoc1); System.out.println ("content:" + rb.getString ("info")); str1= "content:" + rb.getString ("info"); System.out.println (MessageFormat.format (str1, "one", "two");}}

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

Internet Technology

Wechat

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

12
Report