Using WordPress ‘get_available_languages()’ PHP function

The get_available_languages() WordPress PHP function retrieves all available languages based on the presence of *.mo files in a given directory.

Usage

get_available_languages($dir = null);

Custom example:

$languages = get_available_languages();
print_r($languages);

Output:

Array
(
    [0] => en_US
    [1] => es_ES
    [2] => fr_FR
)

Parameters

  • $dir (string, optional) – A directory to search for language files. Default: WP_LANG_DIR (null)

More information

See WordPress Developer Resources: get_available_languages()

Examples

Retrieve available languages

This example retrieves the available languages and displays them.

$languages = get_available_languages();
foreach ($languages as $language) {
    echo $language . "<br>";
}

Retrieve available languages from a custom directory

This example retrieves the available languages from a custom directory and displays them.

$custom_dir = '/path/to/your/languages';
$languages = get_available_languages($custom_dir);
foreach ($languages as $language) {
    echo $language . "<br>";
}

Check if a specific language is available

This example checks if the Spanish language is available.

$languages = get_available_languages();
if (in_array('es_ES', $languages)) {
    echo "Spanish is available!";
} else {
    echo "Spanish is not available.";
}

Count available languages

This example counts the number of available languages.

$languages = get_available_languages();
echo "Total available languages: " . count($languages);

Display available languages with their native names

This example displays the available languages along with their native names.

$languages = get_available_languages();
foreach ($languages as $language) {
    $native_name = locale_get_display_language($language, $language);
    echo "$language - $native_name<br>";
}