Search code examples
javaspring-bootherokulocale

Heroku app does not display Cyrillic characters with UTF-8 as default encoding


I am following this Baeldung tutorial and although the languages change correctly, whenever I try to change to a Cyrillic-based language(Bulgarian,Russian), the characters are just question marks. I am hosting my app on Heroku.

I have tried executing heroku config:add LANG=en_US.UTF-8 in the Heroku CLI but that had no effect.

This is the code for my LocaleResolver,ReloadableResourceBundleMessageSource,LocaleChangeInterceptor and the overridden addInterceptors method

    @Bean
    public LocaleResolver localeResolver() {
        SessionLocaleResolver slr = new SessionLocaleResolver();
        slr.setDefaultLocale(Locale.US);
        return slr;
    }

    @Bean
    public ReloadableResourceBundleMessageSource messageSource() {
        ReloadableResourceBundleMessageSource messageSource = new ReloadableResourceBundleMessageSource();
        messageSource.setBasename("classpath:messages");
        messageSource.setDefaultEncoding("UTF-8"); 
        return messageSource;
    }

    @Bean
    public LocaleChangeInterceptor localeChangeInterceptor() {
        LocaleChangeInterceptor lci = new LocaleChangeInterceptor();
        lci.setParamName("lang");
        return lci;
    }

    @Override
    public void addInterceptors(InterceptorRegistry registry) {
        registry.addInterceptor(localeChangeInterceptor());
    }

I expect to get "Добре дошъл"(Welcome in Bulgarian) however I just get ??????? instead.

EDIT 1 After adding the Heroku Locale Buildpack, I get the following error when I try to deploy my app to Heroku:

-----> Locale app detected
cat: /tmp/build_1406e9f58f41cacf3931deb52b55d687/.locales: No such file or 
directory
!     Push rejected, failed to compile Locale app.
!     Push failed

Solution

  • If you have installed the locale buildpack, remove the .locales file you created and the buildpack by going to your Heroku app's settings.

    I fixed the problem by applying the following settings in my application.properties file:

    spring.http.encoding.charset=UTF-8
    spring.http.encoding.enabled=true
    spring.http.encoding.force=true
    

    Also, on a side note, the first build after these settings are applied may be extra slow - if you are building in Eclipse and are used to your Heroku app building in around 1 minute, beware that this may take over 5 minutes to build.