📅  最后修改于: 2020-11-12 05:46:56             🧑  作者: Mango
在继续之前,让我解释三个重要术语-
国际化(i18n) -这意味着使网站能够提供翻译成访客语言或国籍的不同版本的内容
本地化(l10n) -这意味着向网站添加资源以适应特定的地理或文化区域。
地区-这是一个特定的文化或地理区域。通常将其称为语言符号,后跟由下划线分隔的国家/地区符号。例如,“ en_US”代表美国的英语语言环境。
建立全球网站时,应注意许多事项。本教程不会为您提供完整的详细信息,但是会为您提供一个很好的示例,说明如何通过区分其位置(即区域设置)向互联网社区提供不同语言的网页。
Servlet可以根据请求者的语言环境获取适当的站点版本,并根据本地语言,文化和要求提供适当的站点版本。以下是请求对象的方法,该方法返回Locale对象。
java.util.Locale request.getLocale()
以下是重要的语言环境方法,可用于检测请求者的位置,语言以及语言环境。以下所有方法都显示在请求者的浏览器中设置的国家名称和语言名称。
Sr.No. | Method & Description |
---|---|
1 |
String getCountry() This method returns the country/region code in upper case for this locale in ISO 3166 2-letter format. |
2 |
String getDisplayCountry() This method returns a name for the locale’s country that is appropriate for display to the user. |
3 |
String getLanguage() This method returns the language code in lower case for this locale in ISO 639 format. |
4 |
String getDisplayLanguage() This method returns a name for the locale’s language that is appropriate for display to the user. |
5 |
String getISO3Country() This method returns a three-letter abbreviation for this locale’s country. |
6 |
String getISO3Language() This method returns a three-letter abbreviation for this locale’s language. |
此示例显示如何显示请求的语言和相关国家/地区-
import java.io.*;
import javax.servlet.*;
import javax.servlet.http.*;
import java.util.Locale;
public class GetLocale extends HttpServlet {
public void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
//Get the client's Locale
Locale locale = request.getLocale();
String language = locale.getLanguage();
String country = locale.getCountry();
// Set response content type
response.setContentType("text/html");
PrintWriter out = response.getWriter();
String title = "Detecting Locale";
String docType =
""-//w3c//dtd html 4.0 " + "transitional//en\">\n";
out.println(docType +
"\n" +
"" + title + " \n" +
"\n" +
"" + language + "
\n" +
"" + country + "
\n" +
"
"
);
}
}
Servlet可以输出用西欧语言编写的页面,例如英语,西班牙语,德语,法语,意大利语,荷兰语等。在这里,重要的是设置ContentLanguage标头以正确显示所有字符。
第二点是使用HTML实体显示所有特殊字符,例如“ñ”。代表“ñ”和“¡”表示“¡”,如下所示:
import java.io.*;
import javax.servlet.*;
import javax.servlet.http.*;
import java.util.Locale;
public class DisplaySpanish extends HttpServlet {
public void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
// Set response content type
response.setContentType("text/html");
PrintWriter out = response.getWriter();
// Set spanish language code.
response.setHeader("Content-Language", "es");
String title = "En Español";
String docType =
""-//w3c//dtd html 4.0 " + "transitional//en\">\n";
out.println(docType +
"\n" +
"" + title + " \n" +
"\n" +
"" + "En Español:" + "
\n" +
"" + "¡Hola Mundo!" + "
\n" +
"
"
);
}
}
您可以使用java.text.DateFormat类及其静态的getDateTimeInstance()方法来格式化特定于语言环境的日期和时间。以下是显示如何格式化特定于给定语言环境的日期的示例-
import java.io.*;
import javax.servlet.*;
import javax.servlet.http.*;
import java.util.Locale;
import java.text.DateFormat;
import java.util.Date;
public class DateLocale extends HttpServlet {
public void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
// Set response content type
response.setContentType("text/html");
PrintWriter out = response.getWriter();
//Get the client's Locale
Locale locale = request.getLocale( );
String date = DateFormat.getDateTimeInstance(DateFormat.FULL,
DateFormat.SHORT, locale).format(new Date( ));
String title = "Locale Specific Dates";
String docType =
""-//w3c//dtd html 4.0 " + "transitional//en\">\n";
out.println(docType +
"\n" +
"" + title + " \n" +
"\n" +
"" + date + "
\n" +
"
"
);
}
}
您可以使用java.txt.NumberFormat类及其静态的getCurrencyInstance()方法以特定于语言环境的货币格式化数字,例如long或double类型。以下是显示如何格式化特定于给定语言环境的货币的示例-
import java.io.*;
import javax.servlet.*;
import javax.servlet.http.*;
import java.util.Locale;
import java.text.NumberFormat;
import java.util.Date;
public class CurrencyLocale extends HttpServlet {
public void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
// Set response content type
response.setContentType("text/html");
PrintWriter out = response.getWriter();
//Get the client's Locale
Locale locale = request.getLocale( );
NumberFormat nft = NumberFormat.getCurrencyInstance(locale);
String formattedCurr = nft.format(1000000);
String title = "Locale Specific Currency";
String docType =
""-//w3c//dtd html 4.0 " + "transitional//en\">\n";
out.println(docType +
"\n" +
"" + title + " \n" +
"\n" +
"" + formattedCurr + "
\n" +
"
"
);
}
}
您可以使用java.txt.NumberFormat类及其静态的getPercentInstance()方法来获取特定于语言环境的百分比。以下是显示如何格式化特定于给定语言环境的百分比的示例-
import java.io.*;
import javax.servlet.*;
import javax.servlet.http.*;
import java.util.Locale;
import java.text.NumberFormat;
import java.util.Date;
public class PercentageLocale extends HttpServlet {
public void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
// Set response content type
response.setContentType("text/html");
PrintWriter out = response.getWriter();
//Get the client's Locale
Locale locale = request.getLocale( );
NumberFormat nft = NumberFormat.getPercentInstance(locale);
String formattedPerc = nft.format(0.51);
String title = "Locale Specific Percentage";
String docType =
""-//w3c//dtd html 4.0 " + "transitional//en\">\n";
out.println(docType +
"\n" +
"" + title + " \n" +
"\n" +
"" + formattedPerc + "
\n" +
"
"
);
}
}