minggen / wmgblog

个人博客
1 stars 0 forks source link

国际化 #8

Open minggen opened 5 years ago

minggen commented 5 years ago

国际化

1

<!-- 国际化资源文件 -->
<bean id="messageSource" class="org.springframework.context.support.ReloadableResourceBundleMessageSource">
    <!-- 表示多语言配置文件在根路径下,以language开头的文件-->
    <property name="basename" value="classpath:language/msg"/>
    <property name="useCodeAsDefaultMessage" value="true"/>
            <property name="defaultEncoding" value="UTF-8"/>
</bean>

在resource下新建language文件夹 
新建 msg_en_US.properties
key=English
    a={0} hello 
新建 msg_zh_CN.properties
key=中文
    a={0} 你好

2

 <!-- 配置CookieLocaleResolver用于将Locale对象存储于Cookie中供后续使用 -->
<bean id="localeResolver" class="org.springframework.web.servlet.i18n.CookieLocaleResolver">
    <property name="cookieName" value="lang"/>
</bean>

3

配置拦截器,拦截请求获取lang(en_US&zh_CN等),使用localeResolver存储
<mvc:interceptors>
       <bean id="localeChangeInterceptor" class="org.springframework.web.servlet.i18n.LocaleChangeInterceptor">
        <property name="paramName" value="lang"/>
    </bean>
</mvc:interceptors>

4

2 3 步骤 可以自己灵活实现,根据cookice 或者session 修改LocalContextHolder中local即可

5

messageSource.getMessage("a", new String[]{ "你好" }, local)
minggen commented 5 years ago

参考 https://hahalzb.iteye.com/blog/266834

https://blog.csdn.net/posonrick/article/details/45333283