-
-
Save domdorn/a805f5c4be59c70ab9b3 to your computer and use it in GitHub Desktop.
jsf locale changer filter
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package com.dominikdorn.lyrix.filters; | |
import javax.servlet.*; | |
import javax.servlet.annotation.WebFilter; | |
import javax.servlet.http.Cookie; | |
import javax.servlet.http.HttpServletRequest; | |
import java.io.IOException; | |
import java.util.Locale; | |
/** | |
* Filter for changing the locale of a request if a language cookie is present. | |
*/ | |
public class LangFilter implements Filter { | |
@Override | |
public void init(FilterConfig filterConfig) throws ServletException { | |
// intentionally left blank as no initialization is needed | |
} | |
@Override | |
public void doFilter(ServletRequest servletRequest, ServletResponse servletResponse, FilterChain filterChain) throws IOException, ServletException { | |
if (servletRequest instanceof HttpServletRequest) { | |
HttpServletRequest req = (HttpServletRequest) servletRequest; | |
// implement the logic you want here... set the locale by cookie or URL param or whatever you like | |
Locale l = getSiteLang(req.getCookies()); | |
if (l != null) { | |
filterChain.doFilter(new LocaleWrapper(req, l), servletResponse); | |
} else { | |
filterChain.doFilter(servletRequest, servletResponse); | |
} | |
} else { | |
filterChain.doFilter(servletRequest, servletResponse); | |
} | |
} | |
@Override | |
public void destroy() { | |
} | |
/** | |
* Returns the preferred Locale based on the given cookies | |
* @param cookies an Array of Cookies | |
* @return the preferred Locale, null if no cookie is found | |
* or the cookies are null | |
*/ | |
public Locale getSiteLang(Cookie[] cookies) { | |
if(cookies == null) | |
return null; | |
for (Cookie c : cookies) { | |
if (c.getName().equals("siteLang")) { | |
if (c.getValue().equals("de")) { | |
return Locale.GERMAN; | |
} | |
if (c.getValue().equals("en")) { | |
return Locale.ENGLISH; | |
} | |
} | |
} | |
return null; | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package com.dominikdorn.dc.viewhandlers; | |
import javax.faces.application.ViewHandler; | |
import javax.faces.application.ViewHandlerWrapper; | |
import javax.faces.context.FacesContext; | |
import java.util.Locale; | |
/** | |
* This ViewHandler sets the Locale of the View to the locale given by the | |
* HttpServletRequest. This is usually done in a ServletFilter | |
* or a PhaseListener | |
*/ | |
public class LocaleViewHandler extends ViewHandlerWrapper { | |
ViewHandler parent; | |
public LocaleViewHandler(ViewHandler parent) { | |
this.parent = parent; | |
} | |
@Override | |
public ViewHandler getWrapped() { | |
return parent; | |
} | |
@Override | |
/** | |
* Simply delegates to the external Context (HttpServletContext) | |
*/ | |
public Locale calculateLocale(FacesContext context) { | |
return context.getExternalContext().getRequestLocale(); | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package com.dominikdorn.lyrix.filters; | |
import javax.servlet.http.HttpServletRequest; | |
import javax.servlet.http.HttpServletRequestWrapper; | |
import java.util.*; | |
/** | |
* Dominik Dorn | |
* http://dominikdorn.com | |
* http://twitter.com/domdorn | |
*/ | |
public class LocaleWrapper extends HttpServletRequestWrapper { | |
Locale l; | |
Enumeration<Locale> e; | |
public LocaleWrapper(HttpServletRequest request, Locale l) { | |
super(request); | |
this.l = l; | |
Enumeration xe = super.getLocales(); | |
List<Locale> list = new ArrayList<Locale>(); | |
list.add(l); | |
list.addAll(Collections.list(xe)); | |
e = Collections.enumeration(list); | |
} | |
@Override | |
public Locale getLocale() { | |
return l; | |
} | |
@Override | |
public Enumeration<Locale> getLocales() { | |
return e; | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?xml version='1.0' encoding='UTF-8'?> | |
<!-- =========== FULL CONFIGURATION FILE ================================== --> | |
<faces-config version="2.0" | |
xmlns="http://java.sun.com/xml/ns/javaee" | |
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" | |
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-facesconfig_2_0.xsd"> | |
<application> | |
<resource-bundle> | |
<base-name>messages</base-name> | |
<var>msg</var> | |
</resource-bundle> | |
<locale-config> | |
<default-locale>de</default-locale> | |
<supported-locale>de</supported-locale> | |
<supported-locale>en</supported-locale> | |
</locale-config> | |
<view-handler>com.dominikdorn.dc.viewhandlers.LocaleViewHandler</view-handler> | |
</application> | |
</faces-config> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?xml version="1.0" encoding="UTF-8"?> | |
<web-app | |
xmlns="http://java.sun.com/xml/ns/javaee" | |
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" | |
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd" | |
version="3.0"> | |
<filter> | |
<filter-name>Pretty Filter</filter-name> | |
<filter-class>com.ocpsoft.pretty.PrettyFilter</filter-class> | |
</filter> | |
<filter> | |
<filter-name>LangFilter</filter-name> | |
<filter-class>com.dominikdorn.lyrix.filters.LangFilter</filter-class> | |
</filter> | |
<filter-mapping> | |
<filter-name>Pretty Filter</filter-name> | |
<url-pattern>/*</url-pattern> | |
</filter-mapping> | |
<filter-mapping> | |
<filter-name>LangFilter</filter-name> | |
<url-pattern>/*</url-pattern> | |
<dispatcher>ERROR</dispatcher> | |
<dispatcher>REQUEST</dispatcher> | |
</filter-mapping> | |
</web-app> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment