获取本地化月份列表
由于 PHP 在 intl 扩展中使用来自 ICU 的数据,有没有办法从 PHP 中获取本地化的月份名称?
虽然我可以从 ICU 的 xml 文件获取数据并将它们提取到我自己的文件集中,但如果可能的话,我更喜欢干净且更简单的解决方案。
最后,在给定 intl 扩展名的区域设置的情况下,是否有方法获取日期格式(MM/DD/YYYY 或 DD/MM/YYYY)?
Since PHP uses data from ICU in the intl extension, are there ways to get localized month names from within PHP?
While I can get the data from ICU's xml files and extract them into my own set of files, I would much prefer a clean and easier solution if possible.
Finally, are there ways to get the date format (MM/DD/YYYY or DD/MM/YYYY), given the locale from the intl extension?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
不确定您需要月份名称的用途,但如果您只想使用月份的翻译名称,
strftime()
根据当前区域设置使用名称(使用%B
修饰符)。如果您想要一个月份名称列表用于其他用途,您也可以使用
strftime()
来实现:对于第二个问题,可能有一个本机函数,但您自己制作一个并不难:
Not sure what you need the month names for, but if you just want to use translated names for months,
strftime()
uses the names according to the current locale (use the%B
modifier).If you want a list of month names for some other use you can use
strftime()
for that too:For the second question there might be a native function for it but it's not hard to make one yourself:
您可以使用
IntlDateFormatter::getPattern
来获取模式吗?我不知道 strftime,但我会使用使用模式MMMM
进行格式化的建议来获取每个月的月份名称。看起来 php.intl 没有直接公开数据。Could you use
IntlDateFormatter::getPattern
to get the pattern? I don't know about strftime, but I would use the suggestion of formatting with a patternMMMM
to get the month name, through each month. Looks like php.intl doesn't expose the data directly.(这不是真正的答案,它是对 Steven R. Loomis 答案的评论。我写它只是因为我无法在注释中格式化代码)
谢谢 Steven R. Loomis,这是我的解决方案:
(This is not a real answer, it would be a comment to Steven R. Loomis' answer. I write it only because I can't format code in comments)
Thank you Steven R. Loomis, this is my solution:
对于英语或其他不使用赤纬的语言,它可以正常工作,但对于所有使用赤纬的语言,输出将无效。
对于波兰语,即
March
的输出将为marca
而不是marzec
对于俄语,即
March
的输出将为Мартa
而不是Март
此方法不适用于 所有 语言最有可能是必需的,并且可能不容易被不会说该语言的人测试。
如果您只需要月份名称,则最好使用:
请记住,区域设置更改可能会影响应用程序的其他部分。
For English or other languages that does not use declination it will work fine, but for all of them that use declination the output will not be valid.
For Polish i.e. the output for
March
will bemarca
instead ofmarzec
For Russian i.e. the output for
March
will beМартa
instead ofМарт
This method will not work through ALL languages which most probably is required and may not be easily tested by person not speaking the language.
If you only need names of the months it is better to use:
Please have in mind that locale change may impact other parts of your application.
等等
and so on