หน้าเว็บ

วันพฤหัสบดีที่ 11 เมษายน พ.ศ. 2556

set session locale ผ่าน controller spring mvc : java

dispatcher-servlet.xml
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns:p="http://www.springframework.org/schema/p"
       xmlns="http://www.springframework.org/schema/beans"
       xmlns:context="http://www.springframework.org/schema/context"
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xmlns:mvc="http://www.springframework.org/schema/mvc"
       xsi:schemaLocation="
        http://www.springframework.org/schema/beans     
        http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
        http://www.springframework.org/schema/context 
        http://www.springframework.org/schema/context/spring-context-3.0.xsd
       http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-3.1.xsd">
    
    <context:component-scan base-package="com.blogspot.na5cent" />
    <mvc:annotation-driven/>
    <mvc:default-servlet-handler/>
    <mvc:resources mapping="/static/**" location="/resources/"/> 
    
    <bean
        class="org.springframework.web.servlet.view.InternalResourceViewResolver">
        <property name="prefix">
            <value>/web/</value>
        </property>
        <property name="suffix">
            <value>.jsp</value>
        </property>
    </bean>
    
    
    <!-- internationalization locale -->
    <bean id="messageSource" class="org.springframework.context.support.ResourceBundleMessageSource"
          p:basenames="languages/lang" />

    <!-- Declare the Interceptor -->
    <mvc:interceptors>    
        <bean class="org.springframework.web.servlet.i18n.LocaleChangeInterceptor" p:paramName="language" />
    </mvc:interceptors>

    <!-- Declare the Resolver -->
    <bean id="localeResolver" class="org.springframework.web.servlet.i18n.SessionLocaleResolver" p:defaultLocale="en"/>
    <!-- internationalization locale -->
</beans>
set default locale
controller
@Controller
public class FirstController {

    private static final Locale THAI_LOCALE = new Locale("th", "TH");
    @Autowired
    private SessionLocaleResolver sessionLocaleResolver;

    @RequestMapping(value = "/", method = RequestMethod.GET)
    public ModelAndView getIndex() {
        ModelAndView mav = new ModelAndView("index");

        sessionLocaleResolver.setDefaultLocale(THAI_LOCALE); //****
        return mav;
    }
}
set dynamic locale
controller
@Controller
public class LocaleController implements Serializable {

    private static final Locale THAI_LOCALE = new Locale("th", "TH");
    private static final Logger LOG = LoggerFactory.getLogger(LocaleController.class);
    @Autowired
    private SessionLocaleResolver sessionLocaleResolver;

    @RequestMapping(value = "/language/{keyCode}", method = RequestMethod.GET)
    @ResponseBody
    public void getIndex(@PathVariable(value = "keyCode") String keyCode, HttpServletRequest request, HttpServletResponse response) {

        Locale locale;
        if (keyCode.equals("en")) {
            locale = Locale.ENGLISH;
        } else {
            locale = THAI_LOCALE;
        }
        
        LOG.debug("keyCode => {}", keyCode);
        LOG.debug("locale => {}", locale);

        sessionLocaleResolver.setLocale(request, response, locale); //***
    }
}

ไม่มีความคิดเห็น:

แสดงความคิดเห็น