本文整理汇总了Java中org.springframework.web.servlet.LocaleResolver.setLocale方法的典型用法代码示例。如果您正苦于以下问题:Java LocaleResolver.setLocale方法的具体用法?Java LocaleResolver.setLocale怎么用?Java LocaleResolver.setLocale使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类org.springframework.web.servlet.LocaleResolver
的用法示例。
在下文中一共展示了LocaleResolver.setLocale方法的14个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。
示例1: preHandle
import org.springframework.web.servlet.LocaleResolver; //导入方法依赖的package包/类
public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws ServletException {
if (request.getUserPrincipal() != null) {
String codiIdioma = request.getParameter(this.paramName);
if (codiIdioma != null) {
personaService.savePrefIdioma(codiIdioma);
LocaleResolver localeResolver = RequestContextUtils.getLocaleResolver(request);
if (localeResolver == null)
throw new IllegalStateException ("No LocaleResolver found: not in a DispatcherServlet request?");
LocaleEditor localeEditor = new LocaleEditor();
localeEditor.setAsText(codiIdioma);
localeResolver.setLocale(request, response, (Locale)localeEditor.getValue());
}
}
return true;
}
示例2: changeLanguage
import org.springframework.web.servlet.LocaleResolver; //导入方法依赖的package包/类
@RequestMapping(value = "/api/v1/language", method = RequestMethod.POST)
public String changeLanguage(@ModelAttribute("lang") String language,
HttpServletRequest request, HttpServletResponse response) {
final LocaleResolver localeResolver = getLocaleResolver(request);
final Locale locale = StringUtils.parseLocaleString(language);
if (locale != null) {
localeResolver.setLocale(request, response, locale);
return locale.toLanguageTag();
}
LOG.error("Could not parse language={}", language);
return localeResolver.resolveLocale(request).toLanguageTag();
}
示例3: preHandle
import org.springframework.web.servlet.LocaleResolver; //导入方法依赖的package包/类
@Override
public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws ServletException {
String newLocale = request.getParameter(getParamName());
if(newLocale != null) {
LocaleResolver localeResolver = RequestContextUtils.getLocaleResolver(request);
if(localeResolver == null) {
throw new IllegalStateException("No LocaleResolver found: not in a DispatcherServlet request?");
}
Locale locale = StringUtils.parseLocaleString(newLocale);
localeResolver.setLocale(request, response, locale);
localeHolder.setLocale(locale);
}
return true;
}
示例4: preHandle
import org.springframework.web.servlet.LocaleResolver; //导入方法依赖的package包/类
@Override
public boolean preHandle(HttpServletRequest request,
HttpServletResponse response, Object handler)
throws ServletException {
LocaleResolver localeResolver = RequestContextUtils
.getLocaleResolver(request);
if (localeResolver == null) {
throw new IllegalStateException(
"No LocaleResolver found: not in a DispatcherServlet request?");
}
// CmsSite site = CmsUtils.getSite(request);
CmsSite site=CmsThreadVariable.getSite();
String newLocale = site.getLocaleAdmin();
LocaleEditor localeEditor = new LocaleEditor();
localeEditor.setAsText(newLocale);
localeResolver.setLocale(request, response, (Locale) localeEditor
.getValue());
// Proceed in any case.
return true;
}
示例5: preHandle
import org.springframework.web.servlet.LocaleResolver; //导入方法依赖的package包/类
@Override
public boolean preHandle(HttpServletRequest request,
HttpServletResponse response, Object handler)
throws ServletException {
LocaleResolver localeResolver = RequestContextUtils
.getLocaleResolver(request);
if (localeResolver == null) {
throw new IllegalStateException(
"No LocaleResolver found: not in a DispatcherServlet request?");
}
CmsSite site = CmsUtils.getSite(request);
String newLocale = site.getLocaleFront();
LocaleEditor localeEditor = new LocaleEditor();
localeEditor.setAsText(newLocale);
localeResolver.setLocale(request, response, (Locale) localeEditor
.getValue());
// Proceed in any case.
return true;
}
示例6: internalTest
import org.springframework.web.servlet.LocaleResolver; //导入方法依赖的package包/类
private void internalTest(LocaleResolver localeResolver, boolean shouldSet) {
// create mocks
MockServletContext context = new MockServletContext();
MockHttpServletRequest request = new MockHttpServletRequest(context);
request.addPreferredLocale(Locale.UK);
MockHttpServletResponse response = new MockHttpServletResponse();
// check original locale
Locale locale = localeResolver.resolveLocale(request);
assertEquals(locale, Locale.UK);
// set new locale
try {
localeResolver.setLocale(request, response, Locale.GERMANY);
if (!shouldSet)
fail("should not be able to set Locale");
// check new locale
locale = localeResolver.resolveLocale(request);
assertEquals(locale, Locale.GERMANY);
}
catch (UnsupportedOperationException ex) {
if (shouldSet)
fail("should be able to set Locale");
}
}
示例7: preHandle
import org.springframework.web.servlet.LocaleResolver; //导入方法依赖的package包/类
@Override
public boolean preHandle(HttpServletRequest request,
HttpServletResponse response, Object handler)
throws ServletException {
LocaleResolver localeResolver = RequestContextUtils
.getLocaleResolver(request);
if (localeResolver == null) {
throw new IllegalStateException(
"No LocaleResolver found: not in a DispatcherServlet request?");
}
CmsSite site = CmsUtils.getSite(request);
String newLocale = site.getLocaleAdmin();
LocaleEditor localeEditor = new LocaleEditor();
localeEditor.setAsText(newLocale);
localeResolver.setLocale(request, response, (Locale) localeEditor
.getValue());
// Proceed in any case.
return true;
}
示例8: preHandle
import org.springframework.web.servlet.LocaleResolver; //导入方法依赖的package包/类
@Override
public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) {
Object newLocale = request.getAttribute(LOCALE_ATTRIBUTE_NAME);
if (newLocale != null) {
LocaleResolver localeResolver = RequestContextUtils.getLocaleResolver(request);
if (localeResolver == null) {
throw new IllegalStateException("No LocaleResolver found: not in a DispatcherServlet request?");
}
localeResolver.setLocale(request, response, StringUtils.parseLocaleString(newLocale.toString()));
}
return true;
}
示例9: changeLang
import org.springframework.web.servlet.LocaleResolver; //导入方法依赖的package包/类
@RequestMapping(value = "/changeLang", method = RequestMethod.POST)
@ResponseBody
public String changeLang(HttpServletRequest request, HttpServletResponse response, @RequestParam String lang){
LocaleResolver localeResolver = RequestContextUtils.getLocaleResolver(request);
if ("zh".equals(lang)) {
// request.getSession().setAttribute(SessionLocaleResolver.LOCALE_SESSION_ATTRIBUTE_NAME, new Locale("zh", "CN"));
localeResolver.setLocale(request, response, new Locale("zh", "CN"));
} else if("en".equals(lang)){
// request.getSession().setAttribute(SessionLocaleResolver.LOCALE_SESSION_ATTRIBUTE_NAME, new Locale("en", "US"));
localeResolver.setLocale(request, response, new Locale("en", "US"));
}
return "lang:" + LocaleContextHolder.getLocale().getLanguage();
}
示例10: changeLocale
import org.springframework.web.servlet.LocaleResolver; //导入方法依赖的package包/类
/**
* Change the current locale to the specified one,
* storing the new locale through the configured {@link LocaleResolver}.
* @param locale the new locale
* @see LocaleResolver#setLocale
* @see #changeLocale(java.util.Locale, java.util.TimeZone)
*/
public void changeLocale(Locale locale) {
LocaleResolver localeResolver = RequestContextUtils.getLocaleResolver(this.request);
if (localeResolver == null) {
throw new IllegalStateException("Cannot change locale if no LocaleResolver configured");
}
localeResolver.setLocale(this.request, this.response, locale);
this.locale = locale;
}
示例11: preHandle
import org.springframework.web.servlet.LocaleResolver; //导入方法依赖的package包/类
@Override
public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler)
throws ServletException {
String newLocale = request.getParameter(getParamName());
if (newLocale != null) {
if (checkHttpMethod(request.getMethod())) {
LocaleResolver localeResolver = RequestContextUtils.getLocaleResolver(request);
if (localeResolver == null) {
throw new IllegalStateException(
"No LocaleResolver found: not in a DispatcherServlet request?");
}
try {
localeResolver.setLocale(request, response, StringUtils.parseLocaleString(newLocale));
}
catch (IllegalArgumentException ex) {
if (isIgnoreInvalidLocale()) {
logger.debug("Ignoring invalid locale value [" + newLocale + "]: " + ex.getMessage());
}
else {
throw ex;
}
}
}
}
// Proceed in any case.
return true;
}
示例12: setLocale
import org.springframework.web.servlet.LocaleResolver; //导入方法依赖的package包/类
/**
* 根据用户语言设置Locale信息.
* @param request - HttpRequest对象
* @param response - HttpResponse对象
* @param language - 语言的名称(例如zh_CN)
*/
public static void setLocale(HttpServletRequest request, HttpServletResponse response, String language) {
Locale locale = LocaleUtils.getLocaleOfLanguage(language);
LocaleResolver localeResolver = RequestContextUtils.getLocaleResolver(request);
localeResolver.setLocale(request, response, locale);
request.getSession().setAttribute("language", language);
}
示例13: preHandle
import org.springframework.web.servlet.LocaleResolver; //导入方法依赖的package包/类
@Override
public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler)
throws ServletException {
String newLocale = request.getParameter(this.paramName);
if (newLocale != null) {
LocaleResolver localeResolver = RequestContextUtils.getLocaleResolver(request);
if (localeResolver == null) {
throw new IllegalStateException("No LocaleResolver found: not in a DispatcherServlet request?");
}
localeResolver.setLocale(request, response, StringUtils.parseLocaleString(newLocale));
}
// Proceed in any case.
return true;
}
示例14: setLocale
import org.springframework.web.servlet.LocaleResolver; //导入方法依赖的package包/类
protected void setLocale(HttpServletRequest request, HttpServletResponse response, String newLocale) {
if (newLocale != null) {
LocaleResolver localeResolver = RequestContextUtils.getLocaleResolver(request);
if (localeResolver == null) {
logger.debug("No LocaleResolver found: not in a DispatcherServlet request?");
return;
}
LocaleEditor localeEditor = new LocaleEditor();
localeEditor.setAsText(newLocale);
localeResolver.setLocale(request, response, (Locale) localeEditor.getValue());
}
}