本文整理匯總了Java中org.springframework.web.servlet.LocaleResolver類的典型用法代碼示例。如果您正苦於以下問題:Java LocaleResolver類的具體用法?Java LocaleResolver怎麽用?Java LocaleResolver使用的例子?那麽, 這裏精選的類代碼示例或許可以為您提供幫助。
LocaleResolver類屬於org.springframework.web.servlet包,在下文中一共展示了LocaleResolver類的15個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的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;
}
示例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();
}
示例3: createPageContext
import org.springframework.web.servlet.LocaleResolver; //導入依賴的package包/類
protected MockPageContext createPageContext() {
MockServletContext sc = new MockServletContext();
SimpleWebApplicationContext wac = new SimpleWebApplicationContext();
wac.setServletContext(sc);
wac.setNamespace("test");
wac.refresh();
MockHttpServletRequest request = new MockHttpServletRequest(sc);
MockHttpServletResponse response = new MockHttpServletResponse();
if (inDispatcherServlet()) {
request.setAttribute(DispatcherServlet.WEB_APPLICATION_CONTEXT_ATTRIBUTE, wac);
LocaleResolver lr = new AcceptHeaderLocaleResolver();
request.setAttribute(DispatcherServlet.LOCALE_RESOLVER_ATTRIBUTE, lr);
ThemeResolver tr = new FixedThemeResolver();
request.setAttribute(DispatcherServlet.THEME_RESOLVER_ATTRIBUTE, tr);
request.setAttribute(DispatcherServlet.THEME_SOURCE_ATTRIBUTE, wac);
}
else {
sc.setAttribute(WebApplicationContext.ROOT_WEB_APPLICATION_CONTEXT_ATTRIBUTE, wac);
}
return new MockPageContext(sc, request, response);
}
示例4: preHandle
import org.springframework.web.servlet.LocaleResolver; //導入依賴的package包/類
@Override
public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
LocaleResolver localeResolver = RequestContextUtils.getLocaleResolver(request);
if (localeResolver == null) {
return true;
}
try {
Cookie[] cookies = request.getCookies();
if (cookies != null){
for (Cookie cookie : cookies){
if (Objects.equal("lang", cookie.getName())){
LocaleContextHolder.setLocale(new Locale(cookie.getValue()));
}
}
}
} catch (Exception e) {
Logs.error("occur errors when resolve locale: {}", Throwables.getStackTraceAsString(e));
}
return true;
}
示例5: 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;
}
示例6: 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;
}
示例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.getLocaleFront();
LocaleEditor localeEditor = new LocaleEditor();
localeEditor.setAsText(newLocale);
localeResolver.setLocale(request, response, (Locale) localeEditor
.getValue());
// Proceed in any case.
return true;
}
示例8: 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);
}
示例9: preHandle
import org.springframework.web.servlet.LocaleResolver; //導入依賴的package包/類
@Override
public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler)
throws Exception {
LocaleResolver localeResolver = RequestContextUtils.getLocaleResolver(request);
// Check if it's our case
if (localeResolver == null) {
throw new IllegalStateException("No LocaleResolver found: not in a DispatcherServlet request?");
}
if (!(localeResolver instanceof CookieLocaleResolver)) {
return true;
}
// Check if locale not in cookie.
// If so, then force it to store in cookie
CookieLocaleResolver cookieLocaleResolver = (CookieLocaleResolver) localeResolver;
Cookie cookie = WebUtils.getCookie(request, cookieLocaleResolver.getCookieName());
if (cookie == null) {
cookieLocaleResolver.setLocale(request, response, localeResolver.resolveLocale(request));
}
return true;
}
示例10: preHandle
import org.springframework.web.servlet.LocaleResolver; //導入依賴的package包/類
@Override
public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler)
throws Exception {
LocaleResolver localeResolver = RequestContextUtils.getLocaleResolver(request);
// Check if it's our case
if (localeResolver == null) {
throw new IllegalStateException("No LocaleResolver found: not in a DispatcherServlet request?");
}
if (!(localeResolver instanceof LocaleResolverUserBasedImpl)) {
return true;
}
// Check if locale not in cookie.
// If so, then force it to store in cookie
LocaleResolverUserBasedImpl cookieLocaleResolver = (LocaleResolverUserBasedImpl) localeResolver;
Locale localeFromCookie = cookieLocaleResolver.resolveLocaleFromCookie(request);
Locale localeFromUser = cookieLocaleResolver.resolveLocale(request);
if (localeFromUser != null && !localeFromUser.equals(localeFromCookie)) {
cookieLocaleResolver.setLocale(request, response, localeFromUser);
}
return true;
}
示例11: 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;
}
示例12: createPageContext
import org.springframework.web.servlet.LocaleResolver; //導入依賴的package包/類
protected MockPageContext createPageContext() {
MockServletContext sc = new MockServletContext();
sc.addInitParameter("springJspExpressionSupport", "true");
SimpleWebApplicationContext wac = new SimpleWebApplicationContext();
wac.setServletContext(sc);
wac.setNamespace("test");
wac.refresh();
MockHttpServletRequest request = new MockHttpServletRequest(sc);
MockHttpServletResponse response = new MockHttpServletResponse();
if (inDispatcherServlet()) {
request.setAttribute(DispatcherServlet.WEB_APPLICATION_CONTEXT_ATTRIBUTE, wac);
LocaleResolver lr = new AcceptHeaderLocaleResolver();
request.setAttribute(DispatcherServlet.LOCALE_RESOLVER_ATTRIBUTE, lr);
ThemeResolver tr = new FixedThemeResolver();
request.setAttribute(DispatcherServlet.THEME_RESOLVER_ATTRIBUTE, tr);
request.setAttribute(DispatcherServlet.THEME_SOURCE_ATTRIBUTE, wac);
}
else {
sc.setAttribute(WebApplicationContext.ROOT_WEB_APPLICATION_CONTEXT_ATTRIBUTE, wac);
}
return new MockPageContext(sc, request, response);
}
示例13: 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");
}
}
示例14: 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();
}
示例15: createPageContext
import org.springframework.web.servlet.LocaleResolver; //導入依賴的package包/類
protected MockPageContext createPageContext() {
MockServletContext sc = new MockServletContext();
StaticWebApplicationContext wac = new StaticWebApplicationContext();
wac.setServletContext(sc);
wac.setNamespace("test");
wac.registerSingleton("validator",
org.springframework.validation.beanvalidation.LocalValidatorFactoryBean.class);
wac.refresh();
MockHttpServletRequest request = new MockHttpServletRequest(sc);
MockHttpServletResponse response = new MockHttpServletResponse();
request.setAttribute(DispatcherServlet.WEB_APPLICATION_CONTEXT_ATTRIBUTE, wac);
LocaleResolver lr = new AcceptHeaderLocaleResolver();
request.setAttribute(DispatcherServlet.LOCALE_RESOLVER_ATTRIBUTE, lr);
ThemeResolver tr = new FixedThemeResolver();
request.setAttribute(DispatcherServlet.THEME_RESOLVER_ATTRIBUTE, tr);
request.setAttribute(DispatcherServlet.THEME_SOURCE_ATTRIBUTE, wac);
return new MockPageContext(sc, request, response);
}