A page that renders different translations based on the language or country of the visitor

Source: Internet
Author: User
Tags dateformat locale

JspInternationalization

Before you begin, there are several important concepts that need to be explained:

    • Internationalization (i18n): Indicates that a page renders a different translated version based on the language or country of the visitor.
    • Localization (L10N): Add resources to the site to adapt it to different regions and cultures. For example, the Hindi version of the website.
    • Area: This is a specific area or culture that is usually considered to be a language sign and a national symbol connected by an underscore. For example, "en_US" represents the American English region.

If you want to build a global Web site, you need to be concerned about a range of projects. This chapter will show you in detail how to deal with internationalization issues and give some examples to deepen your understanding.

The JSP container is able to provide the correct page version based on the Locale property of the request. The next step is to get the syntax for the locale object through the Request object:

Detect locale

The following table lists the more important methods in the locale object that are used to detect the locale, language, and region of the request object. All of these methods will display the country name and language name in the browser:

Serial Number method & Description
1 String Getcountry ()

Returns the capitalization of the country/region code, or the area in ISO 3166 2-letter format

2 String Getdisplaycountry ()

Returns the name of the country to display to the user

3 String GetLanguage ()

Returns the English lowercase of the language code, or the area of the ISO 639 format

4 String Getdisplaylanguage ()

Returns the name of the language to be viewed by the user

5 String Getiso3country ()

Returns the 3-letter abbreviation of the country name

6 String Getiso3language ()

Returns the 3-letter abbreviation for the language name

Example Demo

This example shows us how to display the language and the country in the JSP:

<%@ page import= "java.io.*,java.util.locale"%><%@ page import= "javax.servlet.*,javax.servlet.http.*"%> <%    //Get client localization information    locale locale = Request.getlocale ();    String language = Locale.getlanguage ();    String country = locale.getcountry (); %>
Language Settings

JSP can use the Western European language to output a page, such as English, Spanish, German, French, Italian and so on. Thus, it is important to set the Content-language information header to display all characters correctly.

2nd, you need to use HTML character entities to display special characters, such as "& #241;", "?", "& #161;" On behalf of "?":

<%@ page import= "java.io.*,java.util.locale"%><%@ page import= "javax.servlet.*,javax.servlet.http.*"%> <%     //Set response content Type     response.setcontenttype ("text/html");     Set Spanish language code.     Response.setheader ("Content-language", "es");     String title = "En Espa?ol";  %>
Region-specific Dates

You can use the Java.text.DateFormat class and its static method Getdatetimeinstance () to format the date and time. The next example shows how to format the date and time according to the specified area:

<%@ page import= "java.io.*,java.util.locale"%><%@ page import= "javax.servlet.*,javax.servlet.http.*"%> <%@ page import= "java.text.dateformat,java.util.date"%><%     String title = "Locale specific Dates";     Get the client's locale locale     = Request.getlocale ();     String date = dateformat.getdatetimeinstance (                                   dateformat.full,                                    dateformat.short,                                    locale). Format (new Date ()); %>
Region-specific currency

You can use the Java.text.NumberFormat class and its static method Getcurrencyinstance () to format numbers. For example, long and double in a region-specific currency. The following example shows how to format the currency according to the specified region:

<%@ page import= "java.io.*,java.util.locale"%><%@ page import= "javax.servlet.*,javax.servlet.http.*"%> <%@ page import= "java.text.numberformat,java.util.date"%><%     String title = "Locale specific Currency";     Get the client's locale locale     = Request.getlocale ();     NumberFormat NFT = numberformat.getcurrencyinstance (locale);     String Formattedcurr = Nft.format (1000000); %>
Region-specific percentages

You can use the Java.text.NumberFormat class and its static method Getpercentinstance () to format percentages. The next example tells us how to format percentages according to the specified area:

<%@ page import= "java.io.*,java.util.locale"%><%@ page import= "javax.servlet.*,javax.servlet.http.*"%> <%@ page import= "java.text.numberformat,java.util.date"%><%     String title = "Locale specific Percentage";     //get the client ' s Locale     Locale locale = Request.getlocale ();     NumberFormat NFT = numberformat.getpercentinstance (locale);     String Formattedperc = Nft.format (0.51); %>

A page that renders different translations based on the language or country of the visitor

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.