2

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.

M. Deinum
  • 115,695
  • 22
  • 220
  • 224
Sam....
  • 165
  • 2
  • 19
  • Property files are by default and standard read in ascii encoding. Instead of using UTF-8 save as a regular file as use encoded characters in your file instead. – M. Deinum Dec 14 '15 at 11:38
  • If I save the Czech characters in a file and save as Ascii then it changes the meaning of the statement. – Sam.... Dec 14 '15 at 12:36
  • Read again... You shouldn't put the Czech characters in but there encoded form ie \u2349 instead of the actual output. Properties files aren't read in UTF-8 so storing them that way isn't going to work. See http://stackoverflow.com/questions/4659929/how-to-use-utf-8-in-resource-properties-with-resourcebundle – M. Deinum Dec 14 '15 at 12:37
  • I already tried with the same.. but its giving different out put.. – Sam.... Dec 14 '15 at 13:11
  • Figure out that part. Your properties files won't be read in UTF-8 (make sure you aren't storing them as such anymore and have them converted properly). If the output is different there is something else in your proces wrong. – M. Deinum Dec 14 '15 at 13:17

1 Answers1

3

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>
Ralph
  • 118,862
  • 56
  • 287
  • 383
  • 2
    I would recommend using UTF-8, because the default character encoding has changed in Java 9. See https://docs.oracle.com/javase/9/intl/internationalization-enhancements-jdk-9.htm – PowerStat Feb 22 '19 at 08:05