Spring 中的占位符国际化
Internationalization of placeholders in Spring
我找不到像处理常规文本那样将占位符内的文本国际化的方法。我想将我的国际化放在:
输入类型="text" class="form-control" 名称="s" 占位符="Search Hobbies"
with input type="text" class="form-control" name="s" placeHolder="ctrlpanel.search.placeholder"
ctrlpanel.search.placeholder=搜索爱好
现在,在我的 JSP 中包含标签
<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core"%>
<%@ taglib prefix="fmt" uri="http://java.sun.com/jsp/jstl/fmt"%>
<%@ taglib prefix="spring" uri="http://www.springframework.org/tags" %>
<c:set var="contextRoot" value="${pageContext.request.contextPath}" />
<c:url var="search" value="/search" />
<hr/>
<div class="row">
<div class="col-md-8 col-md-offset-2">
<form action="${search}" method="post">
<input type="hidden" name="${_csrf.parameterName}"
value="${_csrf.token}" />
<div class="input-group input-group-lg">
<input type="text" class="form-control" name="s" placeHolder="Search Hobbies">
<span class="input-group-btn">
<button id="search-button" class="btn btn-primary" type="submit">
<spring:message code="ctrlpanel.find.button" text="default text" />
</button>
</span>
</div>
</form>
</div>
</div>
我的配置工作正常:
package com.caveofprogramming.configuration;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.LocaleResolver;
import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurerAdapter;
import org.springframework.web.servlet.i18n.LocaleChangeInterceptor;
import org.springframework.web.servlet.i18n.SessionLocaleResolver;
import java.util.Locale;
@Configuration
public class SpringMvcConfiguration extends WebMvcConfigurerAdapter {
@Bean
public LocaleResolver localeResolver(){
SessionLocaleResolver sessionLocaleResolver = new SessionLocaleResolver();
sessionLocaleResolver.setDefaultLocale(new Locale("es", "ES"));
//sessionLocaleResolver.setDefaultLocale(Locale.US);
return sessionLocaleResolver;
}
@Bean
LocaleChangeInterceptor localeChangeInterceptor(){
LocaleChangeInterceptor localeChangeInterceptor = new LocaleChangeInterceptor();
localeChangeInterceptor.setParamName("lang");
return localeChangeInterceptor;
}
@Override
public void addInterceptors(InterceptorRegistry interceptorRegistry){
interceptorRegistry.addInterceptor(localeChangeInterceptor());
}
}
因为这行不通。
<input type="text" class="form-control" name="s" placeHolder="<spring:message code="ctrlpanel.search.placeholder"/>">
使用 spring:message 的 var 属性,将结果绑定到页面、请求、会话或应用程序范围时使用。
<spring:message code="ctrlpanel.search.placeholder" var="searchPlaceholder"/>
然后像这样更新您的输入字段:
<input type="text" class="form-control" name="s" placeHolder="${searchPlaceholder}">
我找不到像处理常规文本那样将占位符内的文本国际化的方法。我想将我的国际化放在:
输入类型="text" class="form-control" 名称="s" 占位符="Search Hobbies"
with input type="text" class="form-control" name="s" placeHolder="ctrlpanel.search.placeholder"
ctrlpanel.search.placeholder=搜索爱好
现在,在我的 JSP 中包含标签
<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core"%>
<%@ taglib prefix="fmt" uri="http://java.sun.com/jsp/jstl/fmt"%>
<%@ taglib prefix="spring" uri="http://www.springframework.org/tags" %>
<c:set var="contextRoot" value="${pageContext.request.contextPath}" />
<c:url var="search" value="/search" />
<hr/>
<div class="row">
<div class="col-md-8 col-md-offset-2">
<form action="${search}" method="post">
<input type="hidden" name="${_csrf.parameterName}"
value="${_csrf.token}" />
<div class="input-group input-group-lg">
<input type="text" class="form-control" name="s" placeHolder="Search Hobbies">
<span class="input-group-btn">
<button id="search-button" class="btn btn-primary" type="submit">
<spring:message code="ctrlpanel.find.button" text="default text" />
</button>
</span>
</div>
</form>
</div>
</div>
我的配置工作正常:
package com.caveofprogramming.configuration;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.LocaleResolver;
import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurerAdapter;
import org.springframework.web.servlet.i18n.LocaleChangeInterceptor;
import org.springframework.web.servlet.i18n.SessionLocaleResolver;
import java.util.Locale;
@Configuration
public class SpringMvcConfiguration extends WebMvcConfigurerAdapter {
@Bean
public LocaleResolver localeResolver(){
SessionLocaleResolver sessionLocaleResolver = new SessionLocaleResolver();
sessionLocaleResolver.setDefaultLocale(new Locale("es", "ES"));
//sessionLocaleResolver.setDefaultLocale(Locale.US);
return sessionLocaleResolver;
}
@Bean
LocaleChangeInterceptor localeChangeInterceptor(){
LocaleChangeInterceptor localeChangeInterceptor = new LocaleChangeInterceptor();
localeChangeInterceptor.setParamName("lang");
return localeChangeInterceptor;
}
@Override
public void addInterceptors(InterceptorRegistry interceptorRegistry){
interceptorRegistry.addInterceptor(localeChangeInterceptor());
}
}
因为这行不通。
<input type="text" class="form-control" name="s" placeHolder="<spring:message code="ctrlpanel.search.placeholder"/>">
使用 spring:message 的 var 属性,将结果绑定到页面、请求、会话或应用程序范围时使用。
<spring:message code="ctrlpanel.search.placeholder" var="searchPlaceholder"/>
然后像这样更新您的输入字段:
<input type="text" class="form-control" name="s" placeHolder="${searchPlaceholder}">