11.SpringMVC-国际化

本文最后更新于2024.04.28-04:32,某些文章具有时效性,若有错误或已失效,请在下方留言或联系涛哥

SpringMVC 实现国际化的 方式:

1.利用切换浏览器的语言(实际是修改了 请求头里accept-language的值)

2.自定义

a.在resouce下 新建 i18n文件夹 文件夹内创建 视图名.properties 试图名zh_CN.properties 试图名en_US.properties文件

 b.在资源文件中 去设置不用的 键 值 来表示 要被引用的文本 建议: txt.username=用户名

index.properties

txt.welcome=欢迎

index_zh_CN.properties

txt.welcome=欢迎

 index_en_UK.properties

txt.welcome=welcome

c.在spring-mvc.xml里创建

 <bean class="org.springframework.context.support.ResourceBundleMessageSource" id="messageSource">
        <property name="defaultEncoding" value="UTF-8"></property>
        <property name="basenames">
            <array>
                <value>i18n/index</value>
            </array>
        </property>
  </bean>

d.在jsp页面中 使用spring标签库  <spring:message code="txt.username" />来引用资源文件。

<%--
  Created by IntelliJ IDEA.
  User: Administrator
  Date: 2021/4/8
  Time: 9:32
  To change this template use File | Settings | File Templates.
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<%@ taglib prefix="s" uri="http://www.springframework.org/tags"   %>
<html>
  <head>
    <title>$Title$</title>
  </head>
  <h1><s:message code="txt.welcome"/></h1>
</html>
阅读剩余
THE END