SpringMVC 19. Internationalization

globalization

Overview

  • By default, SpringMVC determines the localization type of the client based on the Accept-Language parameter.

  • When receiving a request, SpringMVC will look for a localization resolver (LocalResolver) in the context, and use it to obtain the localization type information corresponding to the request.

  • SpringMVC also allows to assemble an interceptor that dynamically changes the localization type, so that the localization type of a single request can be controlled by specifying a request parameter.

  • Example 1:

in18_index.jsp

<html>
<head>
    <title>in18_index</title>
</head>
<body>
        <a  href="i18n">i18n page</a>
</body>
</html>

i18n.jsp :

<%@ taglib prefix="fmt" uri="http://java.sun.com/jsp/jstl/fmt" %>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
    <title>i18n</title>
</head>
<body>
    <fmt:message key="i18n.username"></fmt:message>
    <a href="i18n2">i18n2 page</a>
</body>
</html>

i18n2.jsp:

<%@ taglib prefix="fmt" uri="http://java.sun.com/jsp/jstl/fmt" %>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
    <title>i18n2</title>
</head>
<body>
    <fmt:message key="i18n.password"></fmt:message>
    <a href="i18n">i18n page</a>
</body>
</html>

springmvc.xml added:

 <!--配置国际化资源文件-->
    <bean  id="messageSource" class="org.springframework.context.support.ResourceBundleMessageSource">
        <property name="basename" value="i18n"></property>
    </bean>

<!--配置直接转发的页面-->
    <!--可以直接转发相应的页面,无需再经过Handler方法-->
    <mvc:view-controller path="i18n" view-name="i18n"></mvc:view-controller>
    <mvc:view-controller path="i18n2" view-name="i18n2"></mvc:view-controller>

  • resource

i18n.properties

i18n.username=username
i18n.password=password

i18n_zh_CH.properties

i18n.username=用户名
i18n.password=密码

i18n_en_US.properties

i18n.username=Username
i18n.password=Password

How SessionLocaleResolver &LocaleChangeInterceptor works

Working principle diagram

Localization Parser and Localization Interceptor

  • AcceptHeaderLocaleResolver: Determine the localization type according to the Accept-Language parameter of the HTTP request header. If no localization resolver is explicitly defined, SpringMVC uses the resolver.

  • CookieLocaleResolver: Determines the localization type based on the specified cookie value

  • SessionLocaleResolver: Determines the localization type based on specific properties in the Session

  • LocaleChangeInterceptor: Get
    the .

Modify springmvc.xml

<!--配置直接转发的页面-->
    <!--可以直接转发相应的页面,无需再经过Handler方法-->
    <mvc:view-controller path="success" view-name="success"></mvc:view-controller>
    <!--<mvc:view-controller path="i18n" view-name="i18n"></mvc:view-controller>-->
    <mvc:view-controller path="i18n2" view-name="i18n2"></mvc:view-controller>

springmvc.xml adds:

<!--配置:SessionLocaleResolver-->
    <!--此处id必须是localeResolver-->
    <!--否则发生Request processing failed; nested exception is java.lang.UnsupportedOperationException: Cannot change HTTP accept header - use a different locale resolution strategy-->
    <bean id="localeResolver" class="org.springframework.web.servlet.i18n.SessionLocaleResolver"></bean>

    <!--配置LocaleChangeInterceptor-->
    <mvc:interceptors>
        <bean id="localeChangeInterceptor" class="org.springframework.web.servlet.i18n.LocaleChangeInterceptor"></bean>
    </mvc:interceptors>

i18n.jsp adds:

    <a href="i18n?locale=zh_CH">中文</a>
    <a href="i18n?locale=en_US">英文</a>

handler:I18nHandler.java


import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.support.ResourceBundleMessageSource;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;

import java.util.Locale;


@Controller
public class I18nHandler {

        @Autowired
        private ResourceBundleMessageSource messageSource ;

        @RequestMapping("/i18n")
        public String testI18n(Locale locale ){
            String val = messageSource.getMessage("i18n.username",null ,locale) ;
            System.out.println(val);
            return "i18n" ;

        }
}

The language display can be switched on the i18n page.

toggle

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325396968&siteId=291194637