Automatically select country and language for user in Java Servlet

I need to automatically determine the country and language of the user in the Java Servlet using the request data (IP address, browser information, etc.). Is it possible to define these settings for most users (~ 90%)?

+7
source share
2 answers

Language detection

Finding the right language is easy. Web browsers typically send an AcceptLanguage header, and the Java Servlet API is so good that it actually converts content into Locale objects. All you need to do is simply access this information and implement a return mechanism. To do this, you really need a list of locales that your application will support (you could consider creating a kind of properties file that will contain supported locales, as well as the default). The example below shows this implementation:

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 ie 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(); } } 

Keep in mind that you will need to list all countries for a given language, as in this case it will not back off. For this reason. Local users, as a rule, do not have a specific Locale anyway (for example, my web browser sends pl_PL, pl, en_US, en in that order). And the reason is that there are some languages โ€‹โ€‹that differ significantly from country to country, for example, Brazilian Portuguese is different from Portuguese and Chinese traditional (Taiwan, Hong Kong) is different from Chinese simplified (China, Singapore), and it will not be to return to one of them.

Country discovery

Depending on what you need this information for, it may or may not be simple. If the end user's web browser is configured correctly, it will give you a hint about the preferred location of the end user - this will be part of Locale. If you only need this information to decide which localized page to load, this is probably the best option. Of course, if the Locale object is not specific (excluding the country), you might want to assign a default country for each supported non-specific locale. In both cases, you must provide end users with some ways to switch countries (i.e., using the "Other countries" field). The list can be obtained as follows:

 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]); } 

If you need it to restrict access to content by location, the problem is more complicated. You might consider checking IP. You will need to prepare a database with address classes belonging to this country. This data can be found on the Internet. The only problem with this solution is that the user can set up a web proxy and trick your website into its real location. In addition, corporate users may look as if they are connecting from the United States, where they are actually connecting from the UK or Ireland. In any case, this is your best shot.

There was a question about GeoLocation earlier, and I think you may find it useful. Here you are .

+12
source

use getlocale from the request object.

+1
source

All Articles