以下内容引用自http://wiki.jikexueyuan.com/project/servlet/internationalization.html

三个重要术语:

  • 国际化(i18n):这意味着网站能够提供翻译成访问者的语言或国籍的不同版本的内容。

  • 本地化(l10n):这意味着向网站添加资源,使其适应特定的地理或文化区域,例如网站翻译成印地语。

  • 区域设置:这是一个特殊的文化或地理区域。它通常指语言符号后跟一个由下划线分隔的国家符号。例如"en_US"表示US的英语区域设置。

Servlet可以根据请求者的区域设置读出相应版本的网站,并根据当地的语言、文化和需求提供相应的网站版本。以下是Request对象中的方法,它返回了Locale对象。

java.util.Locale request.getLocale() 

一、检测区域设置

下面列出了重要的区域设置方法,可以使用它们来检测请求者的地理位置、语言和区域设置。下面所有的方法都显示了请求者浏览器中设置的国家名称和语言名称。

方法 描述

String getCountry()

该方法以2个大写字母形式的ISO3166格式返回该区域设置的国家/地区代码。

String getDisplayCountry()

该方法返回适合向用户显示的区域设置的国家的名称。

String getLanguage()

该方法以小写字母形式的ISO639格式返回该区域设置的语言代码。

String getDisplayLanguage()

该方法返回适合向用户显示的区域设置的语言的名称。

String getISO3Country()

该方法返回该区域设置的国家的三个字母缩写。

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
{
//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可以输出以西欧语言编写的页面,如英语、西班牙语、德语、法语、意大利语、荷兰语等。在这里,设置Content-Language头信息来正确的显示所有字符是非常重要的。

第二点是使用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&ntilde;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&ntilde;ol:" + "</h1>\n" +
"<h1>" + "&iexcl;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.text.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.text.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>");
}
}

测试工程:https://github.com/easonjim/5_java_example/tree/master/servletbasics/test18

Servlet实现国际化的更多相关文章

  1. Struts的拦截器

    Struts的拦截器 1.什么是拦截器 Struts的拦截器和Servlet过滤器类似,在执行Action的execute方法之前,Struts会首先执行Struts.xml中引用的拦截器,在执行完所 ...

  2. 22.struts2-拦截器.md

    目录 1.执行的流程时序图 1.执行的流程时序图 回顾: Struts配置: * 通配符.动态方法调用 * 全局跳转配置.配置的默认值.常量配置 * Struts核心业务 * 请求数据的自动封装 (p ...

  3. servlet中的过滤器 国际化

    1. 过滤器 基本概念 过滤器是需要在xml中配置的. 为什么需用到过滤器? 项目开发中,经常会涉及到重复代码的实现! 注册 ----à Servlet [1. 设置编码] ----à  JSP 修改 ...

  4. Servlet 国际化

    在我们开始之前,先来看看三个重要术语: 国际化(i18n):这意味着一个网站提供了不同版本的翻译成访问者的语言或国籍的内容. 本地化(l10n):这意味着向网站添加资源,以使其适应特定的地理或文化区域 ...

  5. 【Java EE 学习 35 上】【strus2】【类型转换器】【struts2和Servlet API解耦】【国际化问题】【资源文件乱码问题已经解决】

    一.类型转换器 1.在动作类action中,声明和表单中name属性的值同名的属性,提供get和set方法,struts2就可以通过反射机制,从页面中获取对应的内容 package com.kdyzm ...

  6. 小峰servlet/jsp(7)jstl国际化标签库、sql标签库等

    一.jstl国际化标签库: fmt:setLocale 设定用户所在的区域: fmt:formatDate   对日期进行格式化 fmt:requestEncoding 设置所有的请求编码; fmt: ...

  7. struts2国际化

    struts2国际化 1:什么是国际化? 国际化(internationalization)是设计和制造容易适应不同区域要求的产品的一种方式.它要求从产品中抽离所有的与语言,国家/地区和文化相关的元素 ...

  8. 学习SpringMVC——国际化+上传+下载

    每个星期一道菜,这个星期也不例外~~~ 一个软件,一个产品,都是一点点开发并完善起来的,功能越来越多,性能越来越强,用户体验越来越好……这每个指标的提高都需要切切实实的做点东西出来,好比,你的这个产品 ...

  9. Spring拦截机制之后端国际化心得

    需求 前端请求的header里带有Prefer_Lang参数,向后端传递国际化信息,后端需要在处理业务之前(建立拦截机制),将Prefer_Lang保存于线程上下文. 思路分析 初次接收该需求时,为了 ...

随机推荐

  1. axis2客户端的几种调用方式

    (1)使用RPC方式调用WebService // 使用RPC方式调用WebService RPCServiceClient serviceClient = new RPCServiceClient( ...

  2. 用 dojo/request/script 玩垮域

    dojo/request/script 可以用于向服务器发送跨域请求,如JSONP等.但单看官方文档有点不容易理解,特将体会记录. require(["dojo/request/script ...

  3. VBox虚拟机安装debian

    决定在win7上装一个Linux虚拟机用作Linux开发学习,虽然win7下已经有了Cygwin,还是想在一个比较完整的环境下.前面装过Ubuntu发现界面太笨重了,考虑重新换一个,同时比较喜欢apt ...

  4. JavaScript——class与原型对象

    原型对象的意义 通过new 一个构造函数,我们能够获得一个实例,在new 的过程中,程序会在内存中申请一块区域,同时我们可以加参数,所以每个对象都不一样. 原型对象则是同一个构造函数 new 出来的所 ...

  5. Farseer.net轻量级开源框架 中级篇:SQL执行报告

    导航 目   录:Farseer.net轻量级开源框架 目录 上一篇:Farseer.net轻量级开源框架 中级篇: 数据库切换 下一篇:Farseer.net轻量级开源框架 中级篇: 探究ORM(M ...

  6. js 作用域 ?????

    ///*第一种情况 */ //var mycars = new Array() //mycars[0] = 0; //mycars[1] = 1; //mycars[2] = 2; //functio ...

  7. 类unix系统 递归删除指定文件

    递归删除当前目录下所有以 ._开头的文件 find . -name "._*" | xargs rm -f 或者: find . -name "._*" -ex ...

  8. Linux内核中TCP SACK机制远程DoS预警通告

    漏洞描述 2019年6月18日,RedHat官网发布报告:安全研究人员在Linux内核处理TCP SACK数据包模块中发现了三个漏洞,CVE编号为CVE-2019-11477.CVE-2019-114 ...

  9. controller,sevices层,java初步了解

    一.controller层 二.service层 1.接口 2.接口的实现 转换 ClearingAccountArgument对象

  10. JavaSE-15 Log4j参数详解

    一:日志记录器输出级别,共有5级(从前往后的顺序排列) ①fatel:指出严重的错误事件将会导致应用程序的退出 ②error:指出虽然发生错误事件,但仍然不影响系统的继续运行 ③warn:表明会出现潜 ...