allegro / handlebars-spring-boot-starter

Spring Boot auto-configuration for Handlebars
Apache License 2.0
108 stars 26 forks source link

Failing to get Internationalization via accept headers to work #26

Closed dadepo closed 6 years ago

dadepo commented 6 years ago

handlebars-spring-boot-starter: version 0.2.11 spring-boot: version 1.3.1.RELEASE

HI, I am trying to get Internationalization to work via headers but failing. I am not sure where things are going wrong.

Internationalization works if I use the mechanism of having a query parameter to indicate which language to choose. With that way, all I did was to define a bean of LocaleChangeInterceptor and LocaleResolver.

But what I want is for the language to be automatically picked up by what is set in the browser (ie via accept-headers). IN other to get that, this is what I tried:

Defined a bean of ReloadableResourceBundleMessageSource i.e:

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

I then removed the bean definition for LocaleChangeInterceptor

But this does not work. In fact with this definition, the language files stopped getting picked up totally.

I have spent over 1hour trying all sort of permutations to the configuration but not being successful. I thought if I asked here perhaps I get a faster pointer to where things could be going wrong.

Any ideas?

dadepo commented 6 years ago

After many more hours surfing the net for a solution, I finally found out what was wrong.

The bean definition for ReloadableResourceBundleMessageSource should be named messageSource.

so having it as:

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

or

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

Fixed the problem.