Get the App
SLTechnology News&Howtos  ›  Internet Technology  › 

On the use of language internationalization ResourceBundle in java1.8

Shulou Source: shulou.com Published: 2022-06-03 06:35:37 09月16日 Update

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");}}

Tags: File content configuration method software Chinese English two field format program path development support selection language code function dynamic parameter Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno Microsoft MariaDB Redmi macOS Linux