get_available_languages() WordPress Function

The get_available_languages() function is used to get an array of available languages for a site. This is useful for multilingual sites.

get_available_languages( string $dir = null ) #

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


Description

The default directory is WP_LANG_DIR.


Top ↑

Parameters

$dir

(string)(Optional)A directory to search for language files. Default WP_LANG_DIR.

Default value: null


Top ↑

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.


Top ↑

Source

File: wp-includes/l10n.php

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 ( 0 !== strpos( $lang_file, 'continents-cities' ) && 0 !== strpos( $lang_file, 'ms-' ) &&
				0 !== strpos( $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 );
}


Top ↑

Changelog

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

The content displayed on this page has been created in part by processing WordPress source code files which are made available under the GPLv2 (or a later version) license by theĀ Free Software Foundation. In addition to this, the content includes user-written examples and information. All material is subject to review and curation by the WPPaste.com community.