kenny-hybris / urlrewritefilter

Automatically exported from code.google.com/p/urlrewritefilter
Other
0 stars 0 forks source link

Add locale condition/variable #108

Open GoogleCodeExporter opened 9 years ago

GoogleCodeExporter commented 9 years ago
In the conditions section, many different options are listed that are found in 
the HttpServletRequest but one is strangely missing: locale. I am requesting 
that the locale condition/variable be added. Here are a couple of examples of 
how this can be used:

Condition:
<rule>
    <from>^/english/(.*)$</from>
    <condition name="locale" operator="equal">en_US</condition>
    <to>/en/home</to>
</rule>

Variable:
<rule>
    <from>^/$</from>
    <to>/%{locale}/</to>
</rule>

Original issue reported on code.google.com by viper2...@gmail.com on 8 Jun 2012 at 2:06

GoogleCodeExporter commented 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

GoogleCodeExporter commented 9 years ago
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:

GoogleCodeExporter commented 9 years ago

Original comment by p...@tuckey.org on 2 Jul 2012 at 11:07