9

I declared three locale conversions in three properties files like Strings.properties, Strings_es_ES.properties and Strings_en_GB.properties ( for US, ES and UK)

In Strings_es_ES.properties,i declared the strings like below and set the properties file with UTF-8 format.

admin.main.numberofrewards=Número de recompensas:
admin.main.categorylist=lista Categoría

I am using the above resource bundle in .jsp files like below

<%@ page pageEncoding="UTF-8"%>
<h2><spring:message code="admin.main.numberofrewards"/></h2>
<p><spring:message code="admin.main.categorylist"/></p>

I am getting output on the browser like below

Nómero de recompensas
lista CategorÃa

Please help me on this

BalusC
  • 1,082,665
  • 372
  • 3,610
  • 3,555
  • Please format your post properly – SpringLearner Dec 12 '13 at 12:19
  • How have you configured your MessageSource? If you don't specifically configure otherwise I think `.properties` files are expected to be in ISO-8859-1 encoding, not UTF-8. You may wish to consider running them through `native2ascii` as part of your build process as this guarantees they'll be read correctly whatever encoding the loader uses. – Ian Roberts Dec 12 '13 at 12:28

2 Answers2

10

and set the properties file with UTF-8 format

There's your mistake.

Until Java 9 they are by default read as ISO-8859-1 (at the time of writing of this question, the latest available Java version is 7, so the asker is definitely not yet using Java 9).

The java.util.Properties javadoc says the following:

... the input/output stream is encoded in ISO 8859-1 character encoding ...

They should therefore be saved as ISO-8859-1. If you intend to present characters which are not available by the ISO-8859-1 character set, then you have basically 4 options:

  1. Use native2ascii tool to convert an UTF-8 saved properties file to an ISO-8859-1 encoded properties file with unicode escapes in form of \uXXXX over all place instead of those "special characters" which are not covered by ISO-8859-1 charset.

  2. Use an properties file editor which is capable of doing #1 automagically, such as the one built in Eclipse. In other words, just create and edit the properties file in Eclipse. It'll worry automagically about the encoding.

  3. Use a custom ResourceBundle.Control implementation wherein you explicitly read the properties file yourself using new InputStreamReader(inputStream, "UTF-8"). However, as non-Spring-user I have no clue how to tell Spring to use that custom Control. In JSF it's easy.

  4. Upgrade to Java 9 or newer, which reads them as UTF-8 by default. This way you can safely save them as UTF-8.

See also:

BalusC
  • 1,082,665
  • 372
  • 3,610
  • 3,555
1

If your property file looks like this Descripción de artículos but you are getting this on the front-end Descripción de artículos, then the problem is the encoding used to read your property file, here is how you can tell Spring to always use UTF-8:

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.support.ReloadableResourceBundleMessageSource;

@Configuration
public class MessageConfiguration {

    @Bean
    public ReloadableResourceBundleMessageSource messageSource() {
        ReloadableResourceBundleMessageSource messageSource = new ReloadableResourceBundleMessageSource();
        messageSource.setDefaultEncoding("UTF-8");
        // other config options go here if needed, e.g. path to the property bundle
        return messageSource;
    }
}
SergeyB
  • 9,478
  • 4
  • 33
  • 47