get_available_languages( string $dir = null ): string[]

Gets all available languages based on the presence of *.mo files in a given directory.

Description

The default directory is WP_LANG_DIR.

Parameters

$dirstringoptional
A directory to search for language files.
Default WP_LANG_DIR.

Default:null

Return

string[] An array of language codes or an empty array if no languages are present.
Language codes are formed by stripping the .mo extension from the language file names.

Source

function get_available_languages( $dir = null ) {
	$languages = array();

	$lang_files = glob( ( is_null( $dir ) ? WP_LANG_DIR : $dir ) . '/*.mo' );
	if ( $lang_files ) {
		foreach ( $lang_files as $lang_file ) {
			$lang_file = basename( $lang_file, '.mo' );
			if ( ! str_starts_with( $lang_file, 'continents-cities' ) && ! str_starts_with( $lang_file, 'ms-' ) &&
				! str_starts_with( $lang_file, 'admin-' ) ) {
				$languages[] = $lang_file;
			}
		}
	}

	/**
	 * Filters the list of available language codes.
	 *
	 * @since 4.7.0
	 *
	 * @param string[] $languages An array of available language codes.
	 * @param string   $dir       The directory where the language files were found.
	 */
	return apply_filters( 'get_available_languages', $languages, $dir );
}

Hooks

apply_filters( ‘get_available_languages’, string[] $languages, string $dir )

Filters the list of available language codes.

Changelog

VersionDescription
4.7.0The results are now filterable with the 'get_available_languages' filter.
3.0.0Introduced.

User Contributed Notes

You must log in before being able to contribute a note or feedback.