1. Introduction
Handling language and country-specific data is essential for building applications that adapt to user geographical location. Java provides built-in classes, such as Locale
, ResourceBundle
, to support such features. In this tutorial, you will learn how to write applications that support internationalization and localization.
2. Internationalization and Localization
Internationalization is the process of designing and developing software in a way that makes it easy to adapt to different geographical locations, without code changes. You will often see the term i18n, when referring to internationalization. The number 18 refers to the number of deleted characters between the i and the n in the world internationalization.
On the other side, Localization refers to the process of adapting an internationalized application to a specific location. The short form for localization is l10n.
In Java, Localization can be achieved in the following ways:
- Using the Locale class to select the language and the region of the user
- Storing and Loading local-sensitive data(messages, labels, colors, images, etc.) using resource bundle classes like java.util.ResourceBundle.
- Formatting text, date, number, currency, and percentage to fit the user’s geographical location, culture, and behavior.
This tutorial focuses on the first point. The remaining points are covered in separate articles.
3. Obtaining a Locale object
3.1. Before Java 19
Before Java 19, You could create a Locale object using the following:
- Constructors
- Locale.Builder
- Factory Methods
- Constants(See the next section)
3.1.1. Locale Constructors
Up to Java 19, the following constructors are available in the Locale.class:
Locale(String language)
: The string argument is an ISO-639 language code, which is two letters lowercase. With this constructor, the country remains undefined.Locale(String language, String country)
: In addition to the language code, this constructor also takes a string representing the Country code. The country code is an ISO-3166 country code(two letters lowercase) or a UN M.49 three-digit area code.Locale(String language, String country, String variant)
: This constructor adds a variant attribute to the previous constructor.
Locale localeFromLanguage = new Locale("en");
Locale localeFromLanguageAndCountry = new Locale("en","US");
Locale localeFromLanguageAndCountryAndVariant = new Locale("en","US","POSIX");
3.1.2. Locale.Builder Utility Class
This utility will build a Locale object from values configured by setters. Unlike constructors, it will perform a syntax check on the input data.
Locale locale = new Locale.Builder()
.setLanguage("fr") // Set language to French
.setRegion("FR") // Set country/region to France
.build(); // Build the Locale object
Tip: Because of the check it performs on the input data, using the Builder class is the recommended approach for building a Locale object.
3.1.3. Factory Method forLanguageTag
You can use the factory method forLanguageTag(String languageTag) to construct a Locale object from a given language IETF BCP 47 tag.
Locale locale = Locale.forLanguageTag("en-US");
3.2. Since Java 19
The Locale constructors were deprecated in Java 19. Instead of using these constructors, Java encourages you to use the factory methods Locale.of()
. The use of factory methods comes with some performance improvement due to unnecessary object creation among other things.
Locale localeFromLanguage = Locale.of("fr");
Locale localeFromLanguageAndCountry = Locale.of("fr","FR");
However, the utility class Locale.Builder
is still the recommended way of creating a Locale, as it will perform syntax checks on the input.
4. Locale constants
Another way of obtaining a Locale object is by using the different constants available in the Locale class.
4.1. Constants for predefined Languages
Below is how you can create a Locale object using predefined languages:
Locale localeFromLanguage = Locale.ENGLISH;
Locale localeFromLanguage2 = Locale.FRENCH;
Note that with the above constants, the country remains undefined.
4.2. Constants for predefined Countries
You may also create a Locale using predefined countries. For some countries, you can have a combination of country name and language.
Locale localeFromCountry = Locale.FRANCE;
Locale localeFromCountry2 = Locale.US;
Locale localeFromCountryAndLanguage = Locale.CANADA_FRENCH;
5. Locale Class Available Methods
Some of the most used methods in the Locale class are:
- static Locale getDefault(): Returns the default locale on the Operating System
- static void setDefault(Locale locale): Change the default locale with the given value
- static Locale[] getAvailableLocale(): Returns an array of all installed locales
- String getCountry(): Returns the country of the current locale
- String getLanguage(): Returns the language of the current locale
Locale[] availableLocales = Locale.getAvailableLocales();
Locale defaultLocale = Locale.getDefault();
String country = defaultLocale.getCountry();
String language = defaultLocale.getLanguage();
6. Conclusion
In this article, you learned how to use the Locale class in Java. The Locale class is one of the multiple tools you can use to allow your application to adapt to different languages, regions, and cultural conventions effortlessly.
You can find the complete code of this article here in GitHub.
Pingback: Format Numbers In Java