3
votes

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;
            }

        ....

        }

2
The surprising thing is your claim that is has worked before. Resource bundles defined via property files never supported UTF-8. They are defined to be iso-latin-1 and require all other non-latin characters to be written as \uxxxx. They only way to workaround this is by using custom loading code but you would know if you used custom loading code and show us that code, wouldn’t you?Holger

2 Answers

4
votes

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

1
votes

open up eclipse, go to menu "project" ->properties->Resource and set encoding to utf-8 or "window"->preferences->general->workspace and set encoding there