java Spring Boot 国际化(messages.properties)

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/33837993/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-11-02 22:10:56  来源:igfitidea点击:

Spring Boot internationalization (messages.properties)

javaspringinternationalizationspring-bootthymeleaf

提问by Mike Melusky

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.propertiesdefined like this:

我试图简单地将应用程序的版本号添加到 Thymeleaf 片段。我正在使用 Spring Boot 1.2.5。如果我有一个名为/resources/messages.properties的文件,定义如下:

application.version=1.0.0

And I have a Thymeleaf view with the following fragment:

我有一个带有以下片段的 Thymeleaf 视图:

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.propertiesand messages_en_US.propertiesin 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.version_en_US?? 而不是 1.0.0。(我在类路径中也有名为messages_en.propertiesmessages_en_US.properties 的文件,它们的内容也相同。)我真的不知道如何解决这个问题......我花了几个小时在一些看起来非常微不足道的事情上......

Application.java

应用程序.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...

将向可以解决此问题的任何人购买虚拟回合...

采纳答案by Ashwini Rao

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

我想你总是可以在你的 templateEngine 方法中添加这个:

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.

另外,从设计的角度来看,我建议您尝试使用 thymeleaf 的自动配置(删除排除),以及 spring boot 自动为您提供的许多其他东西。