当前位置: 首页>>代码示例>>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;未经允许,请勿转载。