SpringBoot i18n 国际化多语言

#1、配置文件
```
spring:
  messages:
    basename: i18n/messages
    cache-second: 3600
    encoding: UTF-8
```
#注意点:springboot2.0 cache-seconds改为:cache-second
#2、在resource下新建
![image.png](https://upload-images.jianshu.io/upload_images/13498144-bff10ca1eff92c98.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240)
#3、在文件里添加内容
```
messages.properties:welcome = 欢迎
messages_zh_CN.properties:welcome = 欢迎
messages_en_US.properties:welcome= welcome
#(messages.properties默认文件,当找不到语言的配置的时候,使用该文件进行展示)。
```
#4、封装国际化工具类
```
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.MessageSource;
import org.springframework.context.i18n.LocaleContextHolder;
import org.springframework.stereotype.Component;

import java.util.Locale;

@Component
public class LocaleMessage {

    @Autowired
    private MessageSource messageSource;

    /**
     * @param code:对应文本配置的key.
     * @return 对应地区的语言消息字符串
     */
    public String getMessage(String code){
        return this.getMessage(code,new Object[]{});
    }

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

    public String getMessage(String code,String defaultMessage,Locale locale){
        return this.getMessage(code,null,defaultMessage,locale);
    }

    public String getMessage(String code,Locale locale){
        return this.getMessage(code,null,"",locale);
    }

    public String getMessage(String code,Object[] args){
        return this.getMessage(code,args,"");
    }

    public String getMessage(String code,Object[] args,Locale locale){
        return this.getMessage(code,args,"",locale);
    }

    public String getMessage(String code,Object[] args,String defaultMessage){
        Locale locale = LocaleContextHolder.getLocale();
        return this.getMessage(code,args, defaultMessage,locale);
    }

    public String getMessage(String code,Object[]args,String defaultMessage,Locale locale){
        return messageSource.getMessage(code,args, defaultMessage,locale);
    }

}

```
#5、新增controller
```
import com.stylefeng.guns.core.util.LocaleMessage;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;

@RestController
public class HelloController {

    @Resource
    private LocaleMessage localeMessage;


    @RequestMapping("/hello")
    public String hello(){
        System.out.println("1");
        String msg3 = localeMessage.getMessage("welcome");

        System.out.println(msg3);
        return msg3;


    }
}
```
#6、浏览器上测试。
 

  • 1
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值