This is the 18th day of my participation in the August Genwen Challenge.More challenges in August
Internationalization of software: software should be developed so that it can respond to visits from different regions and countries in the world at the same time, and provide pages or data corresponding to visitors’ reading habits for visits from different regions and countries. Internationalization is also called I18N: Internationalization. In layman’s terms, a set of software provides several different sets of interfaces, depending on the country and language of the visitor, to display the corresponding interface.
In fact, the JDK realized software internationalization, in the DOS window input javac command, DOS window will pop up Chinese information. When you change the language of your computer to English, go to the DOS window again and type the javac command, the DOS window pops up with the English message.
What are the characteristics of software internationalization:
- For text elements that are regularly used in the program, such as those used in the menu bar and navigation bar, or error messages and status information, you need to select text in different languages according to the region and country of the visitor.
- For dynamically generated data such as date, currency, etc., the software should be able to display it according to the culture of the country or region in which it is currently located.
Let’s look at the first feature
Fixed internationalization of text elements
- For fixed text information such as menu bar, navigation bar, error message and status information in software, you can write them in a properties file, and write different properties files according to different countries. This set of properties files is called a resource bundle.
- The JavaAPI provides a ResourceBundle class to describe a ResourceBundle, and the ResourceBundle class provides the corresponding method getBundle, which automatically retrieves and displays the corresponding resource file based on the visitor’s country or region.
Some considerations for creating resource bundles and resource files
- Each resource file in a resource bundle must have a common base name. In addition to the base name, the name of each resource file must have an additional part that identifies its local information. For example, if the base name of a resource package is myproperties, the name of the resource file corresponding to the Chinese or English environment is “myproperites_zh.properties” “myproperites_en.properties”.
- Each resource bundle should have a default resource file with no additional parts identifying local information. If the ResourceBundle object does not find a resource file in the ResourceBundle that matches the user, it selects the resource file closest to the user in the ResourceBundle. If it does not find any more, it uses the default resource file. For example myproperites. The properties.
Post a language and country code
The language code | instructions | | | Country code | instructions |
De | German | | | CN | China |
es | Spanish | | | CA | Canada |
En | English | points | DE | Germany |
Fr | French | cut | FR | France |
Ja | Japanese | line | IN | India |
Jw | Javanese | | | US | United State |
ko | Korean | | | ||
Zh | Chinese | | |
Resource bundle files are usually created in the SRC directory. Let me show you. Create a new Web project named day07. Create three configuration files in the SRC directory: myproperties.properties, myproperties_zh_CN. Properties, and myproperties_en_US. Properties file name = default myproperties_zh_cn.properties file name = zhang SAN Myproperties_en_us. properties file name = Mary some students will find that name = zhang SAN configuration file will not save, because the configuration file can not save Chinese. By the way, let’s talk about the format of resource files.
- The content of the resource file is usually in the form of “keyword = value”, and the software retrieves the value according to the keyword and displays it on the page. The keywords of all resource files in a resource bundle must be the same, and the value must be the corresponding country literal.
- In addition, the resource file adopts the properties format file, so all characters in the file must be ASCII characters. For non-ACSII characters like Chinese characters, they must be encoded first. Java provides an native2ascII command for encoding.
So if you want to save Chinese in a configuration file, you first have to convert Chinese to Unicode. How do you do that? We can open a DOS window and type in the windownative2ascii
When you press Enter, the DOS window will wait for the user’s input. At this point, you can enter the Chinese information to be converted, such as Zhang SAN, and the window will display the corresponding Unicode code.Now we can achieve Unicode encoding for several Chinese characters, but what if there is a large amount of Chinese data that needs to be converted in batches? Create an A. perties file on your desktop and type name = Zhang SAN City = Beijing. Our goal is to convert all the contents of this file to Unicode encoding. Open the DOS window, switch the path to your file directory, that is, to the desktop directory, and type
native2ascii a.properties b.properties
Copy the code
The first file is the file to transcode, and the second file is the generated Unicode encoding file.When I return to my desktop, I find an extra B. perties file. When I open it, two Chinese messages have been converted to Unicode encoding. With this small tool, you can easily implement the Chinese to Unicode encoding problem. That seems a little off topic. Ha ha, please forgive my sharing heart.
Ahem, continue today’s content. Once we have the configuration file, we should read the file. New test class, write test code
@Test
public void demo1(a){
// Use ResourceBundle to read the configuration file without setting the country
ResourceBundle bundle = ResourceBundle.getBundle("myproperties");
// The configuration file is selected based on the system language by default
System.out.println(bundle.getString("name"));
}
Copy the code
Run the codeThe configuration file with Chinese information is running.
One such class exists in Java, the Locale class, which represents the country and language. What does it do? Feel it through a case study. Write test code
@Test
public void demo2(a){
// Set the country when reading the file
Locale locale = new Locale("en"."US");// Specify the country as the United States
ResourceBundle bundle = ResourceBundle.getBundle("myproperties",locale);
System.out.println(bundle.getString("name"));
}
Copy the code
Run the codeThe configuration file is in English. It should be obvious what the Locale class does. There’s a priority issue here, which is setting the Locale class has the highest priority, which country and language you’re setting, which language’s configuration file you’re running, system country Settings, and then the default.
With the basics covered, let’s write a login case to deepen our understanding. Create a new login. JSP file
The < %@page import="java.util.ResourceBundle"%>
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<html>
<head>
<title>Insert title here</title>
</head>
<body>
<%
ResourceBundle bundle = ResourceBundle.getBundle("myproperties");
%>
<h1><%=bundle.getString("forminfo") %></h1>
<form>
<%=bundle.getString("username") %><input type="text" name="username" /><br />
<%=bundle.getString("password") %><input type="password" name="password" /><br />
<input type="submit" value="<%=bundle.getString("submit") % >" />
</form>
</body>
</html>
Copy the code
Now, internationalize the text elements of the JSP. Modify the configuration files separately, starting with the myProperties.properties file
forminfo = default Login Form
username = default Username
password = default Password
submit = default Submit
Copy the code
Then modify the myproperties_en_us.properties file
forminfo = Login Form
username = Username
password = Password
submit = Submit
Copy the code
Finally modify the myproperties_zh_cn.properties file
forminfo = \u767B\u5F55\u8868\u5355
username = \u7528\u6237\u540D
password = \u5BC6\u7801
submit = \u63D0\u4EA4
Copy the code
Deploy and run the projectSince we did not specify the country, the system default should prevail and the information displayed is in Chinese. Now let’s manually specify the country. Modify the login.jsp file
ResourceBundle bundle = ResourceBundle.getBundle("myproperties",Locale.US);
Copy the code
All you need to do is change the code that creates the ResourceBundle object. The Locale class defines various country-specific constants, so you can simply pass in the Locale class fields when building the ResourceBundle object. Now let’s rerun the program.As we’ve already said, the Locale class sets the country with the highest priority, so it shows the information in English. The above code uses a lot of <%=%>, which is not only troublesome, but also ugly. We can use JSTL to display data. Modify the login.jsp file
The < %@page import="java.util.Locale"% > < %@page import="java.util.ResourceBundle"%>
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%> <%-- Import internationalized tag library --%> <%@taglib uri="http://java.sun.com/jsp/jstl/fmt" prefix="fmt" %>
<html>
<head>
<title>Insert title here</title>
</head>
<body>
<%
ResourceBundle bundle = ResourceBundle.getBundle("myproperties",Locale.US);
%>
<h1><%=bundle.getString("forminfo") %></h1>
<form>
<%=bundle.getString("username") %><input type="text" name="username" /><br />
<%=bundle.getString("password") %><input type="password" name="password" /><br />
<input type="submit" value="<%=bundle.getString("submit") % >" />
</form>
<hr/>
<fmt:setLocale value="zh_CN"/>
<fmt:setBundle basename="myproperties" var="bundle" scope="page"/>
<h1><fmt:message key="forminfo" bundle="${bundle }"/></h1>
<form>
<fmt:message key="username" bundle="${bundle }"/><input type="text" name="username" /><br />
<fmt:message key="password" bundle="${bundle }"/><input type="password" name="password" /><br />
<input type="submit" value="<fmt:message key="submit" bundle="${bundle }"/ >" />
</form>
</body>
</html>
Copy the code
Run the projectThe same effect is successful, but the code looks cleaner and more comfortable than the first implementation.