spring国际化支持

来源:互联网 发布:飞鹰网络电视直播tv版 编辑:程序博客网 时间:2024/05/14 21:34

       Spring对国际化的支持是beanFactory的子接口ApplicationContext所增加的新功能,主要是因为ApplicationContext接口继承了MessageSource接口,MessageSource接口的getMessage(String code,Object[] obj,Locale locale)方法用于对国际化的支持。

      在Spring对国际化的支持中我们需要在应用上下文中配置:

    <bean  id="messageSource" class="org.springframework.context.support.RegourceBundleMessageSource">

          <property name="basenames">

                  <list>

                           <value>message</value>//表示我们资源文件的basename为message,资源文件的默认路径是在src路径下,  如果我们想把资源文件放在test下,那么<value>test/message</value>

                   </list>

        </property>

    </bean>

       建立资源文件message_zh_CN.properties     hello=你好{0}

                           message_en_US.properties      hello=hello{0}


     我们就可以通过ApplicationContext ac=new ClassPathXmlApplicationContext(.....);

                  String hello=ac.getMessage("hello",new String[]{"hi"},Locale.getDefault(Category.FORMAT));

           就可以获取该语言环境下的hello的值



           

2 0
原创粉丝点击