问题描述
我有一个转换器,该转换器使用ResourceBundleMessageSource
从 .properties 文件读取多个语言环境的属性,例如en_US,fr_FR,cs_CZ
I have a converter which reader the properties from .properties file using ResourceBundleMessageSource
for multiple locales e.g en_US,fr_FR,cs_CZ
下面是读取属性的xml.
Below is the xml to read properties.
<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>
以下是在Java中使用File API
读取和写入javascript文件的代码.
And below is the code to read and write the javascript file using File API
in java.
String localeAr[]= lang.split("_");
Locale currentLocale = new Locale(localeAr[0].trim(),localeAr[1].trim());
file = new File(outputPath +file.separator+ jsFilename + "_" + lang + ".js");
System.out.println(file.getAbsolutePath());
System.out.println();
file.createNewFile();
buffer.append(disclaimerDetails);
buffer.append("var " + var + " = \n");
buffer.append("{ \n");
Iterator iterator = tplPropObj.keySet().iterator();
while (iterator.hasNext())
{
String key = (String) iterator.next();
System.out.println("Key : "+key+" - Label : "+tplPropObj.getProperty(key)+" - Locale : "+currentLocale);
String value = messageSource.getMessage(tplPropObj.getProperty(key), new Object[] { }, currentLocale);
System.out.println( "- Value : "+value);
buffer.append("\t" + "\"" + key + "\": ");
buffer.append("\"" + value + "\"");
if (iterator.hasNext())
{
buffer.append(",");
}
buffer.append("\n");
}
buffer.append("} ");
if(localeAr[1].equalsIgnoreCase("PL") || localeAr[1].equalsIgnoreCase("CZ"))
{
Writer out = new BufferedWriter(new OutputStreamWriter(new FileOutputStream(file), "UTF-8"));
out.write(buffer.toString());
out.flush();
out.close();
buffer.delete(0, buffer.length());
}
else
{
System.setProperty("file.encoding", "ISO-8859-1");
BufferedWriter bufferedWriter = new BufferedWriter(new FileWriter(file));
bufferedWriter.write(buffer.toString());
bufferedWriter.flush();
bufferedWriter.close();
buffer.delete(0, buffer.length());
}
在上面的特定行中,从具有cs_CZ本地文件的文件中读取属性(文件名
Above particular line reads the properties from file having cs_CZ local (File Name
下面是beneficiary_cs_CZ.properties文件的内容.这些内容使用 UTF-8编码用于czech_language
Below are the contents of beneficiary_cs_CZ.properties file..These are saved in STS using UTF-8 encoding For czech_language
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
对象生成的值.
Up to this is oK. But when I read these values from message Resource objects it returns different values Below are the values generated from MessageSource
object.
-
密钥:收款人编号-标签:lbl.beneficiary.number-区域设置:cs_CZ -值:123456
Key : Beneficiary.Number - Label : lbl.beneficiary.number - Locale : cs_CZ - Value : 123456
密钥:Beneficiary.Owner-标签:lbl.beneficiary.owner-区域设置:cs_CZ -值:MãNgÆ°á»?iKýPhát
Key : Beneficiary.Owner - Label : lbl.beneficiary.owner - Locale : cs_CZ - Value : Mã Ngư�i Ký Phát
我不明白为什么我使用语言环境从MessageResouce
中读取值时会发生这种情况.
I don' understand why this is happening if I am reading the values from MessageResouce
using locale.
任何帮助..已申请.
推荐答案
M. Deinum是正确的,您不应对Java属性文件使用UTF-8文件编码. (这在Java9中已更改,请参阅PowerStat的注释.)相反,您应该对该文件中的utf8字符使用转义序列. ( https://stackoverflow.com/a/4660058/280244 )
M. Deinum is right, you should not use UTF-8 file encoding for Java property files. (This has changed in Java9, see PowerStat`s comment.) Instead you should use escape sequences for utf8 charachters within that file. (https://stackoverflow.com/a/4660058/280244)
但是(我不建议您这样做)您可以在Spring中使用UTF8编码的消息文件.关键是您需要配置MessageResource
But (I do not recommend this) you can use UTF8 encodes message files with Spring. The key is that you need to configure the MessageResource
<bean id="messageSource" class="org.springframework.context.support.ReloadableResourceBundleMessageSource">
<property name="defaultEncoding" value="UTF-8"/>
<property name="basenames">...</property>
</bean>
这篇关于用于捷克语MessageResource.getMessage()的Spring ResourceBundleMessageSource编码的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!