Spring ResourceBundleMessageSource捷克MessageResource.getMessage()的编码

Spring ResourceBundleMessageSource捷克MessageResource.getMessage()的编码,spring,properties,character-encoding,locale,resourcebundle,Spring,Properties,Character Encoding,Locale,Resourcebundle,我有一个转换器,它使用ResourceBundleMessageSource读取多个地区(例如en_-US、fr_-fr、cs_-CZ)的.properties文件中的属性 下面是读取属性的xml <bean id="messageSource" class="org.springframework.context.support.ResourceBundleMessageSource"> <property name="basenames"> &

我有一个转换器,它使用
ResourceBundleMessageSource
读取多个地区(例如en_-US、fr_-fr、cs_-CZ)的.properties文件中的属性

下面是读取属性的xml

<bean id="messageSource"
    class="org.springframework.context.support.ResourceBundleMessageSource">
    <property name="basenames">
    <list>
            <value>lang/beneficiaryproperty/beneficiary</value>
            <value>lang/labelsbundlesproperty/labelsbundle</value>
    </list>
    </property>
</bean>
字符串值=messageSource.getMessage(tplPropObj.getProperty(键), 新对象[]{},currentLocale)

上面的特定行从具有cs_CZ local(文件名)的文件中读取属性

受益人(c)(CZ)财产

以下是受益人_cs_CZ.properties文件的内容。这些文件使用捷克语的UTF-8编码保存在STS中

lbl.beneficiary.name=Z technických důvodů zavřeno.
lbl.beneficiary.number=123456 
lbl.beneficiary.loc=Prosím vás, kde je
divadlo lbl.beneficiary.owner=Mã Người Ký Phát
但当我从消息资源对象中读取这些值时,它会返回不同的值 下面是从
MessageSource
对象生成的值

-

关键字:受益人。名称-标签:lbl.受益人。名称-区域设置:cs_CZ 价值:Z technickýChdÅvodÅzavÅeno

关键字:受益人。编号-标签:lbl.受益人。编号-区域设置:cs_CZ -价值:123456

关键字:受益人。所有者-标签:lbl.受益人。所有者-地区:cs_CZ -值:MÃNgÆá?i KýPhÃt

如果我使用locale从
messageresource
读取值,我不理解为什么会发生这种情况


任何帮助..已收到。

M.Deinum是正确的,您不应该对Java属性文件使用UTF-8文件编码。(这在Java9中已更改,请参阅PowerStat的注释。)相反,您应该对该文件中的utf8字符使用转义序列。()

但是(我不建议这样做)您可以使用UTF8对Spring中的消息文件进行编码。关键是您需要配置
MessageResource

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

...

默认情况下,属性文件以ascii编码读取。不要使用UTF-8另存为常规文件,而是在文件中使用编码字符。如果我将捷克字符保存在文件中并另存为ascii,则会更改语句的含义。请再次读取…不应将捷克字符放入编码形式ie\u2349而不是实际输出。属性文件不会以UTF-8格式读取,因此以这种方式存储不会起作用。请参阅,我已经尝试过使用相同的方法。但其输出不同。找出该部分。属性文件不会以UTF-8格式读取(请确保不再以这种方式存储它们,并正确转换它们)。如果输出不同,则进程中还有其他错误。我建议使用UTF-8,因为Java 9中的默认字符编码已更改。请参阅
<bean id="messageSource" class="org.springframework.context.support.ReloadableResourceBundleMessageSource">     
    <property name="defaultEncoding" value="UTF-8"/>
    <property name="basenames">...</property>
</bean>