Use different locale for time.Format

xuanbao · · 922 次点击    
这是一个分享于 的资源,其中的信息可能已经有所发展或是发生改变。
<p>Hey there,</p> <p>I was wondering, is it possible to use a different locale when using time.Format? I would like to display the dates in my application in another language than English.</p> <p>Or should I just translate them myself for now?</p> <p>Cheers!</p> <hr/>**评论:**<br/><br/>tzmg: <pre><p>As far as I know this is not (yet) possible in the standard time library. A quick google gave me some hints about it being on the Roadmap but I can&#39;t find anything more on the subject. I did find a package <a href="https://github.com/mattbaird/go-i18n-formats" rel="nofollow">https://github.com/mattbaird/go-i18n-formats</a> but it doesn&#39;t look complete to me. Other i18n packages that I found seem to be only about translations tho...</p> <p>For now I would work with template functions to handle it...</p></pre>SerialMiller: <pre><p>Yeah, this is exactly what I&#39;ve found :). Hoped someone did have another answer though. Going to solve it like this for now :).</p></pre>jeffrallen: <pre><p>If the only thing about the locale you care about is the words for days of the week and months it seems like you could format to English then use string replacement to fix the words. It&#39;s a cheap trick, but for European languages it would work. The firmato string you use will have the other important part of the locale in it, i.e. the day/month/year order.</p></pre>SerialMiller: <pre><p>Definetly, this is now how I&#39;ve done it. Using a simple map[time.Month]string. Just was wondering if there was a simpeler way though :). Or if it was actually part of the standard Go packages, couldn&#39;t find it. Thanks for your input :).</p></pre>dchapes: <pre><p>I first read this as &#34;to use a different <em>location</em> when using <code>time.Format</code>&#34; and was all set to link to <a href="https://golang.org/pkg/time/#LoadLocation" rel="nofollow"><code>time.LoadLocation</code></a> and <a href="https://golang.org/pkg/time/#Time.In" rel="nofollow"><code>time.In</code></a> before I noticed it was &#34;locale&#34; :).</p> <p>Sadly the standard packages don&#39;t provide anything in the way of internationalization support for anything. :( I image if/when the Go project does any of this it would be added to the sub-repo repositories (like the <a href="https://godoc.org/golang.org/x/text" rel="nofollow"><code>golang.org/x/text</code> package</a>).</p></pre>SerialMiller: <pre><p>Most of my search queries in fact ended up at Location instead of locale, indeed :D.</p> <p>Can imagine the confusion.</p></pre>

入群交流(和以上内容无关):加入Go大咖交流群,或添加微信:liuxiaoyan-s 备注:入群;或加QQ群:692541889

922 次点击  
加入收藏 微博
暂无回复
添加一条新回复 (您需要 登录 后才能回复 没有账号 ?)
  • 请尽量让自己的回复能够对别人有帮助
  • 支持 Markdown 格式, **粗体**、~~删除线~~、`单行代码`
  • 支持 @ 本站用户;支持表情(输入 : 提示),见 Emoji cheat sheet
  • 图片支持拖拽、截图粘贴等方式上传