spring国际化, 使用jsp页面的就不用说了, 配置还是一样, 在spring-mvc.xml配置文件中加入如下代码:

    <bean id="messageSource" class="org.springframework.context.support.ResourceBundleMessageSource">
        <!-- 国际化信息所在的文件名 -->                     
        <property name="basename" value="messages/message" />   
        <!-- 如果在国际化资源文件中找不到对应代码的信息,就用这个代码作为名称  -->               
        <property name="useCodeAsDefaultMessage" value="true" />           
    </bean>

    <bean id="localeResolver" class="org.springframework.web.servlet.i18n.SessionLocaleResolver"></bean>

如果需要用于返回的json数据中:

package cn.iqoo.api.controller;

import java.util.HashMap;
import java.util.Locale;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.MessageSourceResolvable;
import org.springframework.context.annotation.Scope;
import org.springframework.ui.Model;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.servlet.i18n.SessionLocaleResolver;
import org.springframework.web.servlet.support.RequestContext;

import cn.iqoo.api.constant.Constant;
import cn.iqoo.entity.UserInfo;

@RestController
@Scope("prototype")
@RequestMapping("/system")
public class SystemController {
    
    @Autowired 
    SessionLocaleResolver resolver;
    
    @RequestMapping(value = "/language", method = RequestMethod.GET, produces = Constant.JSON_UTF8)
    public Map index(HttpServletRequest request, HttpServletResponse response, String lang) {
        
        Map<String, Object> map = new HashMap<String, Object>();
        if(!StringUtils.isEmpty(lang)){
            lang = lang.toLowerCase();
            if(lang.equals("en")){
                resolver.setLocale(request, response, Locale.ENGLISH );
            }else{
                resolver.setLocale(request, response, Locale.CHINA );
            }
        }
        
        RequestContext requestContext = new RequestContext(request);
        String something = requestContext.getMessage("something");
       
        map.put("something", something);
        
        return map;
    }

}

 基于拦截器的国际化配置:

    <mvc:interceptors>
        <!-- 国际化操作拦截器 如果采用基于(请求/Session/Cookie)则必需配置 -->
        <bean class="org.springframework.web.servlet.i18n.LocaleChangeInterceptor" />
        <mvc:interceptor>
            <mvc:mapping path="/**/*" />
            <bean class="cn.iqoo.api.interceptor.LocaleInterceptor"></bean>
        </mvc:interceptor>
    </mvc:interceptors>
public class LocaleInterceptor implements HandlerInterceptor {

    @Override
    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler)
            throws Exception {

        String lang = request.getHeader("Accept-Language");
        Locale locale = new Locale("zh", "CN");
        request.getSession().setAttribute(SessionLocaleResolver.LOCALE_SESSION_ATTRIBUTE_NAME, locale);

        return true;
    }
}

 

posted on 2016-10-16 17:56  老汪汪  阅读(278)  评论(0编辑  收藏  举报