org.springframework.web.servlet.i18n.SessionLocaleResolver Java Examples

The following examples show how to use org.springframework.web.servlet.i18n.SessionLocaleResolver. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: JsonHandlerMethodReturnValueHandler.java    From sca-best-practice with Apache License 2.0 6 votes vote down vote up
@Override
public void handleReturnValue(Object returnValue, MethodParameter returnType, ModelAndViewContainer mavContainer,
                              NativeWebRequest webRequest) throws Exception {
    mavContainer.setRequestHandled(true);
    HttpServletResponse httpServletResponse = webRequest.getNativeResponse(HttpServletResponse.class);
    httpServletResponse.setContentType(CONTENT_TYPE);
    ServletServerHttpResponse outputMessage = new ServletServerHttpResponse(httpServletResponse);
    Locale locale = (Locale)webRequest.getAttribute(SessionLocaleResolver.LOCALE_SESSION_ATTRIBUTE_NAME,
        RequestAttributes.SCOPE_SESSION);
    String message;
    try {
        message = messageSource.getMessage(DEFAULT_SUCCESS_CODE, new Object[0], locale);
    } catch (NoSuchMessageException e) {
        message = DefaultMessagesProperties.getMessage(DEFAULT_SUCCESS_CODE);
    }
    JsonResponse jsonResponse = new JsonResponse(DEFAULT_SUCCESS_CODE, message, returnValue);
    outputMessage.getBody().write(StringUtils.toBytes(JsonUtils.toJson(jsonResponse)));
    outputMessage.getBody().flush();
}
 
Example #2
Source File: LocaleUtils.java    From EasyReport with Apache License 2.0 6 votes vote down vote up
/**
 * 根据当前request对象中的locale(Header的Accept属性)初始化系统国际化语言区域环境
 *
 * @param request  当前请求对象
 * @param response 当前响应对象
 */
public static void setInitLocale(final HttpServletRequest request, final HttpServletResponse response) {
    final Locale locale = request.getLocale();
    log.info("Init locale from user request,country:{},lang:{}", locale.getCountry(), locale.toLanguageTag());

    if (localeResolver instanceof CookieLocaleResolver) {
        final CookieLocaleResolver cookieLocaleResolver = (CookieLocaleResolver)localeResolver;
        final Cookie cookie = WebUtils.getCookie(request, cookieLocaleResolver.getCookieName());
        if (cookie == null) {
            setLocale(locale.toLanguageTag(), request, response);
        }
    }
    if (localeResolver instanceof SessionLocaleResolver) {
        final Locale sessionLocale = (Locale)WebUtils.getRequiredSessionAttribute(
            request, SessionLocaleResolver.LOCALE_SESSION_ATTRIBUTE_NAME);
        if (sessionLocale == null) {
            setLocale(locale.toLanguageTag(), request, response);
        }
    }
}
 
Example #3
Source File: OptionController.java    From stone with GNU General Public License v3.0 6 votes vote down vote up
/**
 * 保存设置选项
 *
 * @param options options
 *
 * @return JsonResult
 */
@PostMapping(value = "/save")
@ResponseBody
public JsonResult saveOptions(@RequestParam Map<String, String> options, HttpSession session) {
    try {
        optionsService.saveOptions(options);
        //刷新options
        configuration.setSharedVariable("options", optionsService.findAllOptions());
        HaloConst.OPTIONS.clear();
        HaloConst.OPTIONS = optionsService.findAllOptions();
        session.removeAttribute(SessionLocaleResolver.LOCALE_SESSION_ATTRIBUTE_NAME);
        log.info("List of saved options: " + options);
        return new JsonResult(ResultCodeEnum.SUCCESS.getCode(), localeMessageUtil.getMessage("code.admin.common.save-success"));
    } catch (Exception e) {
        log.error("Save settings option failed: {}", e.getMessage());
        return new JsonResult(ResultCodeEnum.FAIL.getCode(), localeMessageUtil.getMessage("code.admin.common.save-failed"));
    }
}
 
Example #4
Source File: WebMvcAutoConfiguration.java    From blog-sharon with Apache License 2.0 5 votes vote down vote up
/**
 * 国际化设置
 *
 * @return LocaleResolver
 */
@Bean
public LocaleResolver localeResolver() {
    SessionLocaleResolver slr = new SessionLocaleResolver();
    slr.setDefaultLocale(Locale.CHINA);
    return slr;
}
 
Example #5
Source File: I18nConfig.java    From fw-spring-cloud with Apache License 2.0 5 votes vote down vote up
/**
 * session区域解析器
 * @return
 */
@Bean
public LocaleResolver localeResolver() {
    SessionLocaleResolver resolver = new SessionLocaleResolver();
    resolver.setDefaultLocale(Locale.CHINA);

    return resolver;
}
 
Example #6
Source File: HelloController.java    From springboot-learn with MIT License 5 votes vote down vote up
/**
 * 切换语言,只作用于session
 *
 * @param request
 * @param lang
 * @return
 */
@RequestMapping("/i18n")
public String changeSessionLanauage(HttpServletRequest request, String lang) {
    System.out.println(lang);
    if (CommonConsts.LANG_ZH.equals(lang)) {
        //代码中即可通过以下方法进行语言设置
        request.getSession().setAttribute(SessionLocaleResolver.LOCALE_SESSION_ATTRIBUTE_NAME, new Locale("zh", "CN"));
    } else if (CommonConsts.LANG_EN.equals(lang)) {
        request.getSession().setAttribute(SessionLocaleResolver.LOCALE_SESSION_ATTRIBUTE_NAME, new Locale("en", "US"));
    }
    return "redirect:/hello";
}
 
Example #7
Source File: I18nConfig.java    From ruoyiplus with MIT License 5 votes vote down vote up
@Bean
public LocaleResolver localeResolver()
{
    SessionLocaleResolver slr = new SessionLocaleResolver();
    // 默认语言
    slr.setDefaultLocale(Locale.SIMPLIFIED_CHINESE);
    return slr;
}
 
Example #8
Source File: LocaleConfig.java    From springBoot with MIT License 5 votes vote down vote up
@Bean
public LocaleResolver localeResolver() {
    SessionLocaleResolver slr = new SessionLocaleResolver();
    // 默认语言
    slr.setDefaultLocale(Locale.US);
    return slr;
}
 
Example #9
Source File: I18nConfig.java    From open-capacity-platform with Apache License 2.0 5 votes vote down vote up
@Bean
public LocaleResolver localeResolver() {
    SessionLocaleResolver sessionLocaleResolver = new SessionLocaleResolver();
    sessionLocaleResolver.setDefaultLocale(Locale.CHINA);
    return sessionLocaleResolver;

}
 
Example #10
Source File: I18nConfig.java    From springboot-learn with MIT License 5 votes vote down vote up
/**
 * Session方式
 *
 * @return
 */
@Bean
public LocaleResolver localeResolver() {
    SessionLocaleResolver slr = new SessionLocaleResolver();
    //设置默认区域
    slr.setDefaultLocale(Locale.US);

    return slr;
}
 
Example #11
Source File: ComplexWebApplicationContext.java    From spring-analysis-note with MIT License 5 votes vote down vote up
@Override
public void doSomething(HttpServletRequest request) throws ServletException, IllegalAccessException {
	WebApplicationContext wac = RequestContextUtils.findWebApplicationContext(request);
	if (!(wac instanceof ComplexWebApplicationContext)) {
		throw new ServletException("Incorrect WebApplicationContext");
	}
	if (WebUtils.getNativeRequest(request, MultipartHttpServletRequest.class) == null) {
		throw new ServletException("Not in a MultipartHttpServletRequest");
	}
	if (request.getParameter("fail") != null) {
		throw new ModelAndViewDefiningException(new ModelAndView("failed1"));
	}
	if (request.getParameter("access") != null) {
		throw new IllegalAccessException("illegal access");
	}
	if (request.getParameter("servlet") != null) {
		throw new ServletRequestBindingException("servlet");
	}
	if (request.getParameter("exception") != null) {
		throw new RuntimeException("servlet");
	}
	if (!(RequestContextUtils.getLocaleResolver(request) instanceof SessionLocaleResolver)) {
		throw new ServletException("Incorrect LocaleResolver");
	}
	if (!Locale.CANADA.equals(RequestContextUtils.getLocale(request))) {
		throw new ServletException("Incorrect Locale");
	}
	if (!Locale.CANADA.equals(LocaleContextHolder.getLocale())) {
		throw new ServletException("Incorrect Locale");
	}
	if (RequestContextUtils.getTimeZone(request) != null) {
		throw new ServletException("Incorrect TimeZone");
	}
	if (!TimeZone.getDefault().equals(LocaleContextHolder.getTimeZone())) {
		throw new ServletException("Incorrect TimeZone");
	}
	if (!(RequestContextUtils.getThemeResolver(request) instanceof SessionThemeResolver)) {
		throw new ServletException("Incorrect ThemeResolver");
	}
	if (!"theme".equals(RequestContextUtils.getThemeResolver(request).resolveThemeName(request))) {
		throw new ServletException("Incorrect theme name");
	}
	RequestContext rc = new RequestContext(request);
	rc.changeLocale(Locale.US, TimeZone.getTimeZone("GMT+1"));
	rc.changeTheme("theme2");
	if (!Locale.US.equals(RequestContextUtils.getLocale(request))) {
		throw new ServletException("Incorrect Locale");
	}
	if (!Locale.US.equals(LocaleContextHolder.getLocale())) {
		throw new ServletException("Incorrect Locale");
	}
	if (!TimeZone.getTimeZone("GMT+1").equals(RequestContextUtils.getTimeZone(request))) {
		throw new ServletException("Incorrect TimeZone");
	}
	if (!TimeZone.getTimeZone("GMT+1").equals(LocaleContextHolder.getTimeZone())) {
		throw new ServletException("Incorrect TimeZone");
	}
	if (!"theme2".equals(RequestContextUtils.getThemeResolver(request).resolveThemeName(request))) {
		throw new ServletException("Incorrect theme name");
	}
}
 
Example #12
Source File: WebMvcAutoConfiguration.java    From stone with GNU General Public License v3.0 5 votes vote down vote up
/**
 * 国际化设置
 *
 * @return LocaleResolver
 */
@Bean
public LocaleResolver localeResolver() {
    final SessionLocaleResolver slr = new SessionLocaleResolver();
    slr.setDefaultLocale(Locale.CHINA);
    return slr;
}
 
Example #13
Source File: LocaleInterceptor.java    From stone with GNU General Public License v3.0 5 votes vote down vote up
@Override
public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
    final Object attribute = request.getSession().getAttribute(SessionLocaleResolver.LOCALE_SESSION_ATTRIBUTE_NAME);
    if (null != attribute) {
        return true;
    }
    if (StrUtil.equals(LocaleEnum.EN_US.getValue(), HaloConst.OPTIONS.get(BlogPropertiesEnum.BLOG_LOCALE.getProp()))) {
        request.getSession().setAttribute(SessionLocaleResolver.LOCALE_SESSION_ATTRIBUTE_NAME, new Locale("en", "US"));
    } else {
        request.getSession().setAttribute(SessionLocaleResolver.LOCALE_SESSION_ATTRIBUTE_NAME, new Locale("zh", "CN"));
    }
    return true;
}
 
Example #14
Source File: LocaleInterceptor.java    From SENS with GNU General Public License v3.0 5 votes vote down vote up
@Override
public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
    if (StringUtils.equals(LocaleEnum.EN_US.getValue(), SensConst.OPTIONS.get(BlogPropertiesEnum.BLOG_LOCALE.getProp()))) {
        request.getSession().setAttribute(SessionLocaleResolver.LOCALE_SESSION_ATTRIBUTE_NAME, new Locale("en", "US"));
    } else {
        request.getSession().setAttribute(SessionLocaleResolver.LOCALE_SESSION_ATTRIBUTE_NAME, new Locale("zh", "CN"));
    }
    return true;
}
 
Example #15
Source File: WebMvcConfig.java    From FlyCms with MIT License 5 votes vote down vote up
@Bean
public LocaleResolver localeResolver() {
	SessionLocaleResolver slr = new SessionLocaleResolver();
	// 默认语言
	slr.setDefaultLocale(Locale.CHINA);
	return slr;
}
 
Example #16
Source File: LocaleInterceptor.java    From blog-sharon with Apache License 2.0 5 votes vote down vote up
@Override
public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
    if (StrUtil.equals(LocaleEnum.EN_US.getValue(), HaloConst.OPTIONS.get(BlogPropertiesEnum.BLOG_LOCALE.getProp()))) {
        request.getSession().setAttribute(SessionLocaleResolver.LOCALE_SESSION_ATTRIBUTE_NAME, new Locale("en", "US"));
    } else {
        request.getSession().setAttribute(SessionLocaleResolver.LOCALE_SESSION_ATTRIBUTE_NAME, new Locale("zh", "CN"));
    }
    return true;
}
 
Example #17
Source File: WebMvcConfig.java    From spring-comparing-template-engines with Apache License 2.0 5 votes vote down vote up
@Bean
public LocaleResolver localeResolver() {
	SessionLocaleResolver slr = new SessionLocaleResolver();
	slr.setDefaultLocale(Locale.US);

	return slr;
}
 
Example #18
Source File: I18nConfig.java    From RuoYi with Apache License 2.0 5 votes vote down vote up
@Bean
public LocaleResolver localeResolver() {
    SessionLocaleResolver slr = new SessionLocaleResolver();
    // 默认语言
    slr.setDefaultLocale(Locale.SIMPLIFIED_CHINESE);
    return slr;
}
 
Example #19
Source File: I18nConfig.java    From LuckyFrameWeb with GNU Affero General Public License v3.0 5 votes vote down vote up
@Bean
public LocaleResolver localeResolver()
{
    SessionLocaleResolver slr = new SessionLocaleResolver();
    // 默认语言
    slr.setDefaultLocale(Locale.SIMPLIFIED_CHINESE);
    return slr;
}
 
Example #20
Source File: WebConfig.java    From auth-server with Apache License 2.0 5 votes vote down vote up
/**
 * <p>
 * Resolver for localizations.
 * </p>
 */
@Bean
public LocaleResolver localeResolver() {
  SessionLocaleResolver slr = new SessionLocaleResolver();
  slr.setDefaultLocale(Locale.US);
  return slr;
}
 
Example #21
Source File: AppConfig.java    From spring-boot-ddd with GNU General Public License v3.0 5 votes vote down vote up
@Bean
public LocaleResolver localeResolver() {
    SessionLocaleResolver slr = new SessionLocaleResolver();
    Locale ptBr = new Locale("pt", "BR");
    slr.setDefaultLocale(ptBr);
    return slr;
}
 
Example #22
Source File: LocaleConfig.java    From wolf with MIT License 5 votes vote down vote up
@Bean
public LocaleResolver localeResolver() {
    SessionLocaleResolver slr = new SessionLocaleResolver();
    // 默认语言
    slr.setDefaultLocale(Locale.CHINA);
    return slr;
}
 
Example #23
Source File: LocaleConfig.java    From SuperBoot with MIT License 5 votes vote down vote up
@Bean
public LocaleResolver localeResolver() {
    SessionLocaleResolver slr = new SessionLocaleResolver();
    // 默认语言
    slr.setDefaultLocale(Locale.SIMPLIFIED_CHINESE);
    return slr;
}
 
Example #24
Source File: UserDefinedLocaleHandlerInterceptor.java    From konker-platform with Apache License 2.0 5 votes vote down vote up
@Override
public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
    if (Optional.ofNullable(SecurityContextHolder.getContext().getAuthentication()).isPresent() &&
            SecurityContextHolder.getContext().getAuthentication().getPrincipal()
            instanceof User) {
        try {
            User user = (User) SecurityContextHolder.getContext().getAuthentication().getPrincipal();
            if (Optional.ofNullable(request.getSession()).isPresent()
                    && Optional.ofNullable(user).isPresent()) {
                if (!Optional.ofNullable(request.getSession()
                        .getAttribute(SessionLocaleResolver.LOCALE_SESSION_ATTRIBUTE_NAME)).isPresent() ||
                        !request.getSession().getAttribute(SessionLocaleResolver.LOCALE_SESSION_ATTRIBUTE_NAME)
                                .equals(user.getLanguage().getLocale())) {

                    request.getSession().setAttribute(
                            SessionLocaleResolver.LOCALE_SESSION_ATTRIBUTE_NAME,
                            user.getLanguage().getLocale());
                }
            }
        } catch (ClassCastException e){
            User invalidUSer = User.builder().name("INVALID USER").tenant(Tenant.builder().name("INVALID TENANT").build()).build();
LOG.error("Invalid user type on session", 
            		invalidUSer.toURI(),
            		invalidUSer.getTenant().getLogLevel(),
            		e);
        }

    }

    return super.preHandle(request, response, handler);
}
 
Example #25
Source File: AppConfiguration.java    From find with MIT License 5 votes vote down vote up
@Bean
public LocaleResolver localeResolver() {
    final SessionLocaleResolver sessionLocaleResolver = new SessionLocaleResolver();
    sessionLocaleResolver.setDefaultLocale(Locale.ENGLISH);

    return sessionLocaleResolver;
}
 
Example #26
Source File: IndexController.java    From pybbs with GNU Affero General Public License v3.0 5 votes vote down vote up
@GetMapping("changeLanguage")
public String changeLanguage(String lang, HttpSession session, HttpServletRequest request) {
    String referer = request.getHeader("referer");
    if ("zh".equals(lang)) {
        session.setAttribute(SessionLocaleResolver.LOCALE_SESSION_ATTRIBUTE_NAME, Locale.SIMPLIFIED_CHINESE);
    } else if ("en".equals(lang)) {
        session.setAttribute(SessionLocaleResolver.LOCALE_SESSION_ATTRIBUTE_NAME, Locale.US);
    }
    return StringUtils.isEmpty(referer) ? redirect("/") : redirect(referer);
}
 
Example #27
Source File: SpringWebApp.java    From hermes with Apache License 2.0 5 votes vote down vote up
@Override
public Locale getLocale() {
	Locale locale = Locale.class.cast(WebUtils.getSessionAttribute(request,
			SessionLocaleResolver.LOCALE_SESSION_ATTRIBUTE_NAME));
	if (locale == null) {
		locale = request.getLocale();
	}
	return locale;
}
 
Example #28
Source File: I18nConfig.java    From supplierShop with MIT License 5 votes vote down vote up
@Bean
public LocaleResolver localeResolver()
{
    SessionLocaleResolver slr = new SessionLocaleResolver();
    // 默认语言
    slr.setDefaultLocale(Locale.SIMPLIFIED_CHINESE);
    return slr;
}
 
Example #29
Source File: ApiDispatcherServletConfiguration.java    From flowable-engine with Apache License 2.0 4 votes vote down vote up
@Bean
public SessionLocaleResolver localeResolver() {
    return new SessionLocaleResolver();
}
 
Example #30
Source File: DispatcherServletConfiguration.java    From flowable-engine with Apache License 2.0 4 votes vote down vote up
@Bean
public SessionLocaleResolver localeResolver() {
    return new SessionLocaleResolver();
}