简体   繁体   中英

How to localize content on a java tomcat webserver?

I have inherited remote content and i would like to localize it. I am looking for the best architecture or if there is a easy logic to get it done in java that would be great. Here is the issue:

In my mobile app to retrieve content from a remote server i would call: https://mywebsite.com/contentFarm/static/images/storeFrontSign@2x.png

and it would return to me an image of a store front sign in english. However, if the user switches in the device to spanish i need to be able to get a spanish store front sign. I would have that image already loaded of course, but where to place it ?

I suppose i could use url paths to make web calls like this: https://mywebsite.com/contentFarm/static/images- spanish /storeFrontSign@2x.png

and https://mywebsite.com/contentFarm/static/images- english /storeFrontSign@2x.png

but is there a more cleaner way of localizing my server assets ? For example, a small java app on the server that reads header information to decide what to return etc. its java server, just need an architecture though.

you might try serverfault for this - you can actually evaluate the request headers in apache or nginx and do a forward there. is it just language, or is it language and country?

anyway - in apache, you could do this:

## Rewriting url for IT browser 
RewriteCond %{HTTP:Accept-Language} ^it [NC]
RewriteRule ^/$ http://my-site.com/it/foo-page [L,R=301]

## Rewriting url for EN browser
RewriteCond %{HTTP:Accept-Language} ^en [NC]
RewriteRule ^/$ http://my-site.com/en/foo-page [L,R=301]

... as long as you don't have too many languages.

If you DO have to use a servlet container, you might want to use a servlet filter:

import java.io.IOException;
import javax.servlet.Filter;
import javax.servlet.FilterChain;
import javax.servlet.FilterConfig;
import javax.servlet.ServletException;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;
import javax.servlet.http.HttpServletRequest;


public class Blah implements Filter {

    public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws ServletException, IOException {
        if(request instanceof HttpServletRequest) {
            HttpServletRequest hr = (HttpServletRequest)request;
            String lang = hr.getHeader("Accept-Language");

            String url = "the new url"; // decide where to send the person
            boolean needsForward = true; // decide wether or not to forward at all
            if(needsForward) {
                request.getRequestDispatcher(url).forward(request, response);
                return;
            }
        }
        chain.doFilter(request, response);
    }

    @Override
    public void destroy() {
    }

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

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