有没有办法在 PHP 中获取可用语言环境的列表? [英] Is there a way to get the list of available locales in PHP?

查看:20
本文介绍了有没有办法在 PHP 中获取可用语言环境的列表?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在 Java 中,您可以调用 Locale.getAvailableLocales() 来获取可用语言环境的列表.

In Java, you can call Locale.getAvailableLocales() to get the list of available locales.

我期待 PHP Locale 类的等价物,但不能找一个.

I was expecting an equivalent from the PHP Locale class, but could not find one.

有没有办法获取所有有效区域设置的数组?

Is there a way to get an array of all valid Locales?

推荐答案

这里的部分困惑是 PHP 有两个被称为语言环境"的概念,它们几乎完全分开.

Part of the confusion here is that PHP has two concepts called "locale" that are pretty much totally separate.

第一个是较旧的,它基本上只是使用 C 语言环境功能.这就是 setlocale 背后的内容以及一些 PHP 函数中的语言环境支持(例如 money_format).这就是其他提到在命令行上运行 locale -a 和使用 setlocale 的答案.

The first is the older one, which basically just uses the C locale features. That's what's behind setlocale and the locale support in some of PHP's functions (like money_format for example). This is what other answers that mention running locale -a on the command line and using setlocale are talking about.

PHP 的 Locale 类和 其他相关功能来自 intl extension 较新,但工作方式不同.它没有使用 libc 语言环境的东西,而是使用一个名为 ICU 的库,该库提供自己的语言环境数据.PHP确实提供了一种方法来确定这个系统支持哪些语言环境:ResourceBundle::getLocales.这里的文档有点冗长,但是您可以将其作为静态方法调用并传递空白字符串以使用 ICU 的默认资源,从而获得 intl 支持的语言环境列表:

PHP's Locale class and the other related functionality from the intl extension is newer, and doesn't work the same way. Instead of using the libc locale stuff, it uses a library called ICU, which ships its own locale data. PHP does provide a method to determine which locales are supported by this system: ResourceBundle::getLocales. The documentation is a little wooly here, but you can call this as a static method and pass the blank string to use ICU's default resources, thus getting a list of the supported locales for intl:

ResourceBundle::getLocales('');

这篇关于有没有办法在 PHP 中获取可用语言环境的列表?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆