zoukankan      html  css  js  c++  java
  • Java国际化(i18n)

    Java国际化(i18n)

    最近在做一个网站国际化的功能。用Java做开发,使用spring+velocity.

    Java提供了对i18n的支持,spring对其做了集成,可以很方便的配置。主要思想就是根据语言来读取不同的配置文件,来显示对应的文本。主要步骤如下:

    1. 用两个properties文件来保存“符号”到对应语言的映射。如messages_en.properties和messages_zh.properties, 将其放到工程的classPath下

    #messages_en.properties
    title=service introduction

    #messages_cn.properties

    title=u670du52a1u8bf4u660e

     注意中文要使用unicode编码

    2. 配置spring的xml文件:

        spring提供了多种国际化的支持方式,如基于浏览器,session和cookie等。项目中使用cookie方式

      

    <bean id="localeResolver" class="org.springframework.web.servlet.i18n.CookieLocaleResolver">
           <property name="cookieMaxAge" value="604800"/>
           <property name="defaultLocale" value="zh_CN"/>
           <property name="cookieName" value="lang"/>
         </bean>
    

      声明1中的资源文件

    <bean id="messageSource" class="org.springframework.context.support.ResourceBundleMessageSource">
            <property name="basename" value="messages"/>
            <property name="useCodeAsDefaultMessage" value="true" />
        </bean>
    

      使用拦截器来处理请求,让对应的页面国际化

    <bean id="localeChangeInterceptor" class="org.springframework.web.servlet.i18n.LocaleChangeInterceptor" />
    
    ...
    <property name="interceptors" ref="localeChangeInterceptor"></property>
    ...

      或者

    <mvc:interceptors>
    	<bean class="org.springframework.web.servlet.i18n.LocaleChangeInterceptor" />
    	<mvc:interceptor>
    			...
    	</mvc:interceptor>
    </mvc:interceptors>
    

     

    然后在vm页面中调用占位符就可以了

    #springMessage("title")

    3. 另外可以写一个controller来切换语言

    @RequestMapping("/lang")
    public String lang(
        HttpServletRequest request, HttpServletResponse response) throws   Exception {
        String langType = request.getParameter("langType");
    
        if ("en".equals(langType)) {
            cookieLocaleResolver.setLocale(request, response, Locale.ENGLISH);
        } else {
            cookieLocaleResolver.setLocale(request, response, Locale.CHINA);
        }
        return null;
    }

    在Java国际化(i18n)中,

    vm页面显示内容需要使用 #springMessage("title")

    实际运行时发现页面输出$springMacroRequestContext.getMessage($code)。看了一下源代码,#springMessage是一个宏,在spring.vm中定义为

    #macro( springMessage $code )$springMacroRequestContext.getMessage($code)#end

    原因是因为未找到$springMacroRequestContext...

    解决方法:在我们定义视图resolver中加入对spring宏的支持

    <bean id="viewResolver" class="org.springframework.web.servlet.view.velocity.VelocityViewResolver">
            ...
            <property name="exposeSpringMacroHelpers" value="true"/>
    </bean>

      

  • 相关阅读:
    python的xpinyin模块:汉字转拼音
    中文报错SyntaxError: Non-UTF-8 code starting with 'xe6' in file
    "底层逻辑”是什么意思?
    【Golang】关于Go中logrus的用法
    【Golang】 关于Go语言中的锁
    【Golang】Go 通过结构(struct) 实现接口(interface)
    【Golang】Go语言之log的使用
    【Golang】Go中时间(time)的用法以及gorm处理时间戳
    【Golang】Go中三个点(...)用法
    一文彻底掌握Apache Hudi异步Clustering部署
  • 原文地址:https://www.cnblogs.com/dreamysmurf/p/6016776.html
Copyright © 2011-2022 走看看