Java – Get Current User Locale

If you are working in a java application and you are asked to make the application customized for international end users; then information present in simple tutorial will help you in achieving i18n (internationalization). I am parting the information into two: first for web application and second for a standalone java desktop application.

To customize your application for international audience, you will need to know the location of your user and then format certain display part of application based on this location information. Location information in java is represented by Locale class.

Locale information can be retrieved in two ways:

1) Get current locale in java web applications

In java web applications, locale information is retrieved from ServletRequest ( and HttpServletRequest ) object obtained in server side. Use below method call for getting the current locale information:

	Locale currentLocale = httpServletRequest.getLocale();
	
	System.out.println(currentLocale.getDisplayLanguage());
	System.out.println(currentLocale.getDisplayCountry());

	System.out.println(currentLocale.getLanguage());
	System.out.println(currentLocale.getCountry());
	
	//Output:
	
	English
	United States
	en
	US

2) Get current locale in java desktop applications

In java desktop applications, locale information is retrieved using Locale.getDefault(); method call. you can also use system properties "user.country" and "user.language" as well for this information.

Locale currentLocale = Locale.getDefault();

System.out.println(currentLocale.getDisplayLanguage());
System.out.println(currentLocale.getDisplayCountry());

System.out.println(currentLocale.getLanguage());
System.out.println(currentLocale.getCountry());

System.out.println(System.getProperty("user.country"));
System.out.println(System.getProperty("user.language"));

Output:

English
United States
en
US
US
en

That’s all regarding this simple, easy but important information regarding getting locale information in java.

Happy Learning !!

Was this post helpful?

Join 7000+ Fellow Programmers

Subscribe to get new post notifications, industry updates, best practices, and much more. Directly into your inbox, for free.

1 thought on “Java – Get Current User Locale”

  1. Hello Sir/Ma’am,
    My server in USA BUT MY USER hit my site all country so
    i know about when request received by client then i know about
    country detail its where country

Comments are closed.

HowToDoInJava

A blog about Java and its related technologies, the best practices, algorithms, interview questions, scripting languages, and Python.