Luca95
Luca95

Reputation: 129

i18n JAVA character encoding

I have a Java (Java 7) program using i18n with properties files, but I cannot insert emoticon or others "non ordinary" letters because Java prints ?. But when I use, for instance:

package res;

import java.util.ListResourceBundle;

public class Bundle_it_IT extends ListResourceBundle {

    @Override
    protected Object[][] getContents() {
        return new Object[][] {
                {"hello", "Hello, “ \ uD83D\ uDE42 \ uD83D\ uDE0A\ uD83D\ uDE00\ uD83D\ uDE01"},
                {"ourfeatures", new String[] {"Test 1", "Test2", "test 3"
        };
    }
}

Java is able to print all without question marks.

Is there a way in order to use proprieties files even for "strange symbol"? And encoding of the properties files is ASCHI?

The main class is:

package testi18nv1;

import res.Bundle_it_IT;

import java.util.Locale;
import java.util.ResourceBundle;

public class Main {
    public static void main(String[] args) {
        Locale locale_it_IT = new Locale("it", "IT");
        Locale locale_ru_RU = new Locale("ru", "RU");

        Bundle_it_IT bundle_it_IT =  new Bundle_it_IT();

        ResourceBundle resourceBundle = ResourceBundle.getBundle("res.bundle", locale_it_IT);

        System.out.println(resourceBundle.getString("hello"));
        System.out.println(bundle_it_IT.getString("hello"));
    }
}

I have tried to use Russian letter on properties files but java print question marks.

Upvotes: 0

Views: 589

Answers (1)

Joop Eggen
Joop Eggen

Reputation: 109547

Originally *.properties were in Latin-1, ISO-8859-1.

The newer java versions first try UTF-8. So you would need to update from Java 7 to a newer version. Which is not problematic.

You could also keep a properties file in UTF-8, say xxx.utf8props and convert and copy them to xxx.properties from the src to the build directory. This can be done with the java SE tool native2ascii. (Like \uD83D)

More cumbersome is to change your code and use your own loading with the class Properties.

But really Java 7 is no longer production quality.

Upvotes: 2

Related Questions