易于使用的Go应用程序i18n解决方案

网友投稿 662 2022-10-23

易于使用的Go应用程序i18n解决方案

易于使用的Go应用程序i18n解决方案

tr

Easy drop-in i18n solution for Go applications.

I was looking for a real easy way to provide i18n support for my Telegram bot, for which the data is pretty much a set of 20 different text messages. I couldn't find a single solution that would utilize the file system. Here's how tr works:

You have to create a locales directory, e.g. $ tree lang: lang├── en│   ├── hello.txt│   └── inner│   └── text.txt├── fr│   ├── hello.txt│   └── inner│   └── text.txt└── ru ├── hello.txt └── inner └── text.html6 directories, 6 files Your files could be of any extension, it doesn't really matter, since tr ignores extensions anyway. Init tr properly in your program:package mainimport ( "fmt" "os" "github.com/tucnak/tr")func init() { // tr.Init(localesDirectory, defaultLocale) if err := tr.Init("lang", "en"); err != nil { fmt.Println(err) os.Exit(1) }} Use simple syntax for i18n:// Inline syntax:fmt.Println("In English:", tr.Lang("en").Tr("hello"))fmt.Println("In French:", tr.Lang("fr").Tr("hello"))fmt.Println("In Russian:", tr.Lang("ru").Tr("hello"))// Shadowingtr := tr.Lang("fr")fmt.Println(tr.Tr("inner/text"))

Pass an optional third true argument to tr.Init() if you wish to trim all \ns from the end of the string returned.

版权声明:本文内容由网络用户投稿,版权归原作者所有,本站不拥有其著作权,亦不承担相应法律责任。如果您发现本站中有涉嫌抄袭或描述失实的内容,请联系我们jiasou666@gmail.com 处理,核实后本网站将在24小时内删除侵权内容。

上一篇:nyoj 803 A/B Problem(大数除小数&&大数求余小数)
下一篇:LaunchScreenSnapshot:保护应用程序快照中的敏感数据
相关文章

 发表评论

暂时没有评论,来抢沙发吧~