以下内容引用自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. YOLO模型对图片中车辆的识别比对

    1,模型对比结果 ²        标准Yolo v3模型 ²        标准Yolo v3 tiny模型 ²        标准Yolo v2 tiny模型 ²        用户训练yolo ...

  2. Jenkins安装和初始化配置

    Jenkins是基于Java开发的一种持续集成工具,用于监控持续重复的工作,功能包括: 1.持续的软件版本发布/测试项目. 2.监控外部调用执行的工作. 所以安装Jenkins包含安装相应的jdk环境 ...

  3. cookie设置和读取以及获取超链接参数

    function setCookie(c_name, value, expiredays) { var exdate = new Date() exdate.setDate(exdate.getDat ...

  4. leetcode_951. Flip Equivalent Binary Trees_二叉树遍历

    https://leetcode.com/problems/flip-equivalent-binary-trees/ 判断两棵二叉树是否等价:若两棵二叉树可以通过任意次的交换任意节点的左右子树变为相 ...

  5. jQuery PC端图片预览,鼠标移上去查看大图

    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/ ...

  6. vsphere中的linux虚拟机安装vmware-tools

    先在vcenter中选中虚拟机点击安装这个工具,如图 然后这台linux虚拟机的控制台操作,挂载先建立挂载目录 cd /mnt #在挂载建一个用来挂载的文件. mkdir cdrom 使用mount命 ...

  7. linux远程开机

    它需要wakeonlan这个软件,     从何处得到它?     它的官方站是:http://sourceforge.net/projects/wake-on-lan/     如果使用rpm包可以 ...

  8. laravel-admin常见错误处理

    php artisan key:generate 新的laravle会有密钥不存在的问题,这时候我们执行这句话就可以生成秘钥了

  9. 网络编程 - 协议遇到IO自动切换

    一.协议遇到IO自动切换 python网络编程,遇到IO自动切换,通过模块gevent来实现: import gevent,time def g1(): print ("g1 is star ...

  10. 原生 js 上传图片

    js <!doctype html> <html> <head> <meta charset="utf-8"> <title& ...