首页 > 其他分享 >Intl

Intl

时间:2024-05-25 23:34:43浏览次数:18  
标签:zh format Intl numeric Hans new

Intl.Collator 排序器的构造函数,可以支持对语言敏感的字符串比较

let list = [1, 2, 3, 10, 11, 12, 20, 21, 22, 100, 102, 103, 200, 201, 202, 130, 220];

// numeric: 是否按照数值进行比较
let collator = Intl.Collator(undefined, { numeric: true });

list.sort(); // [ 1, 10, 100, 102, 103, 11, 12, 130, 2, 20, 200, 201, 202, 21, 22, 220, 3 ]
list.sort(collator.compare); // [ 1, 2, 3, 10, 11, 12, 20, 21, 22, 100, 102, 103, 130, 200, 201, 202, 220 ]
let namelist = [
  '陈坤',
  '邓超',
  '杜淳',
  '冯绍峰',
  '韩庚',
  '胡歌',
  '黄晓明',
  '贾乃亮',
  '李晨',
  '李易峰',
  '鹿晗',
  '井柏然',
  '刘烨',
  '陆毅',
  '孙红雷'
];
namelist.sort(); // [ '井柏然', '冯绍峰', '刘烨', '孙红雷', '李易峰', '李晨', '杜淳', '胡歌', '贾乃亮', '邓超', '陆毅', '陈坤', '韩庚', '鹿晗', '黄晓明' ]
namelist.sort(new Intl.Collator('zh').compare); // [ '陈坤', '邓超', '杜淳', '冯绍峰', '韩庚', '胡歌', '黄晓明', '贾乃亮', '井柏然', '李晨', '李易峰', '刘烨', '陆毅', '鹿晗', '孙红雷' ]

Intl.DateTimeFormat 在特定的语言环境下格式化日期和时间

const format = new Intl.DateTimeFormat('zh-Hans', {
  year: 'numeric',
  month: '2-digit',
  // weekday: 'long',
  day: '2-digit',
  hour: '2-digit',
  minute: '2-digit',
  second: '2-digit',
  hour12: false
})

// 创建一个新的 DateTimeFormat 实例
const format2 = new Intl.DateTimeFormat('zh-CN', {
  year: 'numeric',
  month: 'long',
  day: 'numeric',
  hour: 'numeric',
  minute: 'numeric',
  second: 'numeric'
});

// 格式化当前日期和时间
format.format(new Date()); // '2024/05/25 17:06:02'
format2.format(new Date()); // '2024年5月25日 17:06:24'

Intl.ListFormat 排序器的构造函数,可以支持对语言敏感的字符串比较

// 让对语言敏感的列表进行格式化
var vehicles = ['Motorcycle', 'Bus', 'Car'];

var formatter = new Intl.ListFormat('en', { style: 'long', type: 'conjunction' });
console.log(formatter.format(vehicles)); // 'Motorcycle, Bus, and Car'

Intl.NumberFormat 根据不同语言环境最数值字符串进行不同的呈现处理

Intl.NumberFormat(undefined, {
  minimumFractionDigits: 10
}).format(123456789056.123); // '123,456,789,056.1230000000'

new Intl.NumberFormat('zh-Hans', {
  style: 'currency',
  currency: 'CNY',
  currencyDisplay: 'name'
}).format(12345.6789); // '12,345.68人民币'

'星期' + new Intl.NumberFormat('zh-Hans-CN-u-nu-hanidec').format(new Date().getDay()); // '星期六'

new Intl.NumberFormat('zh-Hans-CN-u-nu-hanidec', {
  style: 'currency',
  currency: 'CNY',
  currencyDisplay: 'name',
  useGrouping: false
}).format(1234); // '一二三四.〇〇人民币'

Intl.PluralRules 启用多个敏感格式和多个语言规则的对象

new Intl.PluralRules('ar-EG').select(0);
// → 'zero'
new Intl.PluralRules('ar-EG').select(1);
// → 'one'
new Intl.PluralRules('ar-EG').select(2);
// → 'two'
new Intl.PluralRules('ar-EG').select(6);
// → 'few'
new Intl.PluralRules('ar-EG').select(18);
// → 'many'

Intl.RelativeTimeFormat 相对时间的格式化

let rtf = new Intl.RelativeTimeFormat('zh', {
  numeric: 'auto'
});
// -1表示前一天
rtf.format(-1, 'day'); // '昨天'
rtf.format(1, 'day'); // '明天'
rtf.format(-365, 'day'); // '365天前'

Intl.getCanonicalLocales 规范语言环境名称的数组。重复项将被忽略,元素将被验证为结构上有效的语言标记

Intl.getCanonicalLocales('zh-hans');
// 结果是:["zh-Hans"]
Intl.getCanonicalLocales('zh');
// 结果是:["zh"]
Intl.getCanonicalLocales('zh-cmn-Hans-CN');
// 结果是:["cmn-Hans-CN"]
Intl.getCanonicalLocales('zh-cn');
// 结果是:["zh-CN"]
Intl.getCanonicalLocales('yue-hk');
// 结果是:["yue-HK"]
Intl.getCanonicalLocales('zh-some');
// 结果是:["zh-Some"]

标签:zh,format,Intl,numeric,Hans,new
From: https://www.cnblogs.com/chlai/p/18213154

相关文章

  • 3.Go 语言 定义变量、fmt 包、Print、Println、Go 语言注释
    Go语言定义变量、fmt包、Print、Println、Printf、Go语言注释1、Go语言定义变量这里我们为了演示代码期间给大家先简单介绍一下变量,后面的教程还会详细讲解。关于变量:程序运行过程中的数据都是保存在内存中,我们想要在代码中操作某个数据时就需要去内存上找到这个变量,但是......
  • print 与 println 的区别
    代码举例publicclassForDemo02{publicstaticvoidmain(String[]args){//循环打印1~1000之间能被5整除的数,并且每行打印3个for(inti=0;i<=1000;i++){if(i%5==0){System.out.print(i+"\t"); //相......
  • office the language DLL 'VBE7INTL.DLL' is not be found
    其实是缺少vba,安装组件就可以了解决方案:在卸载程序的面板中,找到office,右键选择更改,选择添加或删除功能,勾选如下两项就可以了记得关掉office后再打开就好了......
  • react 公司项目学习 【react+webpack+nw.js + mobx+react-intl 实现的多页面多语言win
    这几天突然要来个react项目;听说还比较复杂;项目组内就两个人会react还在忙,整组主要是用vue;这不,这种‘狗都不干’的事,被安排到我身上了,那就学吧;一、研究代码结构不得不说,这目录结构搞得有点复杂,算是我接触中除了乾坤和electron之外,相当复杂的了,慢慢阅读吧;看懂了,原来是react+web......
  • 在Android Studio上使用flutter Intl插件快速实现国际化和多国语言
    Flutter实现国际化和多语言支持在Flutter中实现国际化和多语言支持通常涉及以下步骤:添加依赖库:首先,你需要添加flutter_localizations依赖库到你的pubspec.yaml文件中。这个库包含了Flutter国际化所需的核心功能。dependencies:flutter:sdk:flutterflutter_localiza......
  • 再使用System.out.println()打印收拾东西回家
    一、前言今天没事干的时候,无意间点到了一个System.out.println()中的println()方法,一个闪亮的关键字冲击着我的眼睛。不知道大家知不知道,那就是加锁的synchronized。但凡有锁的地方肯定会性能是有损耗的,当然得在并发的情况下!synchronized在JDK8还是6之后进入了锁升级概念:无锁—>偏......
  • Java中print和println的区别
    Java中print()和println()的区别区别是print()输出完毕之后不换行,而println()输出完毕后会换行,所以println()在不输出任何东西的时候,就只输出一个换行符。示例如下:System.out.println("a");System.out.print("b");System.out.print("c");控制台输出结果:abc......
  • 在Java中print//printf//和println的区别
    1.printprint在JAVA中常常使用System.out.pirnt();的输出格式。在Java中进行一般的输出语句。例子如下: 输出 可见其不会换行。2.printfprintf在JAVA中常常使用System.out.printf();的格式。在Java中printf常用于格式转换,但需要注意不是换行输出,只用于精度转换。例子如......
  • ConstraintLayout(约束布局)
    1.规则layout_constraintLeft_toLeftOf当前View的右侧和另一个View的右侧位置对齐与RelativeLayout的alignLeft属性相似layout_constraintLeft_toRightOf当前view的左侧会在另一个View的右侧位置与RelativeLayout的toRightOf属性相似layout_constraintRight_toLeftOf当......
  • golang println与fmt.Println性能差距
    具体原因和底层细节没有仔细看,不做描述,只说一下结果。循环获取UDP的socket数据,满速情况下(每个数据包获取后输出一下当前接收数据包总数),println比fmt.Println慢了1倍。比如发送10万个包,fmt.Println可以接收到9万多,而println只能接收4万~5万......