Store unicode characters in Bundle

2

I have a project of internationalized and versioned, but it seems that when opening it the accented characters of the resource bundle (the .properties file) of the Spanish language are seen incorrectly.

I already tried to change the coding, but it seems not to be the best solution.

How can I achieve a consistent internationalization with my collaborators that have a different coding than mine?

    
asked by Ruslan López 17.12.2015 в 09:39
source

3 answers

2

The cause of your problem is that the .properties files must be encoded in ISO 8859-1 :

  

The load (Reader) / store (Writer, String) methods load and store properties from and to a character-based flow in a simple, line-oriented format specified later. The pair of load (InputStream) / store (OutputStream, String) methods work in the same way as the load (Reader) / store (Writer, String) pair, except that the input / output stream is encoded by the ISO 8859-1 character encoding . Characters that can not be represented directly in this encoding can be written using Unicode escapes as defined in section 3.3 of The Java ™ Language Specification; only a single 'u' character is allowed in an escape sequence. The native2ascii tool can be used to convert property files to and from other character encodings.

Most frameworks read them like this. I do not know if there are any that read them in XML, which can be UTF-8.

The solution can be to convert them to ISO 8859-1.

I forgot to tell you. To add characters that can not be encoded in ISO 8859-1 (that is, most of the characters defined in Unicode) you can use the \uNNNN syntax. More information here .

    
answered by 18.12.2015 / 21:43
source
1

I have had the same problem opening files that contain accented characters, what is done is to use the UTF-8 code when opening it:

bundle = new PropertyResourceBundle(new InputStreamReader(stream, "UTF-8"));
    
answered by 17.12.2015 в 17:11
-1

At the moment I solved it with a conversion to each string obtained, I'm sure it's not the best solution (especially because of the double definition and the extensive use of PermGen to compensate for the extra time).

private static String cadenaEnUTF8(final String nombre) {
        String internacionalizado = nombre;
        try {
            internacionalizado = new String(
                    ResourceBundle.getBundle(BUNDLE_PATH)
                    .getString(nombre).getBytes(),
                    "UTF-8"
            );
        } catch (UnsupportedEncodingException ex) {
            Logger.getLogger(Main.class.getName()).log(Level.SEVERE, null, ex);
        }
        return internacionalizado;
    }
    
answered by 18.12.2015 в 13:47