Open GoogleCodeExporter opened 9 years ago
req.getLocale() returns preferred locale from Accept-Language header so you can
still use
<condition name="Accept-Language">...</condition>
but you would have to parse the header
Having another type
<condition type="locale" />
would be great.
For now I'm using this workaround:
urlrewrite.xml:
<rule>
<condition type="cookie" name="_selectedLanguage" operator="equal">(en|cs)</condition>
<from>^/$</from>
<to last="true" type="temporary-redirect">%{context-path}/%{cookie:_selectedLanguage}/</to>
</rule>
<rule>
<from>^/$</from>
<run class="urlrewrite.LocaleSetter" />
<to last="true" type="temporary-redirect">%{context-path}/%{attribute:selectedLanguage}/</to>
</rule>
<rule>
<from>^/(en|cs)/$</from>
<set name="selectedLanguage">$1</set>
<set name="_selectedLanguage" type="cookie">$1:%{server-name}:2592000:%{context-path}/</set>
<to last="true">/faces/pages/index.xhtml</to>
</rule>
LocaleSetter.java:
package urlrewrite;
import java.util.Enumeration;
import java.util.HashSet;
import java.util.Locale;
import java.util.Set;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
public class LocaleSetter {
private static Set<String> languages = new HashSet<String>();
static {
languages.add("en");
languages.add("cs");
}
private static final String FALLBACK_LANGUAGE = "en";
@Override
public void run(HttpServletRequest request, HttpServletResponse response) {
Enumeration<?> locales = request.getLocales();
String selectedLanguage = null;
while ((selectedLanguage == null) && locales.hasMoreElements()) {
Locale current = (Locale) locales.nextElement();
String currentLang = current.getLanguage();
if (languages.contains(currentLang)) {
selectedLanguage = currentLang;
}
}
if (selectedLanguage == null) {
selectedLanguage = FALLBACK_LANGUAGE;
}
request.setAttribute("selectedLanguage", selectedLanguage);
}
}
My solution has a few advantages:
1. it searches in all locales given by the browser
2. it allows the user to switch the locale and stores the last used locale in a
cookie
Original comment by le.xi...@gmail.com
on 8 Jun 2012 at 2:48
Thanks for that workaround. I have taken parts of it and incorporated it into
my application.
On a side note, I decided to check out the source and see if I could add the
new locale variable myself. I believe I have the locale properly mapped. I am
attaching the patch to this comment.
Original comment by viper2...@gmail.com
on 13 Jun 2012 at 3:42
Attachments:
Original comment by p...@tuckey.org
on 2 Jul 2012 at 11:07
Original issue reported on code.google.com by
viper2...@gmail.com
on 8 Jun 2012 at 2:06