Detecting browser locales and languages

We have a Java application that displays localized text in a user browser session based on the language settings of the browser. The application reads the browser language settings [is set in the header, which is supplied as in the request to the application], and prepares the localized text. But recently, we have encountered problems for the browser version of Mozilla 5.0. Please note that our code works fine in IE. As an example for the “ja” language, we expect the browser to send the accepted language as “ja-JP” [which IE], but, unfortunately, Mozilla (FF) does not - it only sends the accepted language in the browser as “ja” " Thus, we land on the creation of content using the default language.

Since we provide a fix for the same - basically something like a map [language code to language-Country code], for example - 'ja' to 'ja-JP', and then create a new language standard [only if there is a 2-digit language code after request] - my question concerns other browsers, such as
Chrome
Safari
etc.
What language formats are sent to the headers?
So, having an array, let's say this: ja-JP = ja-JP
ja = ja-JP
and mapping the browser language into country-country code will help solve the problem. But is there any specific limitation that we need to address - let's say there are languages ​​that are spoken in several places - if so, how do we deal with this?
Any other thing we should pay attention to?
Thanks in advance.

+4
source share
3 answers

Take a look at the java.util.ResourceBundle class, mainly the getBundle() method. (You can use this or implement a similar mechanism yourself.)

Basically, you have a hierarchy of locales, and whenever the locale is not specifically supported, you return to the parent language. In your case, "ja_JP" (in Java notation) has the parent language "ja" , which in turn has the parent language "" .

Since most Japanese pages are not specific to Japan, you usually do all of the Japanese translation for ja , and only when something special is valid only for Japanese users in Japan, then optionally ja_JP . Then you also have no problem if any user sends jp_US , since he uses Japanese in the USA.


If you want to use the Java ResourceBundle mechanism only to indicate for which locales we have data, you can create (for example) these (empty) files:

  • MyLocale.properties - matches the Locale ""
  • MyLocale_de.properties - corresponds to "de" Locale (German)
  • MyLocale_en.properties - Corresponds to "ru" Locale (English)
  • MyLocale_ja.properties - matches "ja" Locale (Japanese)

Then in your program you will write

 Locale rLocale = request.getLocale(); ResourceBundle bundle = ResourceBundle.getBundle("MyLocale", rLocale); Locale selectedLocale = bundle.getLocale(); 

Now, selectedLocale is definitely one of "", "de", "en", "jp", no matter what rLocale language was. For example, for "en", "en_GB", "en_US" in all cases, "en" will be selected, "ja" and "ja_JP" will result in "ja", while "de_DE" and "de_AT" will both result " de "and" it_IT "," eo "and most other locales will result in" ".


"The correct Java way": You will not ask your package about your locale, but just use the package as a ResourceBundle using localized resources. So, when you need some text, you do

  String text = bundle.getText("greeting.hello"); 

and then print the text. Sometimes you have to use MessageFormat to format text with values ​​inserted (or Formatter). (Then your property files would not be empty, of course, but they contain the following lines:

  greeting.hello = Hello World! 

(in a file in English)

  greeting.hello = Hallo Welt! 

(in the German file)


Please note that often the browser sends not only one Locale code, but also a list of preferred ones. Thus, you actually have to do this “search package” for each of these codes and transfer the first one that returns something else than “and return to” “only when the desired language is not requested. (For example , my browser sends "eo", "de_DE", "de", "en". Since most websites do not support Esperanto, they return to German (if available, and the selection is correct) or the default language (if they look only at the first record)).

+2
source

http://www.tutorialspoint.com/jsp/jsp_internationalization.htm

and more information about the request: http://www.apl.jhu.edu/~hall/java/Servlet-Tutorial/Servlet-Tutorial-Request-Headers.html

  package hall; import java.io.*; import javax.servlet.*; import javax.servlet.http.*; import java.util.*; /** Shows all the request headers sent on this * particular request. * <P> * Part of tutorial on servlets and JSP that appears at * http://www.apl.jhu.edu/~hall/java/Servlet-Tutorial/ * 1999 Marty Hall; may be freely used or adapted. */ public class ShowRequestHeaders extends HttpServlet { public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { response.setContentType("text/html"); PrintWriter out = response.getWriter(); String title = "Servlet Example: Showing Request Headers"; out.println(ServletUtilities.headWithTitle(title) + "<BODY BGCOLOR=\"#FDF5E6\">\n" + "<H1 ALIGN=CENTER>" + title + "</H1>\n" + "<B>Request Method: </B>" + request.getMethod() + "<BR>\n" + "<B>Request URI: </B>" + request.getRequestURI() + "<BR>\n" + "<B>Request Protocol: </B>" + request.getProtocol() + "<BR><BR>\n" + "<TABLE BORDER=1 ALIGN=CENTER>\n" + "<TR BGCOLOR=\"#FFAD00\">\n" + "<TH>Header Name<TH>Header Value"); Enumeration headerNames = request.getHeaderNames(); while(headerNames.hasMoreElements()) { String headerName = (String)headerNames.nextElement(); out.println("<TR><TD>" + headerName); out.println(" <TD>" + request.getHeader(headerName)); } out.println("</TABLE>\n</BODY></HTML>"); } public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { doGet(request, response); } } 
0
source

You can get the list of locales from the java class - Locale. By calling the method, getAvailableLocale ().

http://docs.oracle.com/javase/8/docs/api/java/util/Locale.html#getAvailableLocales--

0
source

Source: https://habr.com/ru/post/1340395/


All Articles