MessageSource国际化i18n

目录

配置

MessageUtils

TestController

github


配置

LocaleConfig
package com.example.demo.config;

/**
 * Created on 2019/8/19.
 *
 * @author yangsen
 */

import org.springframework.context.MessageSource;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.support.ReloadableResourceBundleMessageSource;
import org.springframework.web.servlet.LocaleResolver;
import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;
import org.springframework.web.servlet.i18n.LocaleChangeInterceptor;
import org.springframework.web.servlet.i18n.SessionLocaleResolver;

import java.util.Locale;

/**
 * 配置国际化语言
 * @author Max.Yang
 */
@Configuration
public class LocaleConfig implements WebMvcConfigurer {

    /**
     * 默认解析器 其中locale表示默认语言
     */
    @Bean
    public LocaleResolver localeResolver() {
        SessionLocaleResolver localeResolver = new SessionLocaleResolver();
        localeResolver.setDefaultLocale(Locale.CHINESE);
        return localeResolver;
    }

    @Bean
    public MessageSource messageSource() {
        ReloadableResourceBundleMessageSource messageSource = new ReloadableResourceBundleMessageSource();
        messageSource.setBasename("classpath:static/i18n/messages");
        messageSource.setCacheSeconds(10);
        messageSource.setDefaultEncoding("GBK");
        messageSource.setUseCodeAsDefaultMessage(true);
        return messageSource;
    }
    @Bean
    public LocaleChangeInterceptor localeChangeInterceptor() {
        LocaleChangeInterceptor lci = new LocaleChangeInterceptor();
        // 参数名
        lci.setParamName("lang");
        return lci;
    }

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


}

WebMvcConfigurer 会拦截请求lang后面的请求进行映射,

messageSource.setBasename("classpath:static/i18n/messages");一般这个会配置在application.properties,可是我配置完不成功

其次是GBK编码,如果是utf-8的话会变成???

 

MessageUtils

package com.example.demo.util;

/**
 * Created on 2019/8/19.
 *
 * @author yangsen
 */

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.MessageSource;
import org.springframework.context.i18n.LocaleContextHolder;
import org.springframework.stereotype.Component;

/**
 * 国际化工具类
 * @author Max.Yang
 */
@Component
public class MessageUtils {

    @Autowired
    private MessageSource messageSource;

    /**
     * 获取单个国际化翻译值
     */
    public String get(String msgKey) {
        return messageSource.getMessage(msgKey, null, LocaleContextHolder.getLocale());
    }

}

 

后面这个可以设定Locale.US,或者Locale.CHINESE来指定映射的内容

TestController

package com.example.demo.controller;

import com.example.demo.util.MessageUtils;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;

/**
 * Created on 2019/8/19.
 *
 * @author yangsen
 */
@RestController
public class TestController {

    @Resource
    MessageUtils messageUtils;

    //http://localhost:8080/get?lang=en_US
    @GetMapping("/get")
    public String a(){
        return messageUtils.get("user.title");
    }

}

因为在默认配置的话是中文。所以其他lang请求都是映射到中文那里,如果要映射到英文的话需要请求http://localhost:8080/get?lang=en_US

github

https://github.com/dajitui/i18n-demo/tree/master

 

 

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值