如何以多种语言显示日期/时间?
我想以多种语言显示日期和时间(或单独的日期或时间)。特别是英语和西班牙语,但将来可能会出现其他语言。
我考虑过将 python 的 time.strftime 函数与 locale.setlocal 结合起来。我在阅读文档后排除了这一点(http://docs.python.org/library/locale .html),库不应该调用 locale.setlocal,所以我不应该只是“翻转”(更改,然后恢复)我的库中的区域设置。
我也考虑过 python 库 Babel,但我读了他们的文档 (http: //babel.edgewall.org/wiki/Documentation/0.9/dates.html)日期解析“未正确实现”,这让我担心。我确实需要日期解析能力。但是,Babel 可以使用不同的区域设置显示时间。
第三个[混合]选项是使用 pythons datetime 来解析日期,然后使用 Babel 以多种语言/区域设置显示日期。这有点混乱,因为 strptime 使用它自己的格式字符串语法,而 Babel 使用 LDML 语法作为其格式字符串。
可能还有其他选择。又是一个问题:以多种语言显示日期或时间的最佳方式是什么?
I would like to display the date and time (or either date or time, separately) in multiple languages. Specifically English and Spanish, but perhaps in other languages in the future.
I've considered pythons time.strftime function in combination with locale.setlocal. I ruled this out after reading in the docs (http://docs.python.org/library/locale.html) that libraries should not call locale.setlocal, so I shouldn't just "flip flop" (change, and then restore) the locale settings in my library.
I've also considered the python library Babel, but I read in their docs (http://babel.edgewall.org/wiki/Documentation/0.9/dates.html) that date parsing "isn't properly implemented" which concerns me. I do need date parsing abilities. However, Babel can display the time using different locales.
A third [hybrid] option would be to use pythons datetime to parse the date, and then Babel to display the date in multiple languages/locales. This is a little messy because strptime uses it's own format string syntax, while Babel uses LDML syntax for its format strings.
There may be other options. The question again: What is the best way to display a date or time in multiple languages?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我将使用 gettext 机制将翻译维护为标准 PO 文件。
对于每种语言,您都将翻译保存在专用文件中。您使用月份和日期名称作为msgid,并将特定翻译保留为msgstr。
http://docs.python.org/library/gettext.html
I would maintain the translations as standard PO file using the gettext mechanism.
For each language you keep the translations in a dedicated file. You use the month and day names as msgid and keep the specific translations as msgstr.
http://docs.python.org/library/gettext.html