springboot国际化配置

来源:互联网 发布:软件测试职位有哪些 编辑:程序博客网 时间:2024/06/11 01:46

messages_zh_cn.properties中文配置文件

messages_en.properties英文配置文件

messages.properties默认配置文件

对于中文的属性文件,我们编写好后,应该使用jdk提供的native2ascii命令把文件转换为unicode编码的文件。命令的使用方式如下: 
native2ascii  源文件.properties  目标文件.properties (有时间报:java.lang.Exception: 无法读取messages.)1.对下文件名保持正确;2.换到E盘或D盘根目录,cmd到根目录执行就好了

为了防止出现中文乱码:用utf-8对messages.properties进行转码,转码后保存到messages_zh_cn.properties文件

native2ascii -encoding UTF-8 messages.properties messages_zh_cn.properties

建立取message方法

 * Created by 飞尘 on 2017/3/20. */@Configurationpublic class InternationalConfig {    @Value(value = "${spring.messages.basename}")    private String basename;    @Bean(name = "messageSource")    public ResourceBundleMessageSource getMessageResource() {        ResourceBundleMessageSource messageSource = new ResourceBundleMessageSource();        messageSource.setBasename(basename);        return messageSource;    }}

/** * Created by 飞尘 on 2017/3/20. */@Componentpublic class MessageUtil {    @Autowired    private MessageSource messageSource;    /**     * 根据code取message     * @param code      * @return     */    public String getMessage(String code) {        return this.getMessage(code,null);    }    /**     * 根据code取message     * @param code      * @param param 参数     * @return     */    public String getMessage(String code, String[] param) {        return messageSource.getMessage(code, param, LocaleContextHolder.getLocale());    }}

reset.greeting=Dear {0} {1}
可以传多参数,具体还要自己多试验



0 0