小编典典

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

jsp

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


阅读 338

收藏
2020-06-08

共1个答案

小编典典

检测语言

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

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对象不是特定的(无国家/地区),则可能要为每个受支持的非特定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。您将需要使用属于给定国家/地区的地址类准备一些数据库。该数据可以在Internet上找到。该解决方案的唯一问题是,用户可以配置Web代理,并在其实际位置上欺骗您的网站。另外,公司用户可能看起来好像他们是从美国连接的,而实际上是从英国或爱尔兰连接的。无论哪种方式,这都是您的最佳选择。

2020-06-08