Internationalization (i18n) and localization (l10n) means that products (publications, software, hardware, etc.) can adapt to non-local environments, especially other languages and cultures. Without modifying the internal code, the program can display the corresponding interface according to different languages and regions.
Principles of internationalization:
Internationalized resource files: Describe the same information in different languages and place it in their respective. properties properties properties files. The program decides which files to load according to the runtime environment.
Internationalization is accomplished mainly through the following categories:
java.util.Locale: Corresponds to a specific country/region, language environment.
java.util.ResourceBundle: Used to load a resource bundle.
I18nInterceptor: The internationalized interceptor provided by struts2 handles Locale-related information.
Internationalization process: The program obtains the country/region and language environment of the current running environment and stores them in Locale. ResourceBundle automatically searches and loads the corresponding international resource files according to the information in Locale. Before an Action is executed, the I18nInterceptor is responsible for detecting local-related information to find corresponding international resources.
Let's first look at the effect diagram of the example:
Default Chinese:
Click on English and the page will be displayed in English:
Next we look at the concrete implementation:
LoginAction
package com.lsj.action; import java.util.Locale; import org.apache.struts2.ServletActionContext; import com.opensymphony.xwork2.ActionContext; import com.opensymphony.xwork2.ActionSupport; public class LoginAction extends ActionSupport { private static final long serialVersionUID = 1L; private String flag; public String getFlag() { return flag; } public void setFlag(String flag) { this.flag = flag; } public String ha() throws Exception { this.flag=ServletActionContext.getRequest().getParameter("flag"); Locale l = Locale.getDefault(); if(this.flag==null){ l = new Locale("zh", "CN"); }else if (this.flag.equals("2")) { l = new Locale("zh", "CN"); } else if (this.flag.equals("1")) { l = new Locale("en", "US"); } ActionContext.getContext().setLocale(l); return "success"; } }
web.xml
<?xml version="1.0" encoding="UTF-8"?> <web-app version="2.5" xmlns="http://java.sun.com/xml/ns/javaee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"> <filter> <filter-name>struts2</filter-name> <filter-class>org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter</filter-class> </filter> <filter-mapping> <filter-name>struts2</filter-name> <url-pattern>/*</url-pattern> </filter-mapping> <welcome-file-list> <welcome-file>index.jsp</welcome-file> </welcome-file-list> </web-app>
struts.xml
<?xml version="1.0" encoding="UTF-8" ?> <!DOCTYPE struts PUBLIC "-//Apache Software Foundation//DTD Struts Configuration 2.0//EN" "http://struts.apache.org/dtds/struts-2.0.dtd"> <struts> <!-- Automatic Publishing --> <constant name="struts.devMode" value="true" /> <!-- Filter --> <constant name="struts.i18n.encoding" value="GBK"/> <!-- To configure i18n --> <constant name="struts.custom.i18n.resources" value="message"></constant> <!-- i18n control --> <package name="i18n" namespace="/" extends="struts-default"> <action name="i18n" class="com.lsj.action.LoginAction" method="ha"> <result name="success"> /i18nlogin.jsp </result> <result name="input"> /i18nlogin.jsp </result> </action> </package> </struts>
login.jsp
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"> <%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%> <%@ taglib prefix="s" uri="/struts-tags" %> <% String path = request.getContextPath(); String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/"; %> <html> <head> <base href="<%=basePath%>"> <title>My JSP 'i18nlogin.jsp' starting page</title> <meta http-equiv="pragma" content="no-cache"> <meta http-equiv="cache-control" content="no-cache"> <meta http-equiv="expires" content="0"> <meta http-equiv="keywords" content="keyword1,keyword2,keyword3"> <meta http-equiv="description" content="This is my page"> </head> <body> <s:i18n name="local.message"> <s:form action="/i18n.action" method="post"> <s:textfield name="user.userName" label="%{getText('login.name')}"/> <s:password name="user.userPassword" label="%{getText('login.pass')}"/> <s:submit value="%{getText('login.submit')}"></s:submit> <a href="i18n.action?flag=1">English</a> <a href="i18n.action?flag=2">Chinese</a> </s:form></s:i18n> </body> </html>
The jar package that needs to be introduced:
OK has come to realize a simple example of internationalization, some say that internationalization is not necessary, because what we do is almost exclusively for domestic use. What I want to say is: Why do we think that what we do will only serve us? Why don't we think that with our continuous efforts, we will do better and better things, and then our things will gradually enter the international market, let foreigners use what we do? Are you confident?
Reprinted at: https://www.cnblogs.com/riasky/p/3433041.html