【icu】icu信息语法

前言

  • 我在查询i18n时看见文档上科普icu信息语法。据文档称该语法被广泛用在c , c++,java与php中。
  • icu是 International Components for Unicode 的缩写。

ICU信息语法官网

  • https://unicode-org.github.io/icu/userguide/format_parse/numbers/skeletons.html

{key, type, format}

  • icu信息语法最重要的是格式化概念,他不仅可以插一个参数来替换插值,剩下2个插值一个用来表示其模型(这个是有内部实现),一个用来以某种方式格式化其插值(和上一个参数模型有关)。
  • 例如:
I have {numCats, number} cats.
Almost {pctBlack, number, ::percent} of them are black.

{key, select, matches}

  • 可以作为条件判断选择使用,例如:
{gender, select,
    male {He}
    female {She}
    other {They}
} will respond shortly.
  • 甚至进行嵌套:
{taxableArea, select,
    yes {An additional {taxRate, number, percent} tax will be collected.}
    other {No taxes apply.}
}

{key, plural, matches}

  • 复数格式的存在是因为很多语言会对复数处理不太一样。
  • 例如:
Cart: {itemCount} {itemCount, plural,
    one {item}
    other {items}
}
You have {itemCount, plural,
    =0 {no items}
    one {1 item}
    other {{itemCount} items}
}.
  • 使用#可以格式化为{key,number}
You have {itemCount, plural,
    =0 {no items}
    one {# item}
    other {# items}
}.

{key, selectordinal, matches}

  • 和上面复数有点像,也是个类似复数的表现:
It's my cat's {year, selectordinal,
    one {#st}
    two {#nd}
    few {#rd}
    other {#th}
} birthday!
  • 不同之处是这种语法会被映射到一个map

富文本格式化

  • icu支持富文本,但是这个像html的标签并不是xml或者html标签。
Our price is <boldThis>{price, number, ::currency/USD precision-integer}</boldThis>
with <link>{pct, number, ::percent} discount</link>

转义

  • 语法必然会有转义 使用’进行转义
"This is not an interpolation: '{word}"
//→ "This is not an interpolation: {word}"
"These are not interpolatons: '{word1} {word2}'"
//→ "These are not interpolatons: {word1} {word2}"
"'<notATag>"
//→ "<notATag>"
"'<notATag>hello</notATag>'"
//→ "<notATag>hello</notATag>"
  • 多重转义
"This '{isn''t}' obvious."
//→ "This {isn't} obvious."
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

业火之理

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值