依赖 setlocale 并依赖正在安装的语言环境是否可行?
我试图用 strftime
生成本地化日期字符串,我使用的占位符是 %x
。语言/区域设置为 setlocale(LC_ALL, array('jp','japanese'))
,但这两个区域设置都不可用,因此它生成了包含不正确字符的字符串。然后,我安装了 ja_JP.utf8 语言环境,并将其指定为数组中的第一个元素,并且解决了我遇到的日期格式问题。
我的问题是,我应该始终依赖安装的语言环境吗?我知道如何将它们安装在我有 root 访问权限的机器上,但如果我没有访问权限怎么办?
我相信 Zend_Locale
和 Zend_Translate
根本不依赖 setlocale
而是以某种方式在内部完成它,这给我的印象是它实际上并不依赖适合企业级应用。
我知道我可能可以在我的应用程序中使用 Zend_Locale
和 Zend_Translate
但它还需要支持 PHP4,至少再一年,这就是为什么我不能仅仅依赖这些。
I was trying to generate a localized date string with strftime
, the placeholder I use is %x
. The language/locale is setlocale(LC_ALL, array('jp','japanese'))
, however neither locale was available so it generated a string with improper characters. I then installed the ja_JP.utf8
locale and specified that as the first element in the array and the date formatting issue I had was resolved.
My question is, should I always rely on locales being installed? I'm aware of how to install them on boxes I have root access to, but what if I don't have access?
I believe Zend_Locale
and Zend_Translate
do not rely on setlocale
at all but somehow do it internally, which gives me the impression that it isn't practically feasible for enterprise level applications.
I know I could probably use Zend_Locale
and Zend_Translate
in my application but it also needs to support PHP4, at least for another year which is why I can't solely rely upon those.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
您可以检查 setlocale 的返回值,至少检查它是否已安装。否则你将会遇到无声的失败:
You can check the return value of setlocale and at least check that it was installed. Otherwise you will have silent failures:
如果我对 setlocale() 的检查是正确的,那么答案是:不,不。安装的区域设置的范围各不相同,其名称也各不相同,并且最终无法完全确定地预测特定区域设置的可用性。
If my examination of setlocale() is correct, the answer is: No and no. The range of installed locales varies, as does their name, and the availability of a certain locale ultimately cannot be predicted with total certainty.
可行,是的。明智,一点也不!
Feasible, yes. Wise, not at all!