Search code examples
javaandroidcharacter-encodingresourcebundle

ResourceBundle usage on Android


I have an Android app which relies on some webservices. The code used by both the Android app and the server component is factored out into a separate project, from which I generate a Jar which is used by the two components.

Now, to localize that shared part of the code, I'd like to use Java's ResourceBundle (obviously, I can't use Android ressources on the server). However, this does not yet work as intended: German umlauts (i.e., the ä - I haven't tried other umlauts yet) appear as (two different flavors of) question marks.

I've so far tried two different approaches:

public static String getString(String key, Locale locale) {
    return ResourceBundle.getBundle(STRINGS, locale).getString(key);
}

and

public static String getString(String key, Locale locale) {
    try {
        String isoString = ResourceBundle.getBundle(STRINGS, locale).getString(key);
        return new String(isoString.getBytes(ENCODING_ISO8859_1), ENCODING_UTF_8);
    } catch (UnsupportedEncodingException e) {
         throw new RuntimeException(e);
    }
}

The former results in question marks within a diamond, the latter in plain question marks.

Eclipse tells me that the properties files are encoded in ISO8859-1 (foo.properties -> Properties -> Resource says "determined from content type: ISO8859-1").

So my question is how to use properties files on Android in a scenario as described above.


Solution

  • I ended up using Java's Properties class - the code below works just fine on both Android and my server component. It is located inside a little utility class which takes care of choosing and caching the according language's .properties file.

    String propertiesFile = getPropertiesFile(locale);
    InputStream inputStream = Localization.class.getClassLoader().getResourceAsStream(propertiesFile);
    Reader reader = new InputStreamReader(inputStream, CommonUtils.ENCODING_ISO8859_1);
    Properties properties = new Properties();
    properties.load(reader);