tags:

views:

36

answers:

2

I am trying to read from a properties file which have keys in English & values in greek.My code is like this:

public class I18NSample {

   static public void main(String[] args) {

      String language;
      String country;

      if (args.length != 2) {
          language = new String("el");
          country = new String("GR");
      } else {
          language = new String(args[0]);
          country = new String(args[1]);
      }

      Locale currentLocale;
      ResourceBundle messages;

      currentLocale = new Locale(language, country);

      messages =
        ResourceBundle.getBundle("MessagesBundle",currentLocale, new CustomClassLoader("E:\\properties"));

      System.out.println(messages.getString("greetings"));
      System.out.println(messages.getString("inquiry"));
      System.out.println(messages.getString("farewell"));
   }
}

import java.io.File;
import java.net.MalformedURLException;
import java.net.URL;


public class CustomClassLoader extends ClassLoader {

    private String path;



    public CustomClassLoader(String path) {
        super();
        this.path = path;
    }



    @Override
    protected URL findResource(String name) {
        File f = new File(path + File.separator + name);
        try {
            return f.toURL();
        } catch (MalformedURLException e) {
            e.printStackTrace();
        }
        return super.findResource(name);
    }

}

MessagesBundle_el_GR.properties greetings=ρήμ. χαιρετώ farewell=επιφ. αντίο

inquiry=τι κάνεισ, τι κάνετε

I am compiling like this javac -encoding UTF8 CustomClassLoader.java javac -encoding UTF8 I18Sample.java

When I run this I get garbled output.If the properies file is in English,French or German it works fine. Please help. Regards, Subhendu

A: 

As per the documentation, properties files appear to be restricted to ISO 8859-1. They do however support Unicode escapes, and the linked documentation has a tool to produce said escapes automatically.

The XML loader allows for UTF-8 encoded XML.

The javac options only apply to string literals in the .java file, and do nothing for the runtime behavior of the program.

Yann Ramin
May be trying to print to console is bad idea as console is not capable of printing anything other than latin1.I will try to print the output to a file.----Subhendu
Subhendu Mahanta
@Subhendu: Very possible.
Yann Ramin
A: 

Thanks theatrus.

I tried to directly read from properties file like this :

FileInputStream fis = new FileInputStream("E:\\properties\\MessagesBundle_el_GR.properties");
        DataInputStream dis = new DataInputStream(fis);
        BufferedReader br = new BufferedReader(new InputStreamReader(dis, "ISO-8859-7"));
        String strLine;
        while((strLine = br.readLine()) != null){
            System.out.println(strLine);
        }

        dis.close();

It did not help.

We can put the same information in database & read from there.Though in the terminal of the machine where mysql database is installed we get the message displayed in Greek properly when we try to read the same from java code it becomes garbled.Will the javac -encoding UTF8 be of help for reading the message from database?

The native2ascii tool will not be of much help as user of our system will not agree to take all that pain.The user is ready to use database( as we will be providing a GUI for entry into database) or properties file.

Regards,


Subhendu

Subhendu Mahanta