美文网首页
Spring boot 1.5.9 实现国际化

Spring boot 1.5.9 实现国际化

作者: 小玲子之凌空蹈虚 | 来源:发表于2018-06-06 18:10 被阅读0次

之前没有考虑过如何实现国际化,在这次的香港项目中,客户要求繁体切换简体功能,有了这篇文章。

快速入门

第一步:构建一个基础的Spring Boot应用

第二步:在pom.xml中引入依赖

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
        </dependency>
        <dependency>
            <groupId>junit</groupId>
            <artifactId>junit</artifactId>
        </dependency>

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

    </dependencies>

第三步:创建应用主类

@SpringBootApplication
public class Application {
    public static void main(String[] args) {
        SpringApplication.run(Application.class, args);
    }
}

第四步:配置文件resources下新建文件夹

image.png
welcome=hello welcome

注意必须是messages打头

第五步:创建国际化配置文件,继承WebMvcConfigurationSupport


/**
 * 类职责:
 * <p>
 * Title: MainController.java
 * Description:
 * Copyright: Copyright (c) 2018 杭州云赢网络科技有限公司
 * Company: 杭州云赢网络科技有限公司
 *
 * @author zhanggl10620@hundsun.com
 * @date 2018.06.05 9:16
 */
@Configuration
public class WebConfiguration extends WebMvcConfigurationSupport {

    /**
     * cookie区域解析器
     *
     * @return
     */
    @Bean
    public LocaleResolver localeResolver() {
        CookieLocaleResolver slr = new CookieLocaleResolver();
        //设置默认区域,
        slr.setDefaultLocale(Locale.SIMPLIFIED_CHINESE);
        slr.setCookieMaxAge(3600);//设置cookie有效期.
        return slr;
    }

    @Bean
    public LocaleChangeInterceptor localeChangeInterceptor() {
        LocaleChangeInterceptor lci = new LocaleChangeInterceptor();
        // 设置请求地址的参数,默认为:locale
        lci.setParamName(LocaleChangeInterceptor.DEFAULT_PARAM_NAME);

        return lci;
    }

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

application.yml文件配置

spring:
  messages:
    basename: i18n/messages
    encoding: UTF-8
    cache-seconds: 3600

第六步:MessageSource类可以获取messages的内容。

@Component
public class LocaleMessageSourceUtil {
    @Resource
    private MessageSource messageSource;

    public String getMessage(String code) {
        return getMessage(code, null);
    }

    /**
     *
     * @param code :对应messages配置的key.
     * @param args : 数组参数.
     * @return
     */
    public String getMessage(String code, Object[] args){
        return getMessage(code, args, "");
    }

    /**
     *
     * @param code :对应messages配置的key.
     * @param args : 数组参数.
     * @param defaultMessage : 没有设置key的时候的默认值.
     * @return
     */
    public String getMessage(String code,Object[] args,String defaultMessage){
        //这里使用比较方便的方法,不依赖request.
        Locale locale = LocaleContextHolder.getLocale();
        return messageSource.getMessage(code, args, defaultMessage, locale);
    }
    
}

第七步:前端调用HelloController.java

@Controller
public class HelloController {


    @Autowired
    private LocaleMessageSourceUtil messageSourceUtil;

    @RequestMapping("/hello")
    public String hello() {
        String welcome = messageSourceUtil.getMessage("welcome");
        System.out.println(welcome);
        return "hello";
    }
}

第八步:http调用测试

image.png

参数locale= en_US,返回英文
参数locale=zh_CN ,返回简体中文

相关文章

网友评论

      本文标题:Spring boot 1.5.9 实现国际化

      本文链接:https://www.haomeiwen.com/subject/cbabsftx.html