Properties Encoding

SWT Designer allows you to create the views, editors, perspectives, pref pages, composites, etc. that comprise Eclipse SWT & RCP applications and plug-ins.

Moderators: Konstantin.Scheglov, gnebling, Alexander.Mitin, jwren, Eric Clayberg

Properties Encoding

Postby markokrajnc » Thu Jul 09, 2009 2:28 am

Hi!

I am using GWT and SWT for my application. GWT encodes translation texts in UTF-8 encoding, SWT uses Java property encoding which is ISO-8859-1. I would like to share messages.properties between these GWT and SWT, but this is not possible because of different encodings.

The problem would be solved if SWT Designer and GWT Designer would honor "Text File Encoding" information in Eclipse. In this case I would be able to set the encoding to UTF-8.

(I already use my custom Messages class for SWT to load properties with UTF-8 encoding, but SWT Designer is still reading/writing texts in ISO-8859-1 encoding.)

Would it be possible for SWT Designer and GWT Designer to use the encoding defined in "Text File Encoding"?

Marko
Marko Krajnc
Founder and Director
Cursor d.o.o., Glavni trg 19 c, SI-2000 Maribor, Slovenia
http://www.cursor.si
markokrajnc
 
Posts: 10
Joined: Tue Aug 19, 2008 10:09 pm

Re: Properties Encoding

Postby Eric Clayberg » Thu Jul 09, 2009 2:13 pm

Have you confirmed that this will actually work outside of GWT Designer and SWT Designer?

Can you send us your custom Message class?
Eric Clayberg
Software Engineering Manager
Google
http://code.google.com/webtoolkit/download.html

Author: "Eclipse Plug-ins"
http://www.qualityeclipse.com
Eric Clayberg
Moderator
 
Posts: 4503
Joined: Tue Sep 30, 2003 6:39 am
Location: Boston, MA USA

Re: Properties Encoding

Postby markokrajnc » Wed Jul 15, 2009 1:57 am

Bellow is my custom Messages class which I use from SWT part of the application and it correctly loads GWT message properties. The class doesn't cover all possible locale codes (language+country+variant), but just language which is fine for me but not for general use.

I develop everything in GWT and then manually wire strings in SWT part. Of course I can not change the labels and other texts in SWT Designer part because messages.properties would be over-written with wrong encoding. I have to do this in GWT Designer...

Another issue is with removal of controls: if I remove a control in a window will Designer also remove corresponding text from messages.properties? This is not good in my case since messages are used in more than one location in source code!

Marko
---
Code: Select all
package si.cursor.jub.cphysics.swt;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.util.HashMap;
import java.util.Locale;
import java.util.Map;
import java.util.MissingResourceException;
import java.util.Properties;

public class MessagesFromGWT {
   private static final String BUNDLE_NAME = "si.cursor.jubcp.client.AppConstants"; //$NON-NLS-1$
   private static final Map<String,Properties> cache = new HashMap<String,Properties>();

   public static String getString(String key) {
      String localeCode = Locale.getDefault().toString();

      // Get bundle from cache or load it!
      String cacheKey = BUNDLE_NAME + "/" + localeCode; //$NON-NLS-1$
      Properties properties = cache.get(cacheKey);
      if (properties == null) {
         try {
            properties = loadTranslationProperties(BUNDLE_NAME, localeCode);
         } catch (IOException e) {
            throw new RuntimeException(e);
         }
         cache.put(cacheKey, properties);
      }

      try {
         return properties.getProperty(key);
      } catch (MissingResourceException e) {
         return '!' + key + '!';
      }
   }

   private static Properties loadTranslationProperties(String bundleName, String localeCode) throws IOException {
      Properties properties = new Properties();
      String resourceName = bundleName.replace('.', '/') + ".properties";       //$NON-NLS-1$
      InputStream is = MessagesFromGWT.class.getClassLoader().getResourceAsStream(resourceName);
      if (is == null) throw new IOException("Properties file not found (" + resourceName + ")!"); //$NON-NLS-1$ //$NON-NLS-2$
      try {
         loadUTF8Properties(properties, is, resourceName);
      } finally {
         is.close();
      }
      if (!localeCode.equals("default")) { //$NON-NLS-1$
         resourceName = bundleName.replace('.', '/') + "_" + localeCode + ".properties"; //$NON-NLS-1$ //$NON-NLS-2$
         is = MessagesFromGWT.class.getClassLoader().getResourceAsStream(resourceName);
         if (is != null) {
            try {
               loadUTF8Properties(properties, is, resourceName);
            } finally {
               is.close();
            }
         }
      }
      return properties;
   }

   private static void loadUTF8Properties(Properties properties, InputStream is, String resourceName) throws IOException {
      BufferedReader reader = new BufferedReader(new InputStreamReader(is, "UTF8")); //$NON-NLS-1$
      String line = reader.readLine();
      while (line != null) {
         int index = line.indexOf('#');
         if (index >= 0) {
            line = line.substring(0, index);
         }
         if (line.trim().length() > 0) {
            index = line.indexOf('=');
            if (index < 0) throw new IOException("Wrong properties file format (" + resourceName + ")!"); //$NON-NLS-1$ //$NON-NLS-2$
            String key = line.substring(0, index).trim();
            String value = line.substring(index + 1);
            properties.put(key, value);
         }
         line = reader.readLine();
      }
   }
}
Last edited by markokrajnc on Sat Jul 18, 2009 1:22 pm, edited 1 time in total.
Marko Krajnc
Founder and Director
Cursor d.o.o., Glavni trg 19 c, SI-2000 Maribor, Slovenia
http://www.cursor.si
markokrajnc
 
Posts: 10
Joined: Tue Aug 19, 2008 10:09 pm

Re: Properties Encoding

Postby Eric Clayberg » Fri Jul 17, 2009 7:22 am

Give this a try using the latest SWT Designer v7.1 build.

If the *.properties file is encoded in UTF-8 and you are using JDK 1.6, SWT Designer will now preserve that encoding when it updates the file.
Eric Clayberg
Software Engineering Manager
Google
http://code.google.com/webtoolkit/download.html

Author: "Eclipse Plug-ins"
http://www.qualityeclipse.com
Eric Clayberg
Moderator
 
Posts: 4503
Joined: Tue Sep 30, 2003 6:39 am
Location: Boston, MA USA

Re: Properties Encoding

Postby markokrajnc » Sat Jul 18, 2009 1:19 pm

Hi Eric!

You are really great! I will try it out to check if it works for me!

You have impressive response times!

Marko
Marko Krajnc
Founder and Director
Cursor d.o.o., Glavni trg 19 c, SI-2000 Maribor, Slovenia
http://www.cursor.si
markokrajnc
 
Posts: 10
Joined: Tue Aug 19, 2008 10:09 pm

Re: Properties Encoding

Postby markokrajnc » Sat Jul 18, 2009 11:49 pm

Hi Eric!

I've tested it and it works with UTF8 within SWT Designer now!

Thank you very much again!

Marko
Marko Krajnc
Founder and Director
Cursor d.o.o., Glavni trg 19 c, SI-2000 Maribor, Slovenia
http://www.cursor.si
markokrajnc
 
Posts: 10
Joined: Tue Aug 19, 2008 10:09 pm

Re: Properties Encoding

Postby Eric Clayberg » Sun Jul 19, 2009 8:37 am

Glad we could help.
Eric Clayberg
Software Engineering Manager
Google
http://code.google.com/webtoolkit/download.html

Author: "Eclipse Plug-ins"
http://www.qualityeclipse.com
Eric Clayberg
Moderator
 
Posts: 4503
Joined: Tue Sep 30, 2003 6:39 am
Location: Boston, MA USA


Return to SWT Designer

Who is online

Users browsing this forum: Google [Bot] and 2 guests