栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 面试经验 > 面试问答

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

面试问答 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

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

检测语言

检测正确的语言很容易。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代理,并在其实际位置上欺骗您的网站。另外,公司用户可能看起来好像他们是从美国连接的,而实际上是从英国或爱尔兰连接的。无论哪种方式,这都是您的最佳选择。



转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/377398.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号