本文整理汇总了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);
}
示例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;
}
示例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");
}
}
示例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();
}
示例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);
}
示例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);
}
示例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);
}
示例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);
}
示例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);
}
示例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");
}
示例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"));
}
示例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;
}
}
}