Content · Web Development · PHP

PHP ResourceBundle::getLocales() Method: Usage and Examples

Lists locale identifiers available inside a resource bundle. Requires the intl extension and an ICU data bundle; an unavailable bundle or locale should be handled explicitly.

1 views Published Oct 17, 2026 Updated Oct 17, 2026 admin

Lists locale identifiers available inside a resource bundle.

The examples focus on the operation itself, including the important distinction: ResourceBundle::create opens a bundle; getLocales lists locales stored in it.

What It Does


Use the examples below to observe the behavior on a small dataset before extending it to application code.

Syntax and Return Value


ResourceBundle::getLocales(...) — requires the corresponding optional extension

The displayed signature describes the API or identifies any correction or optional dependency. When a parameter is optional, supply it only when the task requires it.

Example 1: Basic Use


Supply the directory containing compiled ICU resource files, not a PHP array of translations. Requires the intl extension.

<?php
$bundleRoot = __DIR__ . "/icu-bundles";
$locales = ResourceBundle::getLocales($bundleRoot);
if ($locales === false) {
    throw new RuntimeException("Resource bundle could not be inspected");
}
foreach ($locales as $locale) {
    echo $locale . PHP_EOL;
}

This example is meant for a configured application environment; output depends on the installed extension, external service, or database contents.

Example 2: Checking Behavior


Supply the directory containing compiled ICU resource files, not a PHP array of translations. Requires the intl extension. This version demonstrates a separate way of consuming the locale list.

<?php
$bundleRoot = __DIR__ . "/icu-bundles";
$locales = ResourceBundle::getLocales($bundleRoot);
if ($locales === false) {
    throw new RuntimeException("Resource bundle could not be inspected");
}
foreach ($locales as $locale) {
    if (str_starts_with($locale, "en")) { echo $locale . PHP_EOL; }
}

This example is meant for a configured application environment; output depends on the installed extension, external service, or database contents.

Example 3: Practical Example


Supply the directory containing compiled ICU resource files, not a PHP array of translations. Requires the intl extension. This version demonstrates a separate way of consuming the locale list.

<?php
$bundleRoot = __DIR__ . "/icu-bundles";
$locales = ResourceBundle::getLocales($bundleRoot);
if ($locales === false) {
    throw new RuntimeException("Resource bundle could not be inspected");
}
foreach ($locales as $locale) {
    echo strtolower($locale) . PHP_EOL;
}

This example is meant for a configured application environment; output depends on the installed extension, external service, or database contents.

Important Behavior and Edge Cases


Requires the intl extension and an ICU data bundle; an unavailable bundle or locale should be handled explicitly.

This API is supplied by an optional extension that is not installed in the local test runtime, or requires a different PHP version. The example must be verified in a matching environment.

Comparison with Related Approaches


ApproachDifference
ResourceBundle::getLocalesLists locale identifiers available inside a resource bundle.
Related API or alternativeResourceBundle::create opens a bundle; getLocales lists locales stored in it.

Common Beginner Mistakes


MistakeCorrect approach
Ignoring the operation-specific limitsRequires the intl extension and an ICU data bundle; an unavailable bundle or locale should be handled explicitly.
Confusing it with a related APIResourceBundle::create opens a bundle; getLocales lists locales stored in it.

Key Takeaways


  • Lists locale identifiers available inside a resource bundle.
  • Requires the intl extension and an ICU data bundle; an unavailable bundle or locale should be handled explicitly.
  • ResourceBundle::create opens a bundle; getLocales lists locales stored in it.

Use ResourceBundle::getLocales when its documented behavior matches the problem. Test the relevant edge cases in your own runtime before deploying it.

Share this resourceSend this page to someone who may find it useful.