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


Java LocaleResolver.resolveLocale方法代碼示例

本文整理匯總了Java中org.springframework.web.servlet.LocaleResolver.resolveLocale方法的典型用法代碼示例。如果您正苦於以下問題:Java LocaleResolver.resolveLocale方法的具體用法?Java LocaleResolver.resolveLocale怎麽用?Java LocaleResolver.resolveLocale使用的例子?那麽, 這裏精選的方法代碼示例或許可以為您提供幫助。您也可以進一步了解該方法所在org.springframework.web.servlet.LocaleResolver的用法示例。


在下文中一共展示了LocaleResolver.resolveLocale方法的15個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Java代碼示例。

示例1: getMessage

import org.springframework.web.servlet.LocaleResolver; //導入方法依賴的package包/類
/**
 * 獲得國際化信息
 * 
 * @param request
 *            HttpServletRequest
 * @param code
 *            國際化代碼
 * @param args
 *            替換參數
 * @return
 * @see org.springframework.context.MessageSource#getMessage(String,
 *      Object[], Locale)
 */
public static String getMessage(HttpServletRequest request, String code,
		Object... args) {
	WebApplicationContext messageSource = RequestContextUtils.getWebApplicationContext(request);
	if (messageSource == null) {
		throw new IllegalStateException("WebApplicationContext not found!");
	}
	LocaleResolver localeResolver = RequestContextUtils
			.getLocaleResolver(request);
	Locale locale;
	if (localeResolver != null) {
		locale = localeResolver.resolveLocale(request);
	} else {
		locale = request.getLocale();
	}
	return messageSource.getMessage(code, args, locale);
}
 
開發者ID:huanzhou,項目名稱:jeecms6,代碼行數:30,代碼來源:MessageResolver.java

示例2: getLocale

import org.springframework.web.servlet.LocaleResolver; //導入方法依賴的package包/類
/**
 * Get locale for curent request
 * 
 * @return
 */
public static Locale getLocale() {
	// Plan A: Try to get it from locale context holder
	Locale locale = LocaleContextHolder.getLocale();
	if (locale != null) {
		return locale;
	}

	// Plan B: Try to get it from request
	HttpServletRequest request = get();
	LocaleResolver localeResolver = RequestContextUtils.getLocaleResolver(request);
	if (localeResolver == null) {
		log.warn("Cannot get locale resolver. Will fallback to: " + defaultLocale);
		return defaultLocale;
	}
	locale = localeResolver.resolveLocale(request);
	if (locale != null) {
		return locale;
	}

	// fallback
	log.warn("Cannot resolve locale. Will fallback to: " + defaultLocale);
	return defaultLocale;
}
 
開發者ID:skarpushin,項目名稱:summerb,代碼行數:29,代碼來源:CurrentRequestUtils.java

示例3: 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

示例4: doTag

import org.springframework.web.servlet.LocaleResolver; //導入方法依賴的package包/類
@Override
public void doTag() throws JspException, IOException {
	PageContext pageContext = (PageContext) getJspContext();
	ServletContext servletContext = pageContext.getServletContext();
	WebApplicationContext ac = WebApplicationContextUtils.getWebApplicationContext(servletContext);

	final List<String> languages = (List) ac.getBean("supportedLanguages");
	this.messageSource = (MessageSource) ac.getBean("messageSource");
	final LocaleResolver localeResolver = (LocaleResolver) ac.getBean("localeResolver");
	final Locale currentLocale = localeResolver.resolveLocale((HttpServletRequest) pageContext.getRequest());
	final JspWriter out = pageContext.getOut();

	final String label = getMessage(labelCode, currentLocale);

	out.println("<div data-role=\"l10nBox\" data-l10n-field=\"" + name + "\">");
	out.println("<label>" + label + ": </label>");
	this.writeHiddenInputCode(out);
	this.writeLanguageOptions(out, languages);
	this.writeLanguageInputs(out, languages);
	out.println("</div>");
	super.doTag();
}
 
開發者ID:tamerman,項目名稱:mobile-starting-framework,代碼行數:23,代碼來源:LocalisedFieldTag.java

示例5: getMessage

import org.springframework.web.servlet.LocaleResolver; //導入方法依賴的package包/類
/**
 * 獲得國際化信息
 * 
 * @param request
 *            HttpServletRequest
 * @param code
 *            國際化代碼
 * @param args
 *            替換參數
 * @return
 * @see org.springframework.context.MessageSource#getMessage(String,
 *      Object[], Locale)
 */
public static String getMessage(HttpServletRequest request, String code,
		Object... args) {
	WebApplicationContext messageSource = RequestContextUtils
			.getWebApplicationContext(request);
	if (messageSource == null) {
		throw new IllegalStateException("WebApplicationContext not found!");
	}
	LocaleResolver localeResolver = RequestContextUtils
			.getLocaleResolver(request);
	Locale locale;
	if (localeResolver != null) {
		locale = localeResolver.resolveLocale(request);
	} else {
		locale = request.getLocale();
	}
	return messageSource.getMessage(code, args, locale);
}
 
開發者ID:caipiao,項目名稱:Lottery,代碼行數:31,代碼來源:MessageResolver.java

示例6: getMessage

import org.springframework.web.servlet.LocaleResolver; //導入方法依賴的package包/類
/**
 * 獲得i18n字符串
 */
public static String getMessage(String code, Object[] args) {
	LocaleResolver localLocaleResolver = (LocaleResolver) SpringContextHolder.getBean(LocaleResolver.class);
	HttpServletRequest request = ((ServletRequestAttributes) RequestContextHolder.getRequestAttributes()).getRequest();
	Locale localLocale = localLocaleResolver.resolveLocale(request);
	return SpringContextHolder.getApplicationContext().getMessage(code, args, localLocale);
}
 
開發者ID:funtl,項目名稱:framework,代碼行數:10,代碼來源:StringUtils.java

示例7: getMessage

import org.springframework.web.servlet.LocaleResolver; //導入方法依賴的package包/類
/**
 * 獲得i18n字符串
 */
public static String getMessage(String code, Object[] args) {
    LocaleResolver localLocaleResolver = (LocaleResolver) SpringContextHolder.getBean(LocaleResolver.class);
    HttpServletRequest request = ((ServletRequestAttributes) RequestContextHolder.getRequestAttributes()).getRequest();
    Locale localLocale = localLocaleResolver.resolveLocale(request);
    return SpringContextHolder.getApplicationContext().getMessage(code, args, localLocale);
}
 
開發者ID:NeilRen,項目名稱:NEILREN4J,代碼行數:10,代碼來源:StringUtils.java

示例8: getMessage

import org.springframework.web.servlet.LocaleResolver; //導入方法依賴的package包/類
/**
 * 獲得i18n字符串
 */
public static String getMessage(String code, Object[] args) {
    LocaleResolver localLocaleResolver = SpringContextHolder.getBean(LocaleResolver.class);
    HttpServletRequest request = ((ServletRequestAttributes) RequestContextHolder.getRequestAttributes())
            .getRequest();
    Locale localLocale = localLocaleResolver.resolveLocale(request);
    return SpringContextHolder.getApplicationContext().getMessage(code, args, localLocale);
}
 
開發者ID:ansafari,項目名稱:melon,代碼行數:11,代碼來源:StringUtils.java

示例9: getMessage

import org.springframework.web.servlet.LocaleResolver; //導入方法依賴的package包/類
/**
 * 獲得i18n字符串
 */
public static String getMessage(String code, Object[] args) {
	LocaleResolver localLocaleResolver = (LocaleResolver) SpringContextHolder.getBean(LocaleResolver.class);
	HttpServletRequest request = ((ServletRequestAttributes)RequestContextHolder.getRequestAttributes()).getRequest();  
	Locale localLocale = localLocaleResolver.resolveLocale(request);
	return SpringContextHolder.getApplicationContext().getMessage(code, args, localLocale);
}
 
開發者ID:EleTeam,項目名稱:Shop-for-JavaWeb,代碼行數:10,代碼來源:StringUtils.java

示例10: overrideLocale

import org.springframework.web.servlet.LocaleResolver; //導入方法依賴的package包/類
@Test
public void overrideLocale() throws Exception {
	load(AllResources.class, "spring.mvc.locale:en_UK",
			"spring.mvc.locale-resolver=fixed");
	// mock request and set user preferred locale
	MockHttpServletRequest request = new MockHttpServletRequest();
	request.addPreferredLocale(StringUtils.parseLocaleString("nl_NL"));
	request.addHeader(HttpHeaders.ACCEPT_LANGUAGE, "nl_NL");
	LocaleResolver localeResolver = this.context.getBean(LocaleResolver.class);
	assertThat(localeResolver).isInstanceOf(FixedLocaleResolver.class);
	Locale locale = localeResolver.resolveLocale(request);
	// test locale resolver uses fixed locale and not user preferred locale
	assertThat(locale.toString()).isEqualTo("en_UK");
}
 
開發者ID:vikrammane23,項目名稱:https-github.com-g0t4-jenkins2-course-spring-boot,代碼行數:15,代碼來源:WebMvcAutoConfigurationTests.java

示例11: useAcceptHeaderLocale

import org.springframework.web.servlet.LocaleResolver; //導入方法依賴的package包/類
@Test
public void useAcceptHeaderLocale() {
	load(AllResources.class, "spring.mvc.locale:en_UK");
	// mock request and set user preferred locale
	MockHttpServletRequest request = new MockHttpServletRequest();
	request.addPreferredLocale(StringUtils.parseLocaleString("nl_NL"));
	request.addHeader(HttpHeaders.ACCEPT_LANGUAGE, "nl_NL");
	LocaleResolver localeResolver = this.context.getBean(LocaleResolver.class);
	assertThat(localeResolver).isInstanceOf(AcceptHeaderLocaleResolver.class);
	Locale locale = localeResolver.resolveLocale(request);
	// test locale resolver uses user preferred locale
	assertThat(locale.toString()).isEqualTo("nl_NL");
}
 
開發者ID:vikrammane23,項目名稱:https-github.com-g0t4-jenkins2-course-spring-boot,代碼行數:14,代碼來源:WebMvcAutoConfigurationTests.java

示例12: useDefaultLocaleIfAcceptHeaderNoSet

import org.springframework.web.servlet.LocaleResolver; //導入方法依賴的package包/類
@Test
public void useDefaultLocaleIfAcceptHeaderNoSet() {
	load(AllResources.class, "spring.mvc.locale:en_UK");
	// mock request and set user preferred locale
	MockHttpServletRequest request = new MockHttpServletRequest();
	LocaleResolver localeResolver = this.context.getBean(LocaleResolver.class);
	assertThat(localeResolver).isInstanceOf(AcceptHeaderLocaleResolver.class);
	Locale locale = localeResolver.resolveLocale(request);
	// test locale resolver uses default locale if no header is set
	assertThat(locale.toString()).isEqualTo("en_UK");
}
 
開發者ID:vikrammane23,項目名稱:https-github.com-g0t4-jenkins2-course-spring-boot,代碼行數:12,代碼來源:WebMvcAutoConfigurationTests.java

示例13: overrideLocale

import org.springframework.web.servlet.LocaleResolver; //導入方法依賴的package包/類
@Test
public void overrideLocale() throws Exception {
	load(AllResources.class, "spring.mvc.locale:en_UK");
	// mock request and set user preferred locale
	MockHttpServletRequest request = new MockHttpServletRequest();
	request.addPreferredLocale(StringUtils.parseLocaleString("nl_NL"));
	LocaleResolver localeResolver = this.context.getBean(LocaleResolver.class);
	Locale locale = localeResolver.resolveLocale(request);
	assertThat(localeResolver).isInstanceOf(FixedLocaleResolver.class);
	// test locale resolver uses fixed locale and not user preferred locale
	assertThat(locale.toString()).isEqualTo("en_UK");
}
 
開發者ID:philwebb,項目名稱:spring-boot-concourse,代碼行數:13,代碼來源:WebMvcAutoConfigurationTests.java

示例14: overrideLocale

import org.springframework.web.servlet.LocaleResolver; //導入方法依賴的package包/類
@Test
public void overrideLocale() throws Exception {
	load(AllResources.class, "spring.mvc.locale:en_UK");

	// mock request and set user preferred locale
	MockHttpServletRequest request = new MockHttpServletRequest();
	request.addPreferredLocale(StringUtils.parseLocaleString("nl_NL"));
	LocaleResolver localeResolver = this.context.getBean(LocaleResolver.class);
	Locale locale = localeResolver.resolveLocale(request);
	assertThat(localeResolver, instanceOf(FixedLocaleResolver.class));
	// test locale resolver uses fixed locale and not user preferred locale
	assertThat(locale.toString(), equalTo("en_UK"));
}
 
開發者ID:Nephilim84,項目名稱:contestparser,代碼行數:14,代碼來源:WebMvcAutoConfigurationTests.java

示例15: WebErrors

import org.springframework.web.servlet.LocaleResolver; //導入方法依賴的package包/類
/**
 * 通過HttpServletRequest創建WebErrors
 * 
 * @param request
 *            從request中獲得MessageSource和Locale,如果存在的話。
 */
public WebErrors(HttpServletRequest request) {
	WebApplicationContext webApplicationContext = RequestContextUtils
			.getWebApplicationContext(request);
	if (webApplicationContext != null) {
		LocaleResolver localeResolver = RequestContextUtils
				.getLocaleResolver(request);
		Locale locale;
		if (localeResolver != null) {
			locale = localeResolver.resolveLocale(request);
			this.messageSource = webApplicationContext;
			this.locale = locale;
		}
	}
}
 
開發者ID:huanzhou,項目名稱:jeecms6,代碼行數:21,代碼來源:WebErrors.java


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