Before starting, it's necessary to explain a few important concepts:
* **Internationalization (i18n):** Indicates that a page presents different translated versions based on the visitor's language or country.
* **Localization (l10n):** Adding resources to a website to adapt it to different regions and cultures. For example, the Hindi version of a website.
* **Locale:** This refers to a specific region or culture, typically considered as a language identifier and a country identifier connected by an underscore. For example, "en_US" represents the English (United States) locale.
If you want to build a global website, you need to pay attention to a series of items. This chapter will explain in detail how to handle internationalization issues and provide some examples to deepen understanding.
The JSP container can serve the correct version of the page based on the request's locale attribute. The following shows the syntax for obtaining a Locale object through the request object:
java.util.Locale request.getLocale()
* * *
## Detecting Locale
The following table lists the important methods in the Locale object used to detect the region, language, and locale of the request object. All these methods will display the country name and language name in the browser:
| **No.** | **Method & Description** |
| --- | --- |
| 1 | **String getCountry()** Returns the country/region code in uppercase English, or the region in ISO 3166 2-letter format. |
| 2 | **String getDisplayCountry()** Returns the country name to be displayed to the user. |
| 3 | **String getLanguage()** Returns the language code in lowercase English, or the region in ISO 639 format. |
| 4 | **String getDisplayLanguage()** Returns the language name to be shown to the user. |
| 5 | **String getISO3Country()** Returns the 3-letter abbreviation of the country name. |
| 6 | **String getISO3Language()** Returns the 3-letter abbreviation of the language name. |
* * *
## Example Demonstration
This example shows how to display the language and country in JSP:
Detecting LocaleDetecting Locale
<% out.println("Language : " + language + "
"); out.println("Country : " + country + "
"); %>
* * *
## Language Settings
JSP can output a page using Western European languages, such as English, Spanish, German, French, Italian, etc. Therefore, it is important to set the Content-Language header to display all characters correctly.
The second point is that HTML character entities need to be used to display special characters, such as "ñ" for Γ± and "¡" for Β‘:
* * *
## Locale-Specific Dates
The java.text.DateFormat class and its static method getDateTimeInstance() can be used to format dates and times. The following example shows how to format dates and times based on a specified locale:
* * *
## Locale-Specific Currency
The java.text.NumberFormat class and its static method getCurrencyInstance() can be used to format numbers, such as long and double types in locale-specific currencies. The following example shows how to format currency based on a specified locale:
* * *
## Locale-Specific Percentages
The java.text.NumberFormat class and its static method getPercentInstance() can be used to format percentages. The following example shows how to format percentages based on a specified locale: