为什么我的 spring 引导应用程序不在小胡子模板中呈现 messages.properties
Why doesn't my spring boot application render messages.properties in mustache templates
我想使用国际化的 mustache 模板构建一个 spring 引导 Web 应用程序。
遵循本指南https://www.baeldung.com/spring-boot-internationalization 我尝试了一个使用 gradle 和 kotlin 的迷你示例,该示例可与 thymeleaf 模板一起使用,但无法用于 mustache
为了适应小胡子指南,我做了以下更改:
- 切换
implementation 'org.springframework.boot:spring-boot-starter-thymeleaf'
与
implementation 'org.springframework.boot:spring-boot-starter-mustache'
- 将 international.html 重命名为 international.mustache
像这样改变international.mustache
<html>
<head>
<title>Home</title>
</head>
<body>
<h1>{{#i18n}}greeting{{/i18n}} test</h1>
</body>
</html>
文件 messages.properties 包含行
greeting=Hello! Welcome to our website!
这里只是提供所有必要的代码是我的配置class
@Configuration
@ComponentScan(basePackages = ["com.example.translationtest.config"])
class AppConfig: WebMvcConfigurer {
@Bean
fun localeResolver(): LocaleResolver {
val slr = SessionLocaleResolver()
slr.setDefaultLocale(Locale.US)
return slr
}
@Bean
fun localeChangeInterceptor(): LocaleChangeInterceptor {
val lci = LocaleChangeInterceptor()
lci.paramName = "lang"
return lci
}
override fun addInterceptors(registry: InterceptorRegistry) {
registry.addInterceptor(localeChangeInterceptor())
}
}
当我在浏览器中访问页面时,我只看到字符串 test
虽然我希望看到
Hello! Welcome to our website! test
JMustache,这是 spring-boot-starter-mustache
使用的,不提供任何开箱即用的国际化支持。模板中的 {{#i18n}}greeting{{/i18n}}
被忽略,因为 JMustache 无法识别 i18n
.
如其自述文件中所述,您可以使用 Mustache.Lamda
:
实现国际化支持
You can also obtain the results of the fragment execution to do things like internationalization or caching:
Object ctx = new Object() {
Mustache.Lambda i18n = new Mustache.Lambda() {
public void execute (Template.Fragment frag, Writer out) throws IOException {
String key = frag.execute();
String text = // look up key in i18n system
out.write(text);
}
};
};
// template might look something like:
<h2>{{#i18n}}title{{/i18n}</h2>
{{#i18n}}welcome_msg{{/i18n}}
添加 Andy Winlkinson answer and Joining on Rotzlucky 预期我分享我为实现 JMustache 国际化工作所做的工作。
@ControllerAdvice
public class InternacionalizationAdvice {
@Autowired
private MessageSource message;
@ModelAttribute("i18n")
public Mustache.Lambda i18n(Locale locale){
return (frag, out) -> {
String body = frag.execute();
String message = this.message.getMessage(body, null, locale);
out.write(message);
};
}
}
我想使用国际化的 mustache 模板构建一个 spring 引导 Web 应用程序。
遵循本指南https://www.baeldung.com/spring-boot-internationalization 我尝试了一个使用 gradle 和 kotlin 的迷你示例,该示例可与 thymeleaf 模板一起使用,但无法用于 mustache
为了适应小胡子指南,我做了以下更改:
- 切换
implementation 'org.springframework.boot:spring-boot-starter-thymeleaf'
与implementation 'org.springframework.boot:spring-boot-starter-mustache'
- 将 international.html 重命名为 international.mustache
像这样改变international.mustache
<html> <head> <title>Home</title> </head> <body> <h1>{{#i18n}}greeting{{/i18n}} test</h1> </body> </html>
文件 messages.properties 包含行
greeting=Hello! Welcome to our website!
这里只是提供所有必要的代码是我的配置class
@Configuration
@ComponentScan(basePackages = ["com.example.translationtest.config"])
class AppConfig: WebMvcConfigurer {
@Bean
fun localeResolver(): LocaleResolver {
val slr = SessionLocaleResolver()
slr.setDefaultLocale(Locale.US)
return slr
}
@Bean
fun localeChangeInterceptor(): LocaleChangeInterceptor {
val lci = LocaleChangeInterceptor()
lci.paramName = "lang"
return lci
}
override fun addInterceptors(registry: InterceptorRegistry) {
registry.addInterceptor(localeChangeInterceptor())
}
}
当我在浏览器中访问页面时,我只看到字符串 test
虽然我希望看到
Hello! Welcome to our website! test
JMustache,这是 spring-boot-starter-mustache
使用的,不提供任何开箱即用的国际化支持。模板中的 {{#i18n}}greeting{{/i18n}}
被忽略,因为 JMustache 无法识别 i18n
.
如其自述文件中所述,您可以使用 Mustache.Lamda
:
You can also obtain the results of the fragment execution to do things like internationalization or caching:
Object ctx = new Object() { Mustache.Lambda i18n = new Mustache.Lambda() { public void execute (Template.Fragment frag, Writer out) throws IOException { String key = frag.execute(); String text = // look up key in i18n system out.write(text); } }; };
// template might look something like: <h2>{{#i18n}}title{{/i18n}</h2> {{#i18n}}welcome_msg{{/i18n}}
添加 Andy Winlkinson answer and Joining on Rotzlucky 预期我分享我为实现 JMustache 国际化工作所做的工作。
@ControllerAdvice
public class InternacionalizationAdvice {
@Autowired
private MessageSource message;
@ModelAttribute("i18n")
public Mustache.Lambda i18n(Locale locale){
return (frag, out) -> {
String body = frag.execute();
String message = this.message.getMessage(body, null, locale);
out.write(message);
};
}
}