mustache的国际化支持

一、国际化的实现

1、上一章中,我们在sprihg配置文件中配置了国际化信息文件前缀,同时定义了mustache的拦截器,这个实际上就是为了支持国际化。再将配置贴出:

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:aop="http://www.springframework.org/schema/aop"
xmlns:c="http://www.springframework.org/schema/c" xmlns:cache="http://www.springframework.org/schema/cache"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:jdbc="http://www.springframework.org/schema/jdbc" xmlns:jee="http://www.springframework.org/schema/jee"
xmlns:lang="http://www.springframework.org/schema/lang" xmlns:mvc="http://www.springframework.org/schema/mvc"
xmlns:p="http://www.springframework.org/schema/p" xmlns:task="http://www.springframework.org/schema/task"
xmlns:tx="http://www.springframework.org/schema/tx" xmlns:util="http://www.springframework.org/schema/util"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop.xsd
http://www.springframework.org/schema/cache http://www.springframework.org/schema/cache/spring-cache.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd
http://www.springframework.org/schema/jdbc http://www.springframework.org/schema/jdbc/spring-jdbc.xsd
http://www.springframework.org/schema/jee http://www.springframework.org/schema/jee/spring-jee.xsd
http://www.springframework.org/schema/lang http://www.springframework.org/schema/lang/spring-lang.xsd
http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc.xsd
http://www.springframework.org/schema/task http://www.springframework.org/schema/task/spring-task.xsd
http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx.xsd
http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util.xsd">

<!-- 自动扫描controller包下的所有类,使其认为spring mvc的控制器 -->
<context:component-scan base-package="com.zzq.test" />

<!-- mustache的视图解析器 -->
<bean id="viewResolver" class="org.springframework.web.servlet.view.mustache.MustacheViewResolver">
<property name="cache" value="false" />
<property name="prefix" value="/WEB-INF/views/" />
<property name="suffix" value=".html" />
<property name="templateLoader">
<bean class="org.springframework.web.servlet.view.mustache.MustacheTemplateLoader" />
</property>
</bean>

<!-- 国际化信息文件 -->
<bean id="messageSource"
class="org.springframework.context.support.ReloadableResourceBundleMessageSource">
<property name="basename" value="classpath:message" />
</bean>

<bean id="localeResolver" class="org.springframework.web.servlet.i18n.SessionLocaleResolver">
<property name="defaultLocale" value="zh" />
</bean>

<bean id="localeChangeInterceptor" class="org.springframework.web.servlet.i18n.LocaleChangeInterceptor">
<property name="paramName" value="locale" />
</bean>

<bean
class="org.springframework.web.servlet.mvc.annotation.DefaultAnnotationHandlerMapping">
<property name="interceptors">
<list>
<ref bean="localeChangeInterceptor" />
<ref bean="messageInterceptor" />
</list>
</property>
</bean>

<!-- mustache模板中取i18n信息的拦截器 -->
<bean id="messageInterceptor" class="org.springframework.web.servlet.i18n.MustacheMessageInterceptor">
<constructor-arg ref="messageSource" />
<constructor-arg ref="localeResolver" />
</bean>
</beans>

2、测试

首先在message_zh.properties中添加select=选择,message_zh_TW.properties中添加select=選擇,然后再MyController中新增一个方法,并添加i18n.html,如下:

package com.zzq.test.controller;

import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;

@Controller
@RequestMapping("/my")
public class MyController {
	
	@RequestMapping("/hello.mu")
	public String hello(Model model){
		model.addAttribute("name", "zzq");
		//此处return的为模板,以WEB-INF/views为相对路径,.html为默认后缀,所以此处不加后缀。具体的后缀为mvc文件中配置
		return "hello";
	}
	
	@RequestMapping("/i18n.mu")
	public String i18n(){
		return "i18n";
	}
}
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Hello Page</title>
</head>
<body>
{{#i18n}}select{{/i18n}}
</body>
</html>

测试访问http://localhost:8080/my/i18n.mu?locale=zh  和http://localhost:8080/my/i18n.mu?locale=zh_TW分别可以看到中文简体和繁体。

二、分析

分析是由于配置了mustache的拦截器org.springframework.web.servlet.i18n.MustacheMessageInterceptor,查看拦截器可以看到,该类中指定了对于标签为i18n(可以通过配置文件修改这个值)的标签,将里面的值当作key进行了国际化处理,即{{#i18n}}select{{/i18n}},这边的select就是key,将select当作key去国际化文件查找匹配的值然后再回填到模板。

代码下载地址:http://www.oschina.net/action/code/download?code=48746&id=70650

上一篇:mustache的环境搭建

下一篇:mustache的标签使用


转载于:https://my.oschina.net/zzq350623/blog/466697

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值