spring学习笔记(二十一)springboot_web开发

4.8 国际化

SpringMVC配置过程:

1)、编写国际化配置文件;

2)、使用ResourceBundleMessageSource管理国际化资源文件

3)、在页面使用fmt:message取出国际化内容

SpringBoot配置过程

步骤:

1)、编写国际化配置文件,抽取页面需要显示的国际化消息

spring学习笔记(二十一)springboot_web开发_第1张图片

2)、SpringBoot自动配置好了管理国际化资源文件的组件;

@ConfigurationProperties(prefix = "spring.messages")
public class MessageSourceAutoConfiguration {
    
    /**
	 * Comma-separated list of basenames (essentially a fully-qualified classpath
	 * location), each following the ResourceBundle convention with relaxed support for
	 * slash based locations. If it doesn't contain a package qualifier (such as
	 * "org.mypackage"), it will be resolved from the classpath root.
	 */
	private String basename = "messages";  
    //我们的配置文件可以直接放在类路径下叫messages.properties;
    //如果配置文件没有直接放在类路径下,则需要在配置文件中配置spring.message.basename指定基础名
    //如:spring.messages.basename=i18n/login
    
    @Bean
	public MessageSource messageSource() {
		ResourceBundleMessageSource messageSource = new ResourceBundleMessageSource();
		if (StringUtils.hasText(this.basename)) {
            //设置国际化资源文件的基础名(去掉语言国家代码的)
			messageSource.setBasenames(StringUtils.commaDelimitedListToStringArray(
					StringUtils.trimAllWhitespace(this.basename)));
		}
		if (this.encoding != null) {
			messageSource.setDefaultEncoding(this.encoding.name());
		}
		messageSource.setFallbackToSystemLocale(this.fallbackToSystemLocale);
		messageSource.setCacheSeconds(this.cacheSeconds);
		messageSource.setAlwaysUseMessageFormat(this.alwaysUseMessageFormat);
		return messageSource;
	}

3)、去页面获取国际化的值;



	
		
		
		
		
		Signin Template for Bootstrap
		
		
		
		
	

	
		

	

效果:根据浏览器语言设置的信息切换了国际化;

4)、点击链接切换国际化

原理:

国际化Locale(区域信息对象);LocaleResolver(获取区域信息对象);

		@Bean
		@ConditionalOnMissingBean
		@ConditionalOnProperty(prefix = "spring.mvc", name = "locale")
		public LocaleResolver localeResolver() {
			if (this.mvcProperties
					.getLocaleResolver() == WebMvcProperties.LocaleResolver.FIXED) {
				return new FixedLocaleResolver(this.mvcProperties.getLocale());
			}
			AcceptHeaderLocaleResolver localeResolver = new AcceptHeaderLocaleResolver();
			localeResolver.setDefaultLocale(this.mvcProperties.getLocale());
			return localeResolver;
		}
        //默认的就是根据请求头带来的区域信息获取Locale进行国际化
	中文
	English
package com.atguigu.springboot04web.component;

import org.springframework.web.servlet.LocaleResolver;
import org.thymeleaf.util.StringUtils;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.Locale;

public class MyLocaleResolver implements LocaleResolver {


    @Override
    public void setLocale(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse, Locale locale) {

    }

    @Override
    public Locale resolveLocale(HttpServletRequest httpServletRequest) {
        String l = httpServletRequest.getParameter("l");
        //Locale locale = Locale.getDefault();
        String acceptLanguage = httpServletRequest.getHeader("Accept-Language");
        Locale locale = new Locale(acceptLanguage);
        if (!StringUtils.isEmpty(l)) {
            String[] split = l.split("_");
            locale = new Locale(split[0], split[1]);
        }
        return locale;
    }
}
    @Bean
    public LocaleResolver localeResolver() {
        return new MyLocaleResolver();
    }

spring学习笔记(二十一)springboot_web开发_第2张图片

spring学习笔记(二十一)springboot_web开发_第3张图片

你可能感兴趣的:(spring)