Java 当他访问网站并以用户语言显示页面时,如何在jsp页面中获取用户语言环境

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/18656223/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-12 09:54:38  来源:igfitidea点击:

how get user locale in jsp page when he visit website and display page in user language

javajspinternationalization

提问by Developer Desk

i am trying to get user locale language when he visit my website and show website in that language.How to detect user locale and append to browser url like when we visit http://www.microsoft.comfrom india then it automatically display url as http://www.microsoft.com/en-in/default.aspxso for example my website name is http://www.myweb.comso how to make url as http://www.myweb.com?language=enon page load..? as i have set home.jsp as welcome file in my web.xml so when i run my web app from netbeans it shows localhost:8080/Myweb/ so how would user know that page is in english or dutch if i want to send page link to my frnd who is in netherland ? In short i want to display language parameter in URL when user open my website like http://www.myweb.com?language=nli.e automatically detect user locale and set that language as parameter

当他访问我的网站并以该语言显示网站时,我试图获取用户区域设置语言。如何检测用户区域设置并附加到浏览器 url 就像我们从印度访问http://www.microsoft.com然后它自动显示 url作为http://www.microsoft.com/en-in/default.aspx所以例如我的网站名称是http://www.myweb.com那么如何将 url 设为http://www.myweb.com?页面加载时的语言=en..?因为我已将 home.jsp 设置为 web.xml 中的欢迎文件,所以当我从 netbeans 运行我的 Web 应用程序时,它显示 localhost:8080/Myweb/ 所以如果我想发送页面,用户如何知道该页面是英语还是荷兰语链接到我在荷兰的朋友?简而言之,当用户打开我的网站时,我想在 URL 中显示语言参数http://www.myweb.com?language=nl即自动检测用户语言环境并将该语言设置为参数

here is my code....

这是我的代码....

  <%@page import="java.util.Locale"%>
  <%@ page pageEncoding="UTF-8" %>
  <%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
  <%@ taglib prefix="fmt" uri="http://java.sun.com/jsp/jstl/fmt" %>
  <%
   String userLocale = request.getHeader("Accept-Language");
   //out.println(userLocale);
   Locale locale = request.getLocale();
   String userlanguage = locale.getDisplayLanguage();
   out.println("LANG :" + locale.getLanguage());
  %>

  <c:set var="language" value="${not empty param.language ? param.language : not empty language ? language : pageContext.request.locale.language}" scope="session" />
  <fmt:setLocale value="${language}" />

 // here depending on user locale i am setting basename is this good approach?
  <c:choose>
  <c:when test="${language == 'nl'}">
    <fmt:setBundle basename="com.example.i18n.text_nl" />
  </c:when>
  <c:otherwise>
    <fmt:setBundle basename="com.example.i18n.text" />
  </c:otherwise>
  </c:choose>


   <html lang="${language}">
    <nav>
        <ul>
          <li>
              <a href="<c:url value='Aboutus.jsp?language=${language}'/>" 
                 title="<fmt:message key="nav.label.aboutus" />">
                 <fmt:message key="nav.label.aboutus" />
              </a>
         </li>
         <li class="shad">
              <a href="<c:url value='Contactus.jsp?language=${language}'/>"  // and also is this good way to append language as parameter to url here ?
                 title="<fmt:message key="nav.label.contactus" />">
                <fmt:message key="nav.label.contactus" />
             </a>
        </li>
       </ul>
   </nav>      
   </html>

采纳答案by Subin Sebastian

Write an HttpFilter to handle this and map it in web.xml.

编写一个 HttpFilter 来处理这个问题并将其映射到 web.xml 中。

 if(request.getParameter("language")==null) {
  String userLocale = request.getHeader("Accept-Language");
  Locale locale = request.getLocale();

  if(req.getRequestUrl().contains("?")) {
   response.sendRedirect(req.getRequestUrl()+"&language="_locale.getLanguage());
  } else {
   response.sendRedirect(req.getRequestUrl()+"?language="_locale.getLanguage());
  }
 }

回答by Azathoth

Didn't work for me (maybe it is outdated nowadays?)

对我不起作用(也许现在已经过时了?)

Here my updated version:

这是我的更新版本:

public void doFilter(ServletRequest request, ServletResponse response,
    FilterChain chain)
    throws IOException, ServletException {

    if (request instanceof HttpServletRequest) {
        HttpServletRequest req = (HttpServletRequest) request;
        HttpServletResponse res = (HttpServletResponse) response;

        if (request.getParameter("language") == null) {
            Locale locale = request.getLocale();

            String qs = req.getQueryString();
            if (qs == null || qs.isEmpty()) {
                res.sendRedirect(req.getRequestURI() + "?language=" + locale.getLanguage());
            } else {
                res.sendRedirect(req.getRequestURI() + "?" + qs + "&language=" + locale.getLanguage());
            }
        }
    }

    chain.doFilter(request, response);
}