Servlets - 国际化( Internationalization)
在我们开始之前,让我解释一下三个重要的术语 -
Internationalization (i18n) - 这意味着使网站能够提供翻译成访问者语言或国籍的不同版本的内容
Localization (l10n) - 这意味着向网站添加资源以适应特定的地理或文化区域。
locale - 这是一个特定的文化或地理区域。 它通常被称为语言符号,后跟一个由下划线分隔的国家/地区符号。 例如,“en_US”表示美国的英语区域设置。
在建立全球网站时,应该注意许多项目。 本教程不会为您提供有关此内容的完整详细信息,但它会为您提供一个很好的示例,说明如何通过区分其位置(即区域设置)以不同语言向Internet社区提供您的网页。
servlet可以根据请求者的语言环境获取站点的相应版本,并根据本地语言,文化和要求提供适当的站点版本。 以下是返回Locale对象的请求对象的方法。
java.util.Locale request.getLocale()
检测区域设置
以下是重要的区域设置方法,您可以使用它们来检测请求者的位置,语言和当前区域设置。 以下所有方法都显示在请求者的浏览器中设置的国家名称和语言名称。
Sr.No. | 方法和描述 |
---|---|
1 | String getCountry() 此方法以ISO 3166 2字母格式为此区域设置返回大写的国家/地区代码。 |
2 | String getDisplayCountry() 此方法返回适合显示给用户的区域设置国家/地区的名称。 |
3 | String getLanguage() 此方法以ISO 639格式为此语言环境返回小写的语言代码。 |
4 | String getDisplayLanguage() 此方法返回适合显示给用户的语言环境语言的名称。 |
5 | String getISO3Country() 此方法返回此区域设置国家/地区的三个字母缩写。 |
6 | String getISO3Language() 此方法返回此语言环境语言的三个字母缩写。 |
例子 (Example)
此示例显示如何显示请求的语言和关联国家/地区 -
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 =
"<!doctype html public \"-//w3c//dtd html 4.0 " + "transitional//en\">\n";
out.println(docType +
"<html>\n" +
"<head><title>" + title + "</title></head>\n" +
"<body bgcolor = \"#f0f0f0\">\n" +
"<h1 align = \"center\">" + language + "</h1>\n" +
"<h2 align = \"center\">" + country + "</h2>\n" +
"</body>
</html>"
);
}
}
语言设置
servlet可以输出用西欧语言编写的页面,如英语,西班牙语,德语,法语,意大利语,荷兰语等。这里设置ContentLanguage标题以正确显示所有字符非常重要。
第二点是使用HTML实体显示所有特殊字符,例如,“&#241;” 代表“ñ”和“&#161;” 代表“¡”如下:
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 =
"<!doctype html public \"-//w3c//dtd html 4.0 " + "transitional//en\">\n";
out.println(docType +
"<html>\n" +
"<head><title>" + title + "</title></head>\n" +
"<body bgcolor = \"#f0f0f0\">\n" +
"<h1>" + "En Español:" + "</h1>\n" +
"<h1>" + "¡Hola Mundo!" + "</h1>\n" +
"</body>
</html>"
);
}
}
区域特定日期
您可以使用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 =
"<!doctype html public \"-//w3c//dtd html 4.0 " + "transitional//en\">\n";
out.println(docType +
"<html>\n" +
"<head><title>" + title + "</title></head>\n" +
"<body bgcolor = \"#f0f0f0\">\n" +
"<h1 align = \"center\">" + date + "</h1>\n" +
"</body>
</html>"
);
}
}
区域特定货币
您可以使用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 =
"<!doctype html public \"-//w3c//dtd html 4.0 " + "transitional//en\">\n";
out.println(docType +
"<html>\n" +
"<head><title>" + title + "</title></head>\n" +
"<body bgcolor = \"#f0f0f0\">\n" +
"<h1 align = \"center\">" + formattedCurr + "</h1>\n" +
"</body>
</html>"
);
}
}
区域特定百分比
您可以使用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 =
"<!doctype html public \"-//w3c//dtd html 4.0 " + "transitional//en\">\n";
out.println(docType +
"<html>\n" +
"<head><title>" + title + "</title></head>\n" +
"<body bgcolor = \"#f0f0f0\">\n" +
"<h1 align = \"center\">" + formattedPerc + "</h1>\n" +
"</body>
</html>"
);
}
}