JavaScript 国际化字符串
在开发面向全球用户的web应用时,处理不同语言和地区的文本格式化是一项重要挑战。JavaScript提供了Intl
对象,它是ECMAScript国际化API的命名空间,可以帮助我们轻松地实现字符串、日期、数字等内容的国际化处理。
什么是国际化(i18n)?
国际化(Internationalization,简称i18n,因为首尾字母i和n之间有18个字母)是指设计和开发软件时考虑不同语言和地区的需求,使软件能够适应不同的语言和文化环境,而不需要进行工程上的修改。
提示
i18n是"internationalization"的缩写,因为这个单词首字母i和末字母n之间有18个字母,因此缩写为"i18n"。
Intl对象简介
JavaScript的Intl
对象提供了一系列构造函数,用于格式化数字、日期时间和字符串比较等操作,让你的应用能够支持世界各地的用户语言习惯。
主要的Intl
构造函数包括:
Intl.DateTimeFormat
- 用于日期和时间的格式化Intl.NumberFormat
- 用于数字的格式化Intl.Collator
- 用于字符串比较Intl.RelativeTimeFormat
- 用于相对时间格式化Intl.ListFormat
- 用于列表格式化Intl.DisplayNames
- 用于显示语言、地区、脚本和货币的本地化名称
接下来我们将一一介绍这些功能的使用方法。
日期和时间格式化:Intl.DateTimeFormat
Intl.DateTimeFormat
可以根据不同国家和地区的习惯来格式化日期和时间。
基本用法
const date = new Date();
// 使用默认语言环境格式化日期
const defaultFormatter = new Intl.DateTimeFormat();
console.log(defaultFormatter.format(date));
// 输出(美国): 7/15/2023
// 输出(中国): 2023/7/15
// 使用中文格式化日期
const chineseFormatter = new Intl.DateTimeFormat('zh-CN');
console.log(chineseFormatter.format(date));
// 输出: 2023/7/15
// 使用英文格式化日期
const englishFormatter = new Intl.DateTimeFormat('en-US');
console.log(englishFormatter.format(date));
// 输出: 7/15/2023
// 使用德文格式化日期
const germanFormatter = new Intl.DateTimeFormat('de-DE');
console.log(germanFormatter.format(date));
// 输出: 15.7.2023
自定义格式化选项
Intl.DateTimeFormat
构造函数接受第二个参数,允许你指定更多格式化选项:
const date = new Date();
// 显示完整的日期和时间
const fullDateTime = new Intl.DateTimeFormat('zh-CN', {
dateStyle: 'full',
timeStyle: 'full'
});
console.log(fullDateTime.format(date));
// 输出: 2023年7月15日星期六 中国标准时间 上午10:30:15
// 仅显示年月日
const dateOnly = new Intl.DateTimeFormat('zh-CN', {
year: 'numeric',
month: 'long',
day: 'numeric'
});
console.log(dateOnly.format(date));
// 输出: 2023年7月15日
// 仅显示时间
const timeOnly = new Intl.DateTimeFormat('zh-CN', {
hour: 'numeric',
minute: 'numeric',
second: 'numeric'
});
console.log(timeOnly.format(date));
// 输出: 上午10:30:15
数字格式化:Intl.NumberFormat
Intl.NumberFormat
可以按照各国习惯格式化数字,包括货币、百分比等。
基本用法
const number = 123456.789;
// 使用默认语言环境格式化数字
const defaultNumberFormat = new Intl.NumberFormat();
console.log(defaultNumberFormat.format(number));
// 输出(美国): 123,456.789
// 输出(中国): 123,456.789
// 使用中文格式化数字
const chineseNumberFormat = new Intl.NumberFormat('zh-CN');
console.log(chineseNumberFormat.format(number));
// 输出: 123,456.789
// 使用德文格式化数字
const germanNumberFormat = new Intl.NumberFormat('de-DE');
console.log(germanNumberFormat.format(number));
// 输出: 123.456,789
格式化货币
const amount = 123456.789;
// 格式化为人民币
const cnyFormatter = new Intl.NumberFormat('zh-CN', {
style: 'currency',
currency: 'CNY'
});
console.log(cnyFormatter.format(amount));
// 输出: ¥123,456.79
// 格式化为美元
const usdFormatter = new Intl.NumberFormat('en-US', {
style: 'currency',
currency: 'USD'
});
console.log(usdFormatter.format(amount));
// 输出: $123,456.79
// 格式化为欧元
const eurFormatter = new Intl.NumberFormat('de-DE', {
style: 'currency',
currency: 'EUR'
});
console.log(eurFormatter.format(amount));
// 输出: 123.456,79 €
格式化百分比
const percentage = 0.2345;
// 格式化为百分比
const percentFormatter = new Intl.NumberFormat('zh-CN', {
style: 'percent',
minimumFractionDigits: 1,
maximumFractionDigits: 2
});
console.log(percentFormatter.format(percentage));
// 输出: 23.5%