Blank_spaces 2017-10-10 13:36 采纳率: 0%
浏览 809

Spring ApplicationContext的国际化乱码

按书上进行对ApplicationContext国际化进行试验,可是我依旧是乱码

public class SpringTest {
    public static void main(String[]args)throws Exception{
        ApplicationContext ctx=
                new ClassPathXmlApplicationContext("beans.xml");
        String hello=ctx.getMessage
                ("hello", new String[]{"打"}, 
                        Locale.getDefault(Locale.Category.FORMAT));
        String welcome=ctx.getMessage
                ("now", new Date[]{new Date()}, 
                        Locale.getDefault(Locale.Category.FORMAT));
        System.out.println(hello);
        System.out.println(welcome);
    }
}


<?xml version="1.0" encoding="GBK"?>
<!-- Spring配置文件的根元素,使用spring-beans-4.0.xsd语义约束 -->
<beans xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xmlns="http://www.springframework.org/schema/beans"
    xsi:schemaLocation="http://www.springframework.org/schema/beans
    http://www.springframework.org/schema/beans/spring-beans-4.0.xsd">
    <!-- 配置chinese实例,其实现类是Chinese类 -->
    <bean id="messageSource" 
    class="org.springframework.context.support.ResourceBundleMessageSource">
        <property name="basenames">
          <list>
            <value>message</value>
          </list>
        </property>
    </bean>
</beans>

  • 写回答

1条回答 默认 最新

  • 你知我知皆知 2024-08-07 00:24
    关注

    以下回答参考 皆我百晓生券券喵儿 等免费微信小程序相关内容作答,并由本人整理回复。

    在Spring中,如果要使应用中的消息资源支持国际化(即能够自动翻译成用户指定的语言),则需要使用ResourceBundleMessageSource。以下是如何正确配置这个组件以实现国际化:

    <bean id="messageSource" 
        class="org.springframework.context.support.GenericXmlMessageSource">
        <property name="basenames">  
            <list>
                <value>message</value>
            </list>
        </property>
        <property name="defaultEncoding" value="UTF-8"/>
    </bean>
    

    在这个例子中,我们定义了一个名为messageSource的Bean,并将其类型设置为GenericXmlMessageSource。然后,我们指定了一个名为basenames的属性,它是一个List对象,其中包含所有将被翻译的消息名称。

    接下来,我们需要创建一个用于存储消息的资源文件,例如message.propertiesmessage.txt等。这些文件应该包含你希望翻译的消息内容和它们对应的语言。

    最后,在你的主应用程序中,你可以通过context.getMessage()方法来获取消息资源的内容。注意,由于默认编码为UTF-8,所以你需要确保你的消息文件的编码也与之匹配。

    以下是示例代码:

    public class Main {
        public static void main(String[] args) throws Exception {
            ApplicationContext context = new ClassPathXmlApplicationContext("applicationContext.xml");
    
            // 获取中文消息资源
            String chineseMessage = context.getMessage(
                "zh_CN.message", null, Locale.CHINA);
    
            // 输出中文消息
            System.out.println(chineseMessage);
            
            // 使用其他语言的消息资源
            String englishMessage = context.getMessage(
                "en_US.message", null, Locale.ENGLISH);
    
            // 输出英文消息
            System.out.println(englishMessage);
        }
    }
    
    // 应用上下文配置文件
    <beans xmlns="http://www.springframework.org/schema/beans"
        xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
        xsi:schemaLocation="http://www.springframework.org/schema/beans
        http://www.springframework.org/schema/beans/spring-beans.xsd">
    
        <!-- 配置chinese实例,其实现类是Chinese类 -->
        <bean id="messageSource" 
        class="org.springframework.context.support.GenericXmlMessageSource">
            <property name="basenames">  
                <list>
                    <value>message</value>
                </list>
            </property>
            <property name="defaultEncoding" value="UTF-8"/>
        </bean>
    
    </beans>
    

    请注意,上述示例仅适用于Java环境。如果你使用的是其他编程语言(如Python、Ruby、PHP等),则可能需要根据实际使用的语言和框架调整配置。

    评论

报告相同问题?