1
我想讓i18n與我的春季啓動應用程序一起使用,它使用thymeleaf作爲模板引擎。i18n(國際化)和thymeleaf的春季啓動
我跟着一些教程,它教我如何定義消息源和本地化解析器,所以我做了這個配置類:
@Configuration
@EnableWebMvc
public class AppConfig extends WebMvcConfigurerAdapter {
@Bean
public MessageSource messageSource() {
ReloadableResourceBundleMessageSource msgSrc = new ReloadableResourceBundleMessageSource();
msgSrc.setBasename("i18n/messages");
msgSrc.setDefaultEncoding("UTF-8");
return msgSrc;
}
@Bean
public LocaleResolver localeResolver() {
CookieLocaleResolver resolver = new CookieLocaleResolver();
resolver.setDefaultLocale(new Locale("en"));
resolver.setCookieName("myI18N_cookie");
return resolver;
}
@Override
public void addInterceptors(InterceptorRegistry reg) {
LocaleChangeInterceptor interceptor = new LocaleChangeInterceptor();
interceptor.setParamName("locale");
reg.addInterceptor(interceptor);
}
}
然後,在我的資源文件夾中(src /主/資源)我做了文件夾國際化和裏面我把messages.properties和messages_sl.properties
內限定有first.greeting =世界您好!
這是我thymeleaf模板:
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml" xmlns:th="http://www.thymeleaf.org" th:with="lang=${#locale.language}" th:lang="${lang}">
<head>
<meta charset="UTF-8"/>
</head>
<body>
<a href="/?locale=en">English</a> | <a href="/?locale=sl">Slovenian</a>
<h3 th:text="#{first.greeting}"></h3>
</body>
</html>
我的控制器是沒有什麼特別的,它只是轉發這一觀點,當我訪問它,在我的屬性文件我已經定義:
spring.thymeleaf.mode=LEGACYHTML5
spring.thymeleaf.cache=false
但是,當我加載頁面時,而不是Hello World!我得到?? first.greeting_en ??或?? first.greeting_sl ??,取決於設置的區域設置。
無論我看到什麼,我看到了相同的配置,所以我真的迷失了,因爲我錯過了什麼。
這裏是我的項目結構: