by 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