Search code examples
javaeclipseutf-8resourcebundle

Java ResourceBundles does not read utf-8 characters correctly - after upgrading Eclipse


I have a project that runs correctly for quite a long time. Tow days ago, I updated my Eclipse version to Kepler's version and since then - my properties file are not being read correctly. Letters in Hebrew are being read like this: "××× ×©× ×שת×ש ×ס×ס××".

I though that somehow the files were ruined, so I copy-paste them to the simplest txt file and added to the project again. They are read correctly if I just simply read them, but they are not being read correctly if I continue to use the ResourceBundle implementation.

Does anyone have an idea of how to resolve this? I changed the platform and the file setting to utf-8 encoding - anywhere that I could think of...

This is the Resource code:


        public class ResourceManager {

            protected final Logger logger = Logger.getLogger(this.getClass().getName());
            public static final String FILE_PREFIX = "file::";
            private static Locale locale = null;
            private static Map resourcesTable = new HashMap();

            // Static initializer of the Local object - this currently return "iw"
            static {
                locale = new Locale(ApplicationConfiguration.getInstance().getProperty("user.language"));
            }

            /**
             * Return the Resources object according to the base name and Locale.
             */
            private static Resources getResource(String baseName) {
                Resources resource = (Resources) resourcesTable.get(baseName);
                if (resource == null) {
                    resource = new Resources(baseName, locale);
                    resourcesTable.put(baseName, resource);
                }
                return resource;
            }

        //more code...

    /* This is the problematic method - but the problem starts even before when adding the resource to the resources map */
            private static String getFormatedMessage(String baseName, String messageKey, Object... args) {
                Resources resource = getResource(baseName);
                String msg = null;
                if (args == null || args.length == 0) {
                    msg = resource.getString(messageKey, "");
                } else {
                    msg = resource.format(messageKey, args);
                }

                return msg;
            }

        ....

        }


Solution

  • Eventually I didn't create a new workspace - I wanted my code to work under any platform, "in any condition". So I resolved it by changing the code to use Properties instead of Resources. As input parameter I used a Reader with "utf-8" encoding set:

    
        ResourcePatternResolver resourceResolver = new PathMatchingResourcePatternResolver();
        Resource[] fileInJar = resourceResolver.getResources(filePath);
        Properties properties = new Properties();
        BufferedReader reader = new BufferedReader(new InputStreamReader(fileInJar[0].getInputStream(), "UTF-8"));
        properties.load(reader);
    
    

    Thanks for those who answered! I really appreciate it...

    Carmel