當前位置: 首頁>>代碼示例>>Java>>正文


Java LocaleResolver.setLocale方法代碼示例

本文整理匯總了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;
}
 
開發者ID:GovernIB,項目名稱:helium,代碼行數:17,代碼來源:IdiomaInterceptor.java

示例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();
}
 
開發者ID:suomenriistakeskus,項目名稱:oma-riista-web,代碼行數:17,代碼來源:LanguageChangeController.java

示例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;
}
 
開發者ID:Javakit,項目名稱:Javakit,代碼行數:17,代碼來源:LocaleChangeInterceptorWrapper.java

示例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;
}
 
開發者ID:huanzhou,項目名稱:jeecms6,代碼行數:21,代碼來源:AdminLocaleInterceptor.java

示例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;
}
 
開發者ID:huanzhou,項目名稱:jeecms6,代碼行數:20,代碼來源:FrontLocaleInterceptor.java

示例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");
	}
}
 
開發者ID:deathspeeder,項目名稱:class-guard,代碼行數:24,代碼來源:LocaleResolverTests.java

示例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;
}
 
開發者ID:caipiao,項目名稱:Lottery,代碼行數:20,代碼來源:AdminLocaleInterceptor.java

示例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;
}
 
開發者ID:xtianus,項目名稱:yadaframework,代碼行數:13,代碼來源:YadaLocalePathChangeInterceptor.java

示例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();
}
 
開發者ID:javahongxi,項目名稱:whatsmars,代碼行數:14,代碼來源:SampleController.java

示例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;
}
 
開發者ID:langtianya,項目名稱:spring4-understanding,代碼行數:16,代碼來源:RequestContext.java

示例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;
}
 
開發者ID:langtianya,項目名稱:spring4-understanding,代碼行數:29,代碼來源:LocaleChangeInterceptor.java

示例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);
}
 
開發者ID:hzxie,項目名稱:voj,代碼行數:13,代碼來源:LocaleUtils.java

示例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;
}
 
開發者ID:deathspeeder,項目名稱:class-guard,代碼行數:16,代碼來源:LocaleChangeInterceptor.java

示例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());
	}
}
 
開發者ID:mobilipia,項目名稱:Deskera-HRMS,代碼行數:13,代碼來源:authHandlerController.java


注:本文中的org.springframework.web.servlet.LocaleResolver.setLocale方法示例由純淨天空整理自Github/MSDocs等開源代碼及文檔管理平台,相關代碼片段篩選自各路編程大神貢獻的開源項目,源碼版權歸原作者所有,傳播和使用請參考對應項目的License;未經允許,請勿轉載。