1

Please tell me how to solve this problem:

Locale locale = new Locale(language);
        ResourceBundle messages = ResourceBundle.getBundle("i18n.messages", locale, utf8Control);
        try {
            String message = new String(messages.getString(key).getBytes("ISO-8859-1"), "UTF-8");
            pageContext.getOut().write(message);
        } catch (IOException e) {
            e.printStackTrace();
        }

I'm trying to implement localization, I get the text from the created messages file, the problem is that instead of the necessary characters, it outputs "?????? ??????? ????" Googled, the problem seems to be with encodings, I tried to do it like this:

String message = new String(messages.getString(key).getBytes("ISO-8859-1"), "UTF-8")

And also created utf8Control:

public class Utf8Control extends ResourceBundle.Control {

    @Override
    public ResourceBundle newBundle(String baseName, Locale locale, String format, ClassLoader loader, boolean reload)
            throws IllegalAccessException, InstantiationException, IOException {

        String bundleName = toBundleName(baseName, locale);
        String resourceName = toResourceName(bundleName, "properties");
        ResourceBundle bundle = null;
        InputStream stream = null;
        if (reload) {
            URL url = loader.getResource(resourceName);
            if (url != null) {
                URLConnection connection = url.openConnection();
                if (connection != null) {
                    connection.setUseCaches(false);
                    stream = connection.getInputStream();
                }
            }
        } else {
            stream = loader.getResourceAsStream(resourceName);
        }
        if (stream != null) {
            try {
                bundle = new PropertyResourceBundle(new InputStreamReader(stream, "UTF-8"));
            } finally {
                stream.close();
            }
        }
        return bundle;
    }

}

Nothing has happened... JSP has "UTF-8" encoding, files - default "ISO-8859-1"

Nico_99
  • 29
  • 2
  • 7
  • 1
    ISO8859-1 is the default encoding for properties files. So, if they're encoded using ISO8859-1, all you need is `ResourceBundle messages = ResourceBundle.getBundle("i18n.messages", locale); String message = messages.getString(key);`. Decoding using UTF8 a strig that has been encoded using ISO8859-1 doesn't make any sense. – JB Nizet Jan 27 '18 at 07:44
  • I get the impression you don't quite understand how `ResourceBundle` mechanism works in java. Have you seen the Internationalization trail of the _Java Tutorial_ ? https://docs.oracle.com/javase/tutorial/i18n/TOC.html – Abra Jan 27 '18 at 07:48
  • But what do I need to do, if I need to use Russian language at my web application and when I use ResourceBundle messages = ResourceBundle.getBundle("i18n.messages", locale); String message = messages.getString(key); I get unreadeble sentence ? – Nico_99 Jan 27 '18 at 08:05
  • Read https://stackoverflow.com/questions/4659929/how-to-use-utf-8-in-resource-properties-with-resourcebundle – JB Nizet Jan 27 '18 at 08:15
  • I have read this post and overrided ResourceBundle.Control, but it didn`t help me – Nico_99 Jan 27 '18 at 12:18
  • String message = messages.getString(key); byte[] bytes = message.getBytes("ISO-8859-1"); pageContext.getOut().write(new String(bytes, "UTF8")); // I tried to do this, as a result I got the other unreadable symbols – Nico_99 Jan 27 '18 at 12:20
  • @Nico_99 Are you sure your file is encoded in ISO_8859_1 as you claim? Have you run native2ascii as explained in the post, and then simply used the code in my first comment? – JB Nizet Jan 27 '18 at 12:37
  • I understood that my file is encoded in windows-1251, because I got this text to web page "Ôîðìà óïðàâëåíèÿ äëÿ àäìèíà" and when formated online it to windows-1252 i got right text. But how to fix it in code? – Nico_99 Jan 28 '18 at 08:39
  • Be sure to check your locale matches the property file you are intending to target – Janac Meena Aug 14 '20 at 15:29

0 Answers0