前言
go语言也已经使用一段时间了,但是关于golang的相关常用三方库仍然使用的不到位,正好前段时间接触了golang.org/x/text的库,这个包能强大的处理国际化和本地化,对应用的全球化时能帮上大忙。
包概览
golang.org/x/text 包含多层子包,提供了很多的工具和函数,并且用fmt风格的API来格式化字符串。
- cases
提供通用的方法
// codesrc := []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)) }}// outputhello world!i with dot'n ijsberghere comes o'brianHELLO WORLD!İ WİTH DOT'N İJSBERGHERE COMES O'BRİANHello World!I With Dot'n IJsbergHere Comes O'brianHello World!I With Dot'N IjsbergHere Comes O'Brian
- cmd/gotext
gotext工具
// Usage:gotext command [arguments]// The commands are:update merge translations and generate catalogextract extracts strings to be translated from coderewrite rewrites fmt functions to use a message Printergenerate 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
参考
State of golang.org/x/text
手把手教你 Go 程序的国际化和本土化