@RequestMapping(value = "/changeLang", method = RequestMethod.GET) public String changeLocale( @RequestParam String language, HttpServletRequest req, HttpServletResponse resp) { LocaleResolver localeResolver = RequestContextUtils.getLocaleResolver(req); localeResolver.setLocale(req, resp, StringUtils.parseLocaleString(language)); return "redirect:/"; }
protected void service(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { final Locale locale = localeResolver.resolveLocale(request); LocaleContextHolder.setLocale(locale); ServletRequestAttributes requestAttributes = new ServletRequestAttributes(request); RequestContextHolder.setRequestAttributes(requestAttributes); try { super.service( new HttpServletRequestWrapper(request) { @Override public Locale getLocale() { return locale; } }, response); } finally { if (!locale.equals(LocaleContextHolder.getLocale())) { if (logger.isDebugEnabled()) { logger.debug("locale changed, updating locale resolver"); } localeResolver.setLocale(request, response, LocaleContextHolder.getLocale()); } LocaleContextHolder.resetLocaleContext(); RequestContextHolder.resetRequestAttributes(); } }
protected String getMessage(String code, Object[] args) { HttpServletRequest request = ((ServletRequestAttributes) RequestContextHolder.getRequestAttributes()).getRequest(); LocaleResolver localeResolver = RequestContextUtils.getLocaleResolver(request); Locale locale = localeResolver.resolveLocale(request); return getRes().getMessage(code, args, locale); }
@RequestMapping(value = "/", method = RequestMethod.GET) public String getTasksForPerformer( ModelMap model, @RequestParam(value = "tag", required = false) String tag, @RequestParam(value = "lang", required = false) String lang, HttpServletRequest request) { if (SecurityContextHolder.getContext().getAuthentication().getPrincipal() instanceof UserDetailsImpl) { model.put("ordersActive", true); model.put("tag", tag != null ? "#" + tag : ""); return "announcmentsForPerformer"; } else { if (lang != null) { return "redirect:/"; } List<LandingSlideModel> allSlidesPeople = landingSlideService.findAllSlidesPeopleModel( localeResolver.resolveLocale(request).getLanguage()); List<LandingSlideModel> allSlidesBusiness = landingSlideService.findAllSlidesBusinessModel( localeResolver.resolveLocale(request).getLanguage()); List<LandingSlideModel> allSlidesBenefits = landingSlideService.findAllSlidesIconsModel( localeResolver.resolveLocale(request).getLanguage()); Hibernate.initialize(allSlidesPeople); Hibernate.initialize(allSlidesBusiness); Hibernate.initialize(allSlidesBenefits); model.put("allSlidesPeople", allSlidesPeople); model.put("allSlidesBusiness", allSlidesBusiness); model.put("allSlidesBenefits", allSlidesBenefits); return "landing"; } }
/** * Initialize this context with the given request, using the given model attributes for Errors * retrieval. * * <p>Delegates to {@code getFallbackLocale} and {@code getFallbackTheme} for determining the * fallback locale and theme, respectively, if no LocaleResolver and/or ThemeResolver can be found * in the request. * * @param request current HTTP request * @param servletContext the servlet context of the web application (can be {@code null}; * necessary for fallback to root WebApplicationContext) * @param model the model attributes for the current view (can be {@code null}, using the request * attributes for Errors retrieval) * @see #getFallbackLocale * @see #getFallbackTheme * @see org.springframework.web.servlet.DispatcherServlet#LOCALE_RESOLVER_ATTRIBUTE * @see org.springframework.web.servlet.DispatcherServlet#THEME_RESOLVER_ATTRIBUTE */ protected void initContext( HttpServletRequest request, HttpServletResponse response, ServletContext servletContext, Map<String, Object> model) { this.request = request; this.response = response; this.model = model; // Fetch WebApplicationContext, either from DispatcherServlet or the root context. // ServletContext needs to be specified to be able to fall back to the root context! this.webApplicationContext = (WebApplicationContext) request.getAttribute(WEB_APPLICATION_CONTEXT_ATTRIBUTE); if (this.webApplicationContext == null) { this.webApplicationContext = RequestContextUtils.getWebApplicationContext(request, servletContext); } // Determine locale to use for this RequestContext. LocaleResolver localeResolver = RequestContextUtils.getLocaleResolver(request); if (localeResolver instanceof LocaleContextResolver) { LocaleContext localeContext = ((LocaleContextResolver) localeResolver).resolveLocaleContext(request); this.locale = localeContext.getLocale(); if (localeContext instanceof TimeZoneAwareLocaleContext) { this.timeZone = ((TimeZoneAwareLocaleContext) localeContext).getTimeZone(); } } else if (localeResolver != null) { // Try LocaleResolver (we're within a DispatcherServlet request). this.locale = localeResolver.resolveLocale(request); } // Try JSTL fallbacks if necessary. if (this.locale == null) { this.locale = getFallbackLocale(); } if (this.timeZone == null) { this.timeZone = getFallbackTimeZone(); } // Determine default HTML escape setting from the "defaultHtmlEscape" // context-param in web.xml, if any. this.defaultHtmlEscape = WebUtils.getDefaultHtmlEscape(this.webApplicationContext.getServletContext()); // Determine response-encoded HTML escape setting from the "responseEncodedHtmlEscape" // context-param in web.xml, if any. this.responseEncodedHtmlEscape = WebUtils.getResponseEncodedHtmlEscape(this.webApplicationContext.getServletContext()); this.urlPathHelper = new UrlPathHelper(); if (this.webApplicationContext.containsBean(REQUEST_DATA_VALUE_PROCESSOR_BEAN_NAME)) { this.requestDataValueProcessor = this.webApplicationContext.getBean( REQUEST_DATA_VALUE_PROCESSOR_BEAN_NAME, RequestDataValueProcessor.class); } }
/** 获得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); }
/** * Change the current locale to the specified one, storing the new locale through the configured * {@link LocaleResolver}. * * @param locale the new locale * @see LocaleResolver#setLocale * @see #changeLocale(java.util.Locale, java.util.TimeZone) */ public void changeLocale(Locale locale) { LocaleResolver localeResolver = RequestContextUtils.getLocaleResolver(this.request); if (localeResolver == null) { throw new IllegalStateException("Cannot change locale if no LocaleResolver configured"); } localeResolver.setLocale(this.request, this.response, locale); this.locale = locale; }
@RequestMapping(value = "/canviaIdioma", method = RequestMethod.GET) public String canviaIdioma( @RequestParam String idioma, HttpServletRequest request, HttpSession session, HttpServletResponse response) { LocaleResolver localeResolver = RequestContextUtils.getLocaleResolver(request); localeResolver.setLocale(request, response, new Locale(idioma)); session.setAttribute("data", obtenirData(localeResolver.resolveLocale(request))); return "redirect:/"; }
@Override public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception { Cookie[] cookies = request.getCookies(); boolean localeAlreadySetupInCookie = false; if (null != cookies) { Cookie cookie = null; for (int i = 0; i < cookies.length; i++) { cookie = cookies[i]; if ((null != cookie) && COOKIENAME_LOCALE.equals(cookie.getName())) { localeAlreadySetupInCookie = true; break; } } } if (!localeAlreadySetupInCookie) { // if locale not setup in cookie,means first-time to request. String acceptLanuage = request.getHeader("Accept-Language"); if (null != acceptLanuage) { if (acceptLanuage.toLowerCase().indexOf("zh") >= 0) { localeResolver.setLocale(request, response, Locale.SIMPLIFIED_CHINESE); } } } return true; }
@RequestMapping(value = "/index", method = RequestMethod.GET) public String index( @RequestParam(value = "locale", required = false) Locale locale, HttpServletRequest request, HttpServletResponse response) { if (locale != null) { localeResolver.setLocale(request, response, locale); } return "index"; }
/** {@inheritDoc} */ @Override public boolean preHandle( final HttpServletRequest request, final HttpServletResponse response, final Object handler) throws ServletException { String newLocale = request.getParameter(this.paramName); if (newLocale != null) { final LocaleResolver localeResolver = RequestContextUtils.getLocaleResolver(request); if (localeResolver == null) { throw new IllegalStateException( "No LocaleResolver found: not in a DispatcherServlet request?"); } if (!acceptedLocales.contains(newLocale)) { newLocale = getDefaultLocale(); } if (!request.getLocale().getLanguage().equals(new Locale(newLocale).getLanguage())) LOGGER.info("Switching to new Locale: " + newLocale); localeResolver.setLocale(request, response, StringUtils.parseLocaleString(newLocale)); } // Proceed in any case. return true; }
@Override public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws ServletException, EuropeanaQueryException { Language oldLocale = ControllerUtil.getLocale(request); String newLocale = request.getParameter(this.paramName); if (newLocale != null) { if (newLocale.contains("*")) { throw new EuropeanaQueryException(QueryProblem.UNABLE_TO_CHANGE_LANGUAGE.toString()); } 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(newLocale); localeResolver.setLocale(request, response, (Locale) localeEditor.getValue()); clickStreamLogger.logLanguageChange( request, oldLocale, ClickStreamLogger.UserAction.LANGUAGE_CHANGE); } // Proceed in any case. return true; }
public void doTag() throws JspException, IOException { Locale localLocale = _$3.resolveLocale(request()); if (this._$1.size() == 0) { String localObject = ResourceBundleHelper.getString(localLocale, this._$2); write((String) localObject); } else { String[] localObject = new String[this._$1.size()]; for (int i = 0; i < this._$1.size(); ++i) { String str2 = (String) this._$1.get(i); if ((str2 != null) && (str2.startsWith("message:"))) localObject[i] = ResourceBundleHelper.getString(localLocale, str2.substring("message:".length())); else localObject[i] = str2; } String str1 = ResourceBundleHelper.getString(localLocale, this._$2, localObject); write(str1); } }
@RequestMapping(value = "/lang", method = RequestMethod.GET) public String home( @RequestParam(value = "lang", required = false) String lang, Locale locale, Model model, HttpServletRequest request, HttpServletResponse response) { String leng2 = ""; if (lang.toLowerCase().equals("ko")) leng2 = "ko"; else if (lang.toLowerCase().equals("en")) leng2 = "en"; else leng2 = "en"; Locale locale2 = new Locale(leng2); localeResolver.setLocale(request, response, locale2); logger.info("locale {} -> {}", locale, locale2); return "redirect:/"; }
/*
private Locale getLocale(final HttpServletRequest request) { final Locale locale = localeResolver.resolveLocale(request); if (locale == null) return Locale.ENGLISH; if (!Constants.SUPPORTED_LOCALES.contains(locale.getLanguage())) return Locale.ENGLISH; return new Locale(locale.getLanguage()); }