在我们开始之前,先来看看三个重要术语:
当建立一个全球性的网站时有一些注意事项。本教程不会讲解这些注意事项的完整细节,但它会通过一个很好的实例向您演示如何通过差异化定位(即区域设置)来让网页以不同语言呈现。
Servlet 可以根据请求者的区域设置拾取相应版本的网站,并根据当地的语言、文化和需求提供相应的网站版本。以下是 request 对象中返回 Locale 对象的方法。
java.util.Locale request.getLocale()
下面列出了重要的区域设置方法,您可以使用它们来检测请求者的地理位置、语言和区域设置。下面所有的方法都显示了请求者浏览器中设置的国家名称和语言名称。
| 序号 | 方法 & 描述 |
|---|---|
| 1 | String getCountry() 该方法以 2 个大写字母形式的 ISO 3166 格式返回该区域设置的国家/地区代码。 |
| 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;charset=UTF-8");
PrintWriter out = response.getWriter();
String title = "检测区域设置";
String docType = "<!DOCTYPE html> \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
{
// 设置响应内容类型
response.setContentType("text/html;charset=UTF-8");
PrintWriter out = response.getWriter();
// 设置西班牙语言代码
response.setHeader("Content-Language", "es");
String title = "En Español";
String docType = "<!DOCTYPE html> \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
{
// 设置响应内容类型
response.setContentType("text/html;charset=UTF-8");
PrintWriter out = response.getWriter();
// 获取客户端的区域设置
Locale locale = request.getLocale( );
String date = DateFormat.getDateTimeInstance(
DateFormat.FULL,
DateFormat.SHORT,
locale).format(new Date( ));
String title = "特定于区域设置的日期";
String docType = "<!DOCTYPE html> \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
{
// 设置响应内容类型
response.setContentType("text/html;charset=UTF-8");
PrintWriter out = response.getWriter();
// 获取客户端的区域设置
Locale locale = request.getLocale( );
NumberFormat nft = NumberFormat.getCurrencyInstance(locale);
String formattedCurr = nft.format(1000000);
String title = "特定于区域设置的货币";
String docType = "<!DOCTYPE html> \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
{
// 设置响应内容类型
response.setContentType("text/html;charset=UTF-8");
PrintWriter out = response.getWriter();
// 获取客户端的区域设置
Locale locale = request.getLocale( );
NumberFormat nft = NumberFormat.getPercentInstance(locale);
String formattedPerc = nft.format(0.51);
String title = "特定于区域设置的百分比";
String docType = "<!DOCTYPE html> \n";
out.println(docType +
"<html>\n" +
"<head><title>" + title + "</title></head>\n" +
"<body bgcolor=\"#f0f0f0\">\n" +
"<h1 align=\"center\">" + formattedPerc + "</h1>\n" +
"</body></html>");
}
}
作为新的阿里云用户,您可以50免费试用多种优惠,价值高达1,700美元(或8,500美元)。这将让您了解和体验阿里云平台上提供的一系列产品和服务。如果您以个人身份注册免费试用,您将获得价值1,700美元的优惠。但是,如果您是注册公司,您可以选择企业免费试用,提交基本信息通过企业实名注册验证,即可开始价值$8,500的免费试用!本教程介绍了如何设置您的帐户并使用您的免费试用版。关于免费试用在我们开始此试用之前,您还必须遵守以下条款和条件才能访问您的免费试用:只有在一年内创建的账户才有资格获得阿里云免费试用。通过此免费试用优惠,用户可以免费试用免费试用活动页面上列出的每种产品一次。如果您有多个帐
我的rails.pt-BR.yml上有这个:br:errors:format:!'%{attribute}%{message}'messages:restrict_dependent_destroy:one:"Nãoépossívelexcluiroregistropoisexisteum%{record}dependente"many:"Nãoépossívelexcluiroregistropoisexistem%{record}dependentes"在我的模型中,我有这个:has_many:entities,dependent::restrict_with_error每当res
我想删除字符串中的非字母数字字符,但不删除国际字符,如重音字母。我也想保留空白。这是我目前所拥有的:the_string=the_string.gsub(/[^a-z0-9-]/i,'')虽然这确实会删除国际重音字母字符。我使用的解决方案:the_string=the_string.gsub(/[^\p{Alnum}\p{Space}-]/u,'')有效!谢谢。 最佳答案 您可以使用characterproperties这样做:the_string.gsub(/[^\p{Alnum}-]/,'')您可能还想使用\p{Space}来保
Spring国际化遇到的坑org.springframework.context.NoSuchMessageException:Nomessagefoundundercode‘xxx.xxxx’forlocale‘zh_CN’背景以前做的项目客户群体只有国内的客户,从来没有考虑过语言文字的问题。这次有一个需求的返回内容,要根据客户设置的语言返回不同的语言。尝试使用了以后,结果发现怎么都不好使,一直报的一个错误如下:org.springframework.context.NoSuchMessageException:Nomessagefoundundercode'user.name'forloc
只是在没有Rails环境的情况下让I18n工作有困难:irb>require'i18n'=>trueirb>I18n.load_path=Dir['/usr/lib/ruby/gems/1.9.1/gems/rails-i18n-0.6.6/rails/locale/en.yml']=>["/usr/lib/ruby/gems/1.9.1/gems/rails-i18n-0.6.6/rails/locale/en.yml"]irb>I18n.load_path+=Dir['/usr/lib/ruby/gems/1.9.1/gems/rails-i18n-0.6.6/rails/loca
如果我有这样的国际电话号码:0541754301我怎样才能格式化它来产生这样的东西:0541-754-301 最佳答案 您可以使用ActionView::Helpers::NumberHelper中的number_to_phone(number,options={})方法但是,文档指出此方法会将数字格式化为美国电话号码(例如(555)123-9876)。相反,您可以使用thispatch它增加了提供数字分组的能力::groupings-Specifiesalternategroupings(mustspecify3-elementa
我可以为每个验证本地化错误消息,但我如何为特定模型创建错误。普通语言环境看起来像这样:en:mongoid:errors:messages:taken:"Itisalreadytaken"但我想为user模型更改消息:en:mongoid:errors:messages:taken:"Itisalreadytaken"user:taken:"Itisalreadytaken.%{link_to'Rememberpassword',reset_password_path'}" 最佳答案 试试这个:en:mongoid:errors:m
我刚刚开始使用RSpec,我在RSpecgithub存储库上复制了非常简单的测试,以确保一切按预期工作:require'spec_helper'describe'HomePage'doit"Welcomestheuser"dovisit'/products'page.shouldhave_content("Welcome")endend当我将字符串更改为“Olá”或“Caçamba”之类的字符串时,问题就开始了。任何带有特殊字符的字符串。当我这样做时,出现以下错误:invalidmultibytechar(US-ASCII)(SyntaxError)invalidmultibytech
我有一个位于common/static/js/目录中的script.js文件。Common是一个目录,而不是一个应用程序,我需要将脚本中的消息翻译成其他语言。我应该如何配置我的Django项目?请基本说明,因为我很笨。 最佳答案 一种简单的方法是在模板级别设置您的可翻译值,以便您的JavaScript函数/类获取:varmy_name='{%transmy_name%}';但是Django确实有JavaScriptinternationalization. 关于使用Django实现Jav
我们正在寻求国际化网络应用程序。最好是在服务器端(用.net4C#编写)或客户端(Javascript)输出翻译?我们已经开始通过创建一个JS文件在客户端执行此操作,该文件包含一个包含英语短语作为键的对象(以便开发人员了解每条消息在上下文中的含义),值是显示的字符串给客户任何警报和提示。我们正在考虑将其扩展到整个前端的所有措辞。这是一个好主意还是最好在服务器端执行此类工作?更新:如果它有助于影响争论,我们不会在我们的网络应用程序中大量使用服务器端控件,我们的大部分控件都是基于jQuery/JS的。更新:此特定应用程序不公开可见(登录页面除外),因此SEO问题不适用。