在Java Servlet中自动为用户选择国家/地区和语言

问题描述 投票:7回答:2

我必须使用请求详细信息(IP地址,浏览器信息等)在Java Servlet中自动检测用户所在国家/地区和语言。是否可以为大多数用户检测这些设置(~90%)?

jsp servlets localization language-detection
2个回答
12
投票

检测语言

检测正确的语言很容易。 Web浏览器倾向于发送AcceptLanguage标头,Java Servlet API非常适合将其内容实际转换为Locale对象。您只需访问此信息并实施回退机制即可。为此,您实际上需要一个应用程序将支持的Locales列表(您可以考虑创建某种包含受支持的语言环境以及默认语言环境的属性文件)。以下示例显示了此类实现:

public class LousyServlet extends HttpServlet {
    private Properties supportedLanguages;
    private Locale requestLocale = (Locale) supportedLanguages.get("DEFAULT");

    public LousyServlet() {
        supportedLanguages = new Properties();
        // Just for demonstration of the concept
        // you would probably load it from i.e. XML
        supportedLanguages.put("DEFAULT", Locale.US);
        // example mapping of "de" to "de_DE"
        supportedLanguages.put("de-DEFAULT", Locale.GERMANY);
        supportedLanguages.put("de_AT", new Locale("de", "AT"));
        supportedLanguages.put("de_CH", new Locale("de", "CH"));
        supportedLanguages.put("ja_JP", Locale.JAPAN);
    }

    @Override
    protected void doGet(HttpServletRequest request,
            HttpServletResponse response) throws ServletException, IOException {
        detectLocale(request);

        super.doGet(request, response);
    }

    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        detectLocale(request);

        super.doPost(request, response);
    }

    private void detectLocale(HttpServletRequest request) {
        Enumeration locales = request.getLocales();
        while (locales.hasMoreElements()) {
            Locale locale = (Locale) locales.nextElement();
            if (supportedLanguages.contains(locale)) {
                requestLocale = locale;
                break;
            }
        }
    }

    public String getLanguage() {
        // get English name of the language
        // For native call requestLocale.getDisplayName(requestLocale)
        return requestLocale.getDisplayLanguage();
    }
}

请注意,您需要列出给定语言的所有国家/地区,因为在这种情况下它不会退回。这是因为这个原因。本地用户倾向于使用非特定的Locale(例如我的Web浏览器按顺序发送pl_PL,pl,en_US,en)。原因是,有些语言因国家而异,例如巴西葡萄牙语与葡萄牙语不同,繁体中文(台湾,香港)与简体中文(中国,新加坡)不同,不会适合回归其中一个。

检测国家

根据您需要此信息的内容,它可能也可能不是直截了当的。如果最终用户的Web浏览器配置正确,它将为您提供最终用户首选位置的提示 - 这将是Locale的一部分。如果您只需要该信息来决定要加载哪个本地化页面,那么这可能是最佳选择。当然,如果Locale对象不是特定的(无国家/地区),您可能希望为每个受支持的非特定区域设置分配“默认”国家/地区。在这两种情况下,您都应该为最终用户提供一些切换国家/地区的方式(即通过“其他国家/地区”组合框)。列表可以像这样获得:

public String[] getOtherCountries() {
    Set<String> countries = new HashSet<String>();
    Set<Object> keys = supportedLanguages.keySet();
    for (Object key : keys) {
        Locale other = (Locale) supportedLanguages.get(key);
        if (other != requestLocale) {
            countries.add(other.getDisplayCountry(requestLocale));
        }
    }

    return countries.toArray(new String[0]);
}

但是,如果您需要根据位置限制对内容的访问,则问题就更难了。您可以考虑检查IP。您需要准备一些具有属于给定国家/地区的地址类的数据库。这些数据可以在互联网上找到。该解决方案的唯一问题是,用户可能会配置Web代理并在其真实位置欺骗您的网站。此外,企业用户可能看起来好像他们从美国连接,实际上他们从英国或爱尔兰连接。无论哪种方式,这是你最好的一击。

之前有一些关于GeoLocation的问题,我相信你会觉得它很有用。 Here you are


1
投票

从请求对象中使用getlocale。

© www.soinside.com 2019 - 2024. All rights reserved.