有没有办法在 PHP 中获取可用语言环境的列表?

2022-01-18 00:00:00 locale internationalization 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('');

相关文章