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

springboot+thymeleaf

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

springboot+thymeleaf或者springboot+freemarker的国际化_第1张图片

pom.xml

  
    org.springframework.boot  
    spring-boot-starter-thymeleaf  
 

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

Welcome Message

Welcome Message
mnb
Welcome Message
mnb

springboot+freemarker

springboot+thymeleaf或者springboot+freemarker的国际化_第2张图片

pom.xml


	org.springframework.boot
	spring-boot-starter-freemarker

首先获取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/>




Insert title here


语言选择
English(US)
简体中文
<@spring.message code="welcome"/>
用户名
密码

你可能感兴趣的:(spring)