Servlets - 国际化
在继续之前,让我先解释三个重要术语 −
国际化 (i18n) − 这意味着允许网站提供翻译成访问者语言或国籍的不同版本的内容
本地化 (l10n) − 这意味着向网站添加资源以适应特定的地理或文化区域。
语言环境 − 这是一个特定的文化或地理区域。它通常被称为语言符号,后跟国家/地区符号,中间用下划线分隔。例如,"en_US"代表美国的英语语言环境。
在构建全球网站时,有许多事项需要注意。本教程不会为您提供这方面的完整细节,但它会为您提供一个很好的例子,说明如何通过区分其位置(即语言环境)向互联网社区提供不同语言的网页。
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() 此方法返回此语言环境语言的三个字母的缩写。 |
示例
此示例显示如何显示请求的语言和相关国家/地区 −
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 { //获取客户端的区域设置 Locale locale = request.getLocale(); String language = locale.getLanguage(); String country = locale.getCountry(); // 设置响应内容类型 response.setContentType("text/html"); PrintWriter out = response.getWriter(); String title = "Detecting Locale"; String docType = "<!doctype html public \"-//w3c//dtd html 4.0 " + "transitional//en\">"; out.println(docType + "<html>" ++ "<head><title>" + title + "</title></head>" ++ "<body bgcolor = \"#f0f0f0\">" ++ "<h1 align = \"center\">" + language + "</h1>" ++ "<h2 align = \"center\">" + country + "</h2>" ++ "</body> </html>" ); } }
语言设置
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 { // 设置响应内容类型 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\">"; out.println(docType + "<html>" ++ "<head><title>" + title + "</title></head>" ++ "<body bgcolor = \"#f0f0f0\">" ++ "<h1>" + "En Español:" + "</h1>" ++ "<h1>" + "¡Hola Mundo!" + "</h1>" ++ "</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 { // 设置响应内容类型 response.setContentType("text/html"); PrintWriter out = response.getWriter(); //获取客户端的区域设置 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\">"; out.println(docType + "<html>" ++ "<head><title>" + title + "</title></head>" ++ "<body bgcolor = \"#f0f0f0\">" ++ "<h1 align = \"center\">" + date + "</h1>" ++ "</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 { // 设置响应内容类型 response.setContentType("text/html"); PrintWriter out = response.getWriter(); //获取客户端的区域设置 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\">"; out.println(docType + "<html>" ++ "<head><title>" + title + "</title></head>" ++ "<body bgcolor = \"#f0f0f0\">" ++ "<h1 align = \"center\">" + formattedCurr + "</h1>" ++ "</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 { // 设置响应内容类型 response.setContentType("text/html"); PrintWriter out = response.getWriter(); //获取客户端的区域设置 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\">"; out.println(docType + "<html>" ++ "<head><title>" + title + "</title></head>" ++ "<body bgcolor = \"#f0f0f0\">" ++ "<h1 align = \"center\">" + formattedPerc + "</h1>" ++ "</body> </html>" ); } }