samskivert / jmustache

A Java implementation of the Mustache templating language.
Other
834 stars 128 forks source link

Question: Spring Boot Internationalization #106

Closed JuKu closed 5 years ago

JuKu commented 5 years ago

Hi,

i'm using JMustache with Spring Boot and want to support localization. The example in the ReadMe did't work.

My template contains a i18n string:

<input type="submit" class="btn btn-primary btn-large btn-block" value="{{#i18n}}login{{/i18n}} " />

And i tried to configure spring & JMustache to use the correct dataSource:

@Override
    public void addInterceptors(InterceptorRegistry registry) {
        LocaleChangeInterceptor localeChangeInterceptor = new LocaleChangeInterceptor();
        localeChangeInterceptor.setParamName("lang");
        registry.addInterceptor(localeChangeInterceptor);
    }

    @Bean
    public LocaleResolver localeResolver() {
        //see also: https://www.baeldung.com/spring-security-login-error-handling-localization

        CookieLocaleResolver cookieLocaleResolver = new CookieLocaleResolver();

        //By default, the locale resolver will obtain the locale code from the HTTP header. To force a default locale, we need to set it on the localeResolver():
        cookieLocaleResolver.setDefaultLocale(Locale.ENGLISH);

        return cookieLocaleResolver;
    }

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

    @Bean
    public LocalizationMessageInterceptor getLocalizationMessageInterceptor() {
        LocalizationMessageInterceptor lmi = new LocalizationMessageInterceptor();
        lmi.setLocaleResolver(localeResolver());
        lmi.setMessageSource(messageSource());
        return lmi;
    }

But in template there is only a " " (empty space). If i print the string in my controller (with @AutoWired MessageSource) it work's fine, i get the correct string:

System.err.println("message: " + messageSource.getMessage("login", new Object[0], Locale.ENGLISH));

So what's the problem? How can i use localization with Spring Boot and JMustache? Thanks a lot!

dj1109 commented 5 years ago

Hi

JuKu commented 5 years ago

I have already solved the issue. This code is working for me:

    @Override
    public void addInterceptors(InterceptorRegistry registry) {
        LocaleChangeInterceptor localeChangeInterceptor = new LocaleChangeInterceptor();
        localeChangeInterceptor.setParamName("lang");
        registry.addInterceptor(localeChangeInterceptor);

        //i18n
        registry.addInterceptor(i18nMessageInterceptor());
    }

    @Bean
    public LocaleResolver localeResolver() {
        //see also: https://www.baeldung.com/spring-security-login-error-handling-localization

        CookieLocaleResolver cookieLocaleResolver = new CookieLocaleResolver();

        //By default, the locale resolver will obtain the locale code from the HTTP header. To force a default locale, we need to set it on the localeResolver():
        //cookieLocaleResolver.setDefaultLocale(Locale.ENGLISH);

        return cookieLocaleResolver;
    }

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

    @Bean
    public org.springframework.web.servlet.view.mustache.jmustache.LocalizationMessageInterceptor i18nMessageInterceptor () {
        org.springframework.web.servlet.view.mustache.jmustache.LocalizationMessageInterceptor lmi = new org.springframework.web.servlet.view.mustache.jmustache.LocalizationMessageInterceptor();
        lmi.setLocaleResolver(localeResolver());
        lmi.setMessageSource(messageSource());
        lmi.setMessageKey("i18n");
        return lmi;
    }