golang.org/x/text使用
2010 年 11 月 28 日
前言
go语言也已经使用一段时间了,但是关于golang的相关常用三方库仍然使用的不到位,正好前段时间接触了golang.org/x/text的库,这个包能强大的处理国际化和本地化,对应用的全球化时能帮上大忙。
包概览
golang.org/x/text 包含多层子包,提供了很多的工具和函数,并且用fmt风格的API来格式化字符串。
- cases
提供通用的方法
// code src := []string{ "hello world!", "i with dot", "'n ijsberg", "here comes O'Brian", } for _, c := range []cases.Caser{ cases.Lower(language.Und), cases.Upper(language.Turkish), cases.Title(language.Dutch), cases.Title(language.Und, cases.NoLower), } { fmt.Println() for _, s := range src { fmt.Println(c.String(s)) } } // output hello world! i with dot 'n ijsberg here comes o'brian HELLO WORLD! İ WİTH DOT 'N İJSBERG HERE COMES O'BRİAN Hello World! I With Dot 'n IJsberg Here Comes O'brian Hello World! I With Dot 'N Ijsberg Here Comes O'Brian
- cmd/gotext
gotext工具
// Usage: gotext command [arguments] // The commands are: update merge translations and generate catalog extract extracts strings to be translated from code rewrite rewrites fmt functions to use a message Printer generate generates code to insert translated messages
- collate
比较以及排序
- currency
包含与货币相关的功能
- date
- encoding
UTF-8编码
- feature/plural
用于处理文本中的语言复数
- internal
非导出包
- language
将用户首选语言列表与支持的语言列表相匹配
- message
实现了本地化字符串的格式化I/O,其功能类似于fmt的打印功能。 它是fmt的直接替代品。
- number
- search
- secure
- transform
- unicode