AppFuse
  1. AppFuse
  2. APF-141

Code for supporting Locale change using code

    Details

    • Type: Improvement Improvement
    • Status: Resolved
    • Priority: Major Major
    • Resolution: Fixed
    • Affects Version/s: 1.8, 1.8.1, 1.8.2
    • Fix Version/s: 1.9
    • Component/s: i18n
    • Labels:
      None

      Description

      Several people requested a feature to override the Locale auto-detect based on browser preferences. The following code is one way of changing the Locale by using a Filter and a RequestWrapper. This way to use this is when a user wants to change the language of the site by clicking on a flag in the main page, even though the browser's locale settings are different.

      /**
      *

      • @author Ivan Coro
      • @web.filter display-name="Locale Filter" name="localeFilter"
        */
        public class LocaleFilter implements Filter
        {
        public void init(FilterConfig filterConfig)
        {
        }

      public void destroy()
      {
      }
      public void doFilter(ServletRequest request, ServletResponse response,
      FilterChain chain)
      throws IOException, ServletException

      { chain.doFilter(new LocaleResponseWrapper((HttpServletRequest) request), response) }

      }

      public class LocaleResponseWrapper extends HttpServletRequestWrapper
      {
      public LocaleResponseWrapper(HttpServletRequest servletRequest)

      { super(servletRequest); }

      public Locale getLocale()

      { //here you need to add the logic to determine which locale to return. //In this particular case, I'm forcing the locale to be Spanish no matter //what the browser locale is (just as an example) return new Locale("es"); }

      public Enumeration getLocales()

      { //same as getLocale(), I'm forcing the locale to be Spanish only Vector l = new Vector(1); l.add(new Locale("es")); return l.elements(); }

      }

        Activity

        Hide
        Matt Raible added a comment -

        Used solution from APF-142 - thanks for the idea!

        Rather than having a form/action to do this - I just put in the Filter. If you pass in locale=fr (or something similar) to any link, the locale will be switched and persisted. If the locale is non-English, a link will show up to switch to English - the title on this link is $

        {webapp.name}

        in English.

        Show
        Matt Raible added a comment - Used solution from APF-142 - thanks for the idea! Rather than having a form/action to do this - I just put in the Filter. If you pass in locale=fr (or something similar) to any link, the locale will be switched and persisted. If the locale is non-English, a link will show up to switch to English - the title on this link is $ {webapp.name} in English.

          People

          • Assignee:
            Matt Raible
            Reporter:
            W. Ivan Coro
          • Votes:
            0 Vote for this issue
            Watchers:
            0 Start watching this issue

            Dates

            • Created:
              Updated:
              Resolved: