Do you think IIT Guwahati certified course can help you in your career?
No
Introduction
Let's consider a situation where our JSP application needs to support multiple languages. We can solve this issue most effectively by using Internationalization in JSP.
Internationalization ( i18n ) serves the website to users with different languages, including adjustment to date and time, formatting of numbers, etc. Web apps are accessible across the globe, and each country has a different language and formatting standards. For example, in some countries, the date is formatted as DD-MM-YYYY, whereas in other countries, it is written as MM-DD-YYYY.
Internationalization in JSP allows our application to adjust itself based on the locale, including formatting of currency, numbers, date and time, etc.
Internationalization in JSP
This article will discuss Internationalization in JSP. But before moving ahead, let's understand its common concepts:
Internationalization (i18n):− This means that the website will modify itself based on the region and country by translating its contents into the local language.
Localization (l10n): This means adding different resources to the website to adapt it to any particular region, such as Hindi/English translation.
Locale:− Locale is a particular geographical or cultural region. It is generally referred to as a language symbol followed by the country symbol separated by an underscore. For example, "hi_IN" represents the Hindi locale for India.
With JSP, we can pick up the appropriate version of our website based on the user's locale and provide the proper website version with local language, date and time format. Below is the method to request an object which returns the Locale object.
java.util.Locale request.getLocale()
You can also try this code with Online Java Compiler
In this example for detecting locale in JSP, we successfully printed the County Name and Language Name as shown in the image below:
Output
Internationalization in JSP - Example
In this section, we will see different examples of Internationalization in JSP.
Language-Specific Text Example
With JSP we can output a page written in any Western European language such as Spanish, English, French, German, Dutch, Italian, etc. Let’s understand this with the example given below:
In the above example, we have written the content of our web app in the Spanish language. Here, we have set the Content-Language header as Spanish to display all the characters correctly. The output produced by the above example in India Locale is given below:
Output
Here the output produced is the English translation of “Hola estudiantes” to “Hello students” and “En Español” to “in Spanish”.
Locale Specific Dates
We may use the java.text.DateFormat class and getDateTimeInstance() method to format date and time specific to different locale. Let’s understand how to format dates to any locale with the example given below:
In the above example, we have written the code to format the date based on the requester's locale. The output produced by the above example in United State Locale is given below:
Output
Locale Specific Currency
We may use the java.txt.NumberFormat class and getCurrencyInstance()method to format a number such as to different locale. Let’s understand how to format dates to any locale with the example given below:
In the above example, we have written the code to format currency based on the requester's locale. The output produced by the above example in United State Locale is given below:
Output
FAQs
Name the tools to help with the Internationalization of Strings in JSP? Few tools available in JSP to help with the internationalization of string is listed below: WebGettext: It provides a method for localizing strings in JSP in the Gettext style (i.e., keeping English in the source JSP). Regex2PotTask: Provides an Ant task that can find such strings in JSP and generate a Gettext POT (portable object template) file.
Important points to consider for Internationalization in JSP? Multiple items should be taken care of while building up a global Website. A few of them are listed below: We should always detect the user's locale and display contents appropriately. We should always format numbers, dates and times in appropriate format. We should always display the special characters using the HTML entities. For example, "¡" represents "¡", and "ñ" represents "ñ".
How to create a locale object in JSP? We can create locale objects in JSP directly by using the “request” class. For example,
request.getLocale();
You can also try this code with Online Java Compiler