Rookie Learning SSH-Struts Internationalization

Posted by cschotch on Sat, 27 Jul 2019 07:34:16 +0200

Links to the original text:

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:



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 {
		        Locale l = Locale.getDefault();   
		           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");   
		      return "success";   





<?xml version="1.0" encoding="UTF-8"?>
<web-app version="2.5" 





<?xml version="1.0" encoding="UTF-8" ?>
    "-//Apache Software Foundation//DTD Struts Configuration 2.0//EN"

    <!-- 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">
            <result name="input">





<!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+"/";

    <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">	
  <s:i18n name="local.message">
    <s:form action="/i18n.action" method="post">
      <s:textfield name="user.userName" label="%{getText('')}"/>
      <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>

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:

Topics: Struts Java xml JSP