【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."

猜你喜欢

转载自blog.csdn.net/yehuozhili/article/details/116482827