Subdomain Internationalization in Spring Download

I am trying to create a web application to download spring (multi-lang).

Let's say user access from this domain is: "en.mywebsite.com/index.html" → English will start.

from this domain: "fr.mywebsite.com/index.html" → The French language will be launched.

How can i achieve this? I also looked at this blog, but there is no additional information about subdomains.

+4
source share
1 answer

Something like the following will do the trick.

public class SubDomainLocaleResolver extends AbstractLocaleResolver {


    @Override
    public Locale resolveLocale(HttpServletRequest request) {
        String domain = request.getServerName();
        String language = domain.substring(0, domain.indexOf('.'));
        Locale  locale = StringUtils.parseLocaleString(language);
        if (locale == null) {
            locale = determineDefaultLocale(request);
        }
        return locale != null ? locale : determineDefaultLocale(request);
    }

    protected Locale determineDefaultLocale(HttpServletRequest request) {
        Locale defaultLocale = getDefaultLocale();
        if (defaultLocale == null) {
            defaultLocale = request.getLocale();
        }
        return defaultLocale;
    }    

    @Override
    public void setLocale(HttpServletRequest request, HttpServletResponse response, Locale locale) {
        throw new UnsupportedOperationException("Cannot change sub-domain locale - use a different locale resolution strategy");

    }
}

, Locale , .

+2

All Articles