简体   繁体   中英

Spring Boot internationalization (messages.properties)

I'm trying to simply add the version number of an application to a Thymeleaf fragment. I'm using Spring Boot 1.2.5. If I have a file named /resources/messages.properties defined like this:

application.version=1.0.0

And I have a Thymeleaf view with the following fragment:

Application Version: <span th:text="#{application.version}">

It's displaying something like ??application.version_en_US?? instead of 1.0.0. (I also have files named messages_en.properties and messages_en_US.properties in the classpath with the same contents too.) I am really not sure how to resolve this problem... I've spent hours on something which seems incredibly trivial...

Application.java

@SpringBootApplication
@ComponentScan(basePackages = {"org.application"})
@EnableAutoConfiguration(exclude={DataSourceAutoConfiguration.class, ThymeleafAutoConfiguration.class})
@PropertySources(value = {@PropertySource("classpath:website.properties")})
public class Application extends SpringBootServletInitializer {

    public static void main(String[] args) throws Exception {
        SpringApplication.run(Application.class, args);
    }

    @Override
    protected SpringApplicationBuilder configure(SpringApplicationBuilder application) {
        return application.sources(Application.class);
    }

}

WebConfig.java

@EnableWebMvc
@Configuration
public class WebConfig extends WebMvcConfigurerAdapter {

@Bean
public SpelAwareProxyProjectionFactory projectionFactory() {
    return new SpelAwareProxyProjectionFactory();
}

@Bean
public SessionHandler sessionHandler() {
    return new SessionHandler();
}

@Override
public void addViewControllers(ViewControllerRegistry registry) {
    registry.addViewController("/web/auth/login").setViewName("auth/login");
    registry.setOrder(Ordered.HIGHEST_PRECEDENCE);
}

@Override
public void addResourceHandlers(ResourceHandlerRegistry registry) {
    registry.addResourceHandler("/resources/**").addResourceLocations(
            "/resources/");
}

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

@Bean
public CookieLocaleResolver localeResolver() {
    CookieLocaleResolver localeResolver = new CookieLocaleResolver();
    localeResolver.setDefaultLocale(Locale.ENGLISH);
    return localeResolver;
}

@Bean
public ResourceBundleMessageSource messageSource() {
    return new ResourceBundleMessageSource();
}

@Override
public void addInterceptors(InterceptorRegistry registry) {
    // Locale change interceptor
    registry.addInterceptor(localeChangeInterceptor());

    // Utility interceptor which helps with the "active" link styles in the navigation.  --mm
    registry.addInterceptor(new BaseInterceptor());

    // Expire session after a period of time
    registry.addInterceptor(sessionHandler());
}
}

ThymeleafConfig.java

@Configuration
public class ThymeleafConfig {

@Bean
public ServletContextTemplateResolver templateResolver() {
    ServletContextTemplateResolver resolver = new ServletContextTemplateResolver();
    resolver.setPrefix("/WEB-INF/views/");
    resolver.setSuffix(".html");

    // must use Legacy HTML5 as the template, otherwise Handlebars will not parse!
    //
    // this should hopefully be fixed in Thymeleaf 3.0
    resolver.setTemplateMode("LEGACYHTML5");
    resolver.setCacheable(false);
    return resolver;
}

public SpringTemplateEngine templateEngine() {
    SpringTemplateEngine engine = new SpringTemplateEngine();
    engine.setTemplateResolver(templateResolver());

    // Add Spring security
    Set<IDialect> dialects = new HashSet<IDialect>();
    engine.setAdditionalDialects(dialects);
    engine.addDialect(new SpringSecurityDialect());
    return engine;
}

@Bean
public ViewResolver viewResolver() {
    ThymeleafViewResolver viewResolver = new ThymeleafViewResolver();
    viewResolver.setTemplateEngine(templateEngine());
    viewResolver.setOrder(1);
    viewResolver.setViewNames(new String[]{"*"});
    viewResolver.setCache(false);
    return viewResolver;
}
}

Will buy a virtual round of shots to whomever can resolve this issue...

i guess you could always add this in your templateEngine method:

engine.addMessageResolver(new StandardMessageResolver());
or engine.setMessageResolver(new StandardMessageResolver());

Also,from the design perspective,i would suggest you to try using the autoconfiguration for thymeleaf(removing the exclude),and many other stuff which spring boot provides automatically for you.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM