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