简体   繁体   中英

How do I detect user preferred language when showing my website , website is java jsp based

How do I detect user preferred language when showing my website , website is java jsp based. So I currently have

http://jthink.net/songkong/index.jsp

http://jthink.net/songkong/features.jsp

and was going to create a french version by recreating a french version in an fr subfolder

http://jthink.net/fr/songkong/index.jsp

http://jthink.net/fr/songkong/features.jsp

but do I ensure that if the user is using french as their computer language that they automaticaly get directed to the french version.

The trick is by working with "Accept-Language" sent by client (browser). Accept-Language is sent from the client in http header, and lets the server know the user's preferred language(s).

You will need to create a customer Filter (say MyFilter) and register it in web.xml.

public class MyFilter implements Filter {

    @Override
    public void init(FilterConfig config) throws ServletException {
        //
    }

    @Override
    public void doFilter(ServletRequest req, ServletResponse res, FilterChain chain) throws ServletException, IOException {
        HttpServletRequest request = (HttpServletRequest) req;          
        if(request.getParameter("language")==null) {
              String userLocale = request.getHeader("Accept-Language");
              Locale locale = request.getLocale();
              String requestURI = request.getRequestURI();

              // put your logic for userLocale and redirect accordingly          

             }
    }

    @Override
    public void destroy() {
        //
    }
}

Register in web.xml

<filter>
    <filter-name>localeFilter</filter-name>
    <filter-class>com.my.MyFilter</filter-class>
</filter>
<filter-mapping>
    <filter-name>localeFilter</filter-name>
    <url-pattern>/*</url-pattern>
</filter-mapping>

Update:

As suggested by @radimpe, the best practice will be using Persistent Cookie along with. This will help you, to trace, if some use wants to keep another language as default for your site

There are variety of ways in which you can do this :

I am assuming that user does not have to authenticate ?

Using Persistent Cookie for tracking the UPL - Disadvantages - when the user clears all the cookies manually - this preference is lost OR when the user is browsing in Private Mode then these cookies are not detected

IP Based - Assuming that there is no authentication for your website , you can do a predective Language detection by storing the IP and preference of the language. This again has the disadvantage - if the IP of the user changes

The combination of the two

The accept-language used alone , may be also a good criteria but then you would be binding the client to a specific language. It can be the starting point of determination

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM