如何在I18n上使用Spring?

Que*_*inV 1 java spring spring-mvc internationalization thymeleaf

我在github上的项目:https://github.com/QuentinVaut/JavaquariumEE

我跟随许多讲不同内容的教程,我试图实现教程中的解决方案,但没有错,我理解为什么.

你能告诉我我的项目有什么问题并解释一下吗?

许多教程和Github示例中的一个:

http://memorynotfound.com/spring-mvc-internationalization-i18n-example/

Far*_*raz 7

我粗略地看了一下这个教程.我将如何做到这一点:

首先配置它:

在配置类中创建一个返回MessageSource实现的Bean.

@Bean
public MessageSource messageSource() //The bean must be named messageSource.
{
ReloadableResourceBundleMessageSource messageSource =
new ReloadableResourceBundleMessageSource();
messageSource.setCacheSeconds(-1); //cache time in seconds was set to -1. This disables reloading and makes the message source cache messages forever (until the JVM restarts).
messageSource.setDefaultEncoding(StandardCharsets.UTF_8.name());
messageSource.setBasenames(
"/WEB-INF/i18n/messages", "/WEB-INF/i18n/errors"); //the message source is configured with the basenames /WEB-INF/i18n/messages and /WEB-INF/i18n/errors. This means that the message source will look for filenames like /WEB-INF/i18n/messages_en_US.properties, /WEB-INF/i18n/errors_fr_FR.properties
return messageSource;
}
Run Code Online (Sandbox Code Playgroud)

现在创建一个返回LocaleResolver的bean:

@Bean
public LocaleResolver localeResolver() //The bean must be named localeResolver.
{
return new SessionLocaleResolver();
}
Run Code Online (Sandbox Code Playgroud)

这使得LocaleResolver可用于DispatcherServlet执行的任何代码.这意味着其他非视图JSP无法访问LocaleResolver.要解决此问题,您可以创建一个过滤器并将其设置如下:

private ServletContext servletContext;
private LocaleResolver = new SessionLocaleResolver();    
@Inject MessageSource messageSource;
...

@Override
public void doFilter(ServletRequest request, ServletResponse response,
FilterChain chain) throws IOException, ServletException
{
request.setAttribute(
DispatcherServlet.LOCALE_RESOLVER_ATTRIBUTE, this.localeResolver
);
JstlUtils.exposeLocalizationContext(
(HttpServletRequest)request, this.messageSource
); 
Run Code Online (Sandbox Code Playgroud)

现在您需要配置处理程序拦截器:

您可以在配置类中覆盖WebMvcConfigurerAdapter的addInterceptors方法,以设置LocaleChangeInterceptor(或任何其他拦截器).

@Override
public void addInterceptors(InterceptorRegistry registry)
{
super.addInterceptors(registry);
registry.addInterceptor(new LocaleChangeInterceptor());
}
Run Code Online (Sandbox Code Playgroud)

现在,您只需在控制器上使用@Injected LocaleResolver即可.您只需在解析程序上调用setLocale即可更新当前语言环境.

编辑:更具体的例子:

假设你有一个简单的控制器:

@RequestMapping(value = "/", method = RequestMethod.GET)
public String index(Map<String, Object> model)
{
model.put("date", new Date());
model.put("alerts", 12);
model.put("numCritical", 0);
model.put("numImportant", 11);
model.put("numTrivial", 1);
return "home/index";
}
Run Code Online (Sandbox Code Playgroud)

然后说你在/ WEB-INF/i18n /下有messages_en_US.properties文件.此属性文件包含本地化为美国英语的消息.

title.alerts =服务器警报页面

alerts.current.date =当前日期和时间:

number.alerts =有{0,选择,0#没有警报| 1#是一个警报| 1

alert.details = {0,choice,0#没有警报| 1#一个警报是| 1 <{0,数字,整数}> \警告是关键的.{1,选择,0#无警报| 1#一个警报是| 1 <{1,number,\ integer}警报}很重要.{2,选择,0#没有警报| 1#一个警报\是| 1 <{2,数字,整数}警报是微不足道的.

然后,假设您在/ WEB-INF/i18n /下有messages_es_MX.properties文件,并且此文件包含本地化为墨西哥西班牙语的消息.

title.alerts = ServerAlertasPágina

alerts.current.date = Fecha y hora actual:number.alerts = {0,choice,0#No hay alertas | 1#Hay una alerta | 1

alert.details = {0,choice,0#No hay alertassoncríticos| 1#Una alerta es \crítica| 1 <{0,number,integer} alertassoncríticos}.\ {1,choice,0#No hay alertas son importantes | 1#Una alerta es importante\| 1 <{1,number,integer} alertas son importantes}.\ {2,选择,0#没有干草警报儿子琐事| 1#Una alerta es trivial\| 1 <{2,number,integer} alertas son triviales}.

现在,您需要<spring:message>在JSP中使用标记来翻译英语和西班牙语.这就是你的jsp页面的样子:

<spring:htmlEscape defaultHtmlEscape="true" />
<%--@elvariable id="date" type="java.util.Date"--%>
<%--@elvariable id="alerts" type="int"--%>
<%--@elvariable id="numCritical" type="int"--%>
<%--@elvariable id="numImportant" type="int"--%>
<%--@elvariable id="numTrivial" type="int"--%>

<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<%@ taglib prefix="fmt" uri="http://java.sun.com/jsp/jstl/fmt" %>
<%@ taglib prefix="fn" uri="http://java.sun.com/jsp/jstl/functions" %>
<%@ taglib prefix="spring" uri="http://www.springframework.org/tags" %>
<%@ taglib prefix="form" uri="http://www.springframework.org/tags/form" %>

<!DOCTYPE html>
<html>
<head>
<title><spring:message code="title.alerts" /></title>
</head>
<body>
<h2><spring:message code="title.alerts" /></h2>
<i><fmt:message key="alerts.current.date">
<fmt:param value="${date}" />
</fmt:message></i><br /><br />
<fmt:message key="number.alerts">
<fmt:param value="${alerts}" />
</fmt:message><c:if test="${alerts > 0}">
&nbsp;<spring:message code="alert.details">
<spring:argument value="${numCritical}" />
<spring:argument value="${numImportant}" />
<spring:argument value="${numTrivial}" />
</spring:message>
</c:if>
</body>
</html>
Run Code Online (Sandbox Code Playgroud)