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

时间: 2024-08-03 10:54:34

mustache的国际化支持的相关文章

gettext模块实现Python国际化支持

1.gettext模块介绍 gettext模块为Python模块和应用程序提供国际化(i18n)和本地化(l10n)服务.它提供两套API,一套高层的类似于GNU gettext的API和一套基于类的API.前者适用于单语言的场景,并且语言的选择通常依赖于用户的locale,会全局性地影响到你整个应用程序语言的转译.后者允许你在一个python模块内进行本地化,非常适用于在应用程序运行时切换你的语言. 2.通用步骤 (1)无论是使用哪一套API,都必须提取可以翻译的字符串并且生成一个翻译模板文件

(转)QT中文乱码与国际化支持

Qt内部采用的全Unicode编码,这从根本上保证了多国语界面实现的正确性和便捷性.Qt本身提供的linguist工具,用来实现翻译过程十分方便.MFC中利用资源DLL切换资源,或使用多个RC文件进行不同语言版本编译等方法都十分麻烦,如果你曾经使用过MFC,QT解决多语言问题的便捷性绝对会让你感觉是一种享受.本文讨论以下几个方面内容: 1.  QT中解决中文乱码的方法: 2.  QT中实现国家化支持. 3.  对话框实现多语言 一.       中文乱码 1.  在程序中直接使用中文,需要在程序

struts2国际化支持

几年之前,应用程序开发者能够考虑到仅仅支持他们本国的只使用一种语言(或者有时候是 两种)和通常只有一种数量表现方式(例如日期.数字.货币值)的应用.然而,基于web技术的应用程序的爆炸性增长,以及将这些应用程序部署在 Internet或其它被广泛访问的网络之上,已经在很多情况下使得国家的边界淡化到不可见.这种情况转变成为一种对于应用程序支持国际化 (internationalization,经常被称做"i18n",因为18是字母"i"和字母"n"

spring(11)------spring国际化支持

一,关于spring国际化的简介 在java编程中,对于信息的处理一般有两种方式: (1)将信息存在数据库里,用的时候从数据库里取.(惯用手法数据字典就是) (2)将信息存放在java常量类中,通过java类调用属性值. 这两种方式对于处理不需要国际化的网站,系统是能实现的,但是,如果需要国际化, 这两种方式就实现国际化非常困难. 而spring对于国际的实现提供了良好的支持,Application通过继承 org.springframework.context.MessageResource接

国际化支持(I18N)

本章译者:@nixil 使用国际化支持(I18N)能够使你的应用根据用户所在地区的不同选择不同的语言.下面介绍如何在引用中使用国际化. 只允许使用UTF-8 Play只支持UTF-8一种字符编码.这是因为多编码会导致十分诡异难以处理的问题,所以我们决定仅支持一种.UTF-8编码涵盖了所有语言的所有字符. 确认你的应用程序的每个角落都是使用UTF-8编码,也就是说: 所有的源文件都要用UTF-8来编写 HTTP头中也要使用UTF-8 把所有HTML页面的meta标签都设置为UTF-8 如果使用了数

【Spring】国际化支持

[Spring]国际化支持 一.总体结构: 两个国际化资源中的内容: 二.程序 2.1  配置Spring上下文 beans.xml文件 <?xml version="1.0" encoding="GBK"?> <beans xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://www.springframework.org/schema

SpringMVC国际化支持

这周公司领导希望我对一个项目,出一个国际化的解决方案,研究两个小时,采用了SpringMVC的国际化支持,在此记录下. 原理: 在DispatchServlet中注册localeResolver(区域解析器),并添加Locale拦截器(LocaleChangeInterceptor),来检测请求中的参数和语言环境的改变.      在应用上下文中注册ResourceBundleMessageSource,定义国际化文件在程序中的路径和名称. 1. 语言解析器 在SpringMVC中,常用的语言解

开发人员建议阅读:Spring Boot 架构中的国际化支持实践

pring Boot 主要通过 Maven 或 Gradle 这样的构建系统以继承方式添加依赖,同时继承了 Spring 框架中的优秀元素,减少了 Spring MVC 架构中的复杂配置,内置 Tomcat,Jetty 容器,使用 Java application 运行程序,而不是传统地把 WAR 包置于 Tomcat 等容器中运行,从而简化加速开发流程.此外,Spring Boot 学习简单.轻量级.容易扩展.基于这些优秀的特点,Spring Boot 成为了蓬勃发展的快速应用开发领域的领导者

Java的国际化支持(I18N问题)

一,读取PC的locale信息 Locale信息是由语言跟国家代码组成的,在Java中,提供了Locale类对应语言信息. /* * //获取local信息 Locale defaultLocale = Locale.getDefault(); * System.out.println("country:" + defaultLocale.getCountry()); * System.out.println("language:" + defaultLocale.