问题
I have a converter which reader the properties from .properties file using ResourceBundleMessageSource
for multiple locales e.g en_US,fr_FR,cs_CZ
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>
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());
}
String value = messageSource.getMessage(tplPropObj.getProperty(key), new Object[] { }, currentLocale);
Above particular line reads the properties from file having cs_CZ local (File Name
beneficiary_cs_CZ.properties
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
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.
-
Key : Beneficiary.Name - Label : lbl.beneficiary.name - Locale : cs_CZ Value : Z technických důvodů zavÅ?eno.
Key : Beneficiary.Number - Label : lbl.beneficiary.number - Locale : cs_CZ - Value : 123456
Key : Beneficiary.Owner - Label : lbl.beneficiary.owner - Locale : cs_CZ - Value : Mã Ngư�i Ký Phát
I don' understand why this is happening if I am reading the values from MessageResouce
using locale.
Any help ..Appriciated.
回答1:
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)
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>
来源:https://stackoverflow.com/questions/34264371/spring-resourcebundlemessagesource-encoding-for-czech-messageresource-getmessage