本文整理匯總了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());
}
}