springboot+thymeleaf或者springboot+freemarker的国际化

springboot+thymeleaf

参考网站http://blog.csdn.net/lwphk/article/details/41822447


pom.xml

<dependency>  
    <groupId>org.springframework.boot</groupId>  
    <artifactId>spring-boot-starter-thymeleaf</artifactId>  
</dependency> 

application.yml

spring: 
    messages:
        basename: i18n/messages #用于国际化,可以包括多个,以逗号分隔。每一个ResourceBundle

package com.yxhd.web.config;

import java.util.Locale;

import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.LocaleResolver;
import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurerAdapter;
import org.springframework.web.servlet.i18n.LocaleChangeInterceptor;
import org.springframework.web.servlet.i18n.SessionLocaleResolver;

/**
 * WebMvcConfigurerAdapter自定义拦截器
 * @author Administrator
 *
 */
@Configuration
@EnableAutoConfiguration
@ComponentScan
public class LocaleConfig extends WebMvcConfigurerAdapter {

	@Bean
    public LocaleResolver localeResolver() {
        SessionLocaleResolver slr = new SessionLocaleResolver();
        // 默认语言
        slr.setDefaultLocale(Locale.US);
        return slr;
    }

    @Bean
    public LocaleChangeInterceptor localeChangeInterceptor() {
        LocaleChangeInterceptor lci = new LocaleChangeInterceptor();
        // 参数名
        lci.setParamName("lang");
        return lci;
    }

    @Override
    public void addInterceptors(InterceptorRegistry registry) {
        registry.addInterceptor(localeChangeInterceptor());
        super.addInterceptors(registry);
    }

}
package com.yxhd.web.controller;

import java.util.Locale;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.MessageSource;
import org.springframework.context.i18n.LocaleContextHolder;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;


@Controller
@RequestMapping("/")
public class MainController {

	@Autowired
	private MessageSource messageSource;
	
	//跳转到首页面
	@RequestMapping("/")
	public ModelAndView index(){
		ModelAndView mv = new ModelAndView();
		mv.addObject("a", "aa");
		Locale locale = LocaleContextHolder.getLocale();
		String s = messageSource.getMessage("abc", null, locale);//国际化
		System.out.println(s);
		//mv.addObject("world", messageSource.getMessage("welcome", null, locale));
		mv.setViewName("index");
		return mv;
	}	
	
}

messages_en_US.properties

welcome=welcome to my home

相似的有其它几种语言的资源文件


index.html

<p th:text="#{welcome}">Welcome Message</p>
<div th:text="${a}">Welcome Message</div>mnb
<div th:text="#{world}">Welcome Message</div>mnb

springboot+freemarker


扫描二维码关注公众号,回复: 184959 查看本文章

pom.xml

<dependency>
	<groupId>org.springframework.boot</groupId>
	<artifactId>spring-boot-starter-freemarker</artifactId>
</dependency>

首先获取spring.ftl  

获取位置在spring-webmvc-4.1.2.RELEASE.jar  包下面的 \org\springframework\web\servlet\view\freemarker\spring.ftl

在html中显示

<@spring.message code="welcome"/> 向页面输出国际化信息 

<#import "common/spring.ftl" as spring/>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8"/>
<title>Insert title here</title>
</head>
<body>
语言选择
<a href="?lang=en_US">English(US)</a>
<a href="?lang=zh_CN">简体中文</a>
<@spring.message code="welcome"/>
<form action="login" method="post">
	用户名<input type="text" name="sadminname"/><br/>
	密码<input type="password" name="spassword"/><br/>
	<input type="submit" value="登录"/>
</form>
</body>
</html>

猜你喜欢

转载自blog.csdn.net/fulq1234/article/details/79477481