+
+/**
+ * Get all available languages based on the presence of *.mo files in a given directory.
+ *
+ * The default directory is WP_LANG_DIR.
+ *
+ * @since 3.0.0
+ *
+ * @param string $dir A directory to search for language files.
+ * Default WP_LANG_DIR.
+ * @return array 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.
+ */
+function get_available_languages( $dir = null ) {
+ $languages = array();
+
+ foreach( (array)glob( ( is_null( $dir) ? WP_LANG_DIR : $dir ) . '/*.mo' ) 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;
+ }
+
+ return $languages;
+}
+
+/**
+ * Get installed translations.
+ *
+ * Looks in the wp-content/languages directory for translations of
+ * plugins or themes.
+ *
+ * @since 3.7.0
+ *
+ * @param string $type What to search for. Accepts 'plugins', 'themes', 'core'.
+ * @return array Array of language data.
+ */
+function wp_get_installed_translations( $type ) {
+ if ( $type !== 'themes' && $type !== 'plugins' && $type !== 'core' )
+ return array();
+
+ $dir = 'core' === $type ? '' : "/$type";
+
+ if ( ! is_dir( WP_LANG_DIR ) )
+ return array();
+
+ if ( $dir && ! is_dir( WP_LANG_DIR . $dir ) )
+ return array();
+
+ $files = scandir( WP_LANG_DIR . $dir );
+ if ( ! $files )
+ return array();
+
+ $language_data = array();
+
+ foreach ( $files as $file ) {
+ if ( '.' === $file[0] || is_dir( $file ) )
+ continue;
+ if ( substr( $file, -3 ) !== '.po' )
+ continue;
+ if ( ! preg_match( '/(?:(.+)-)?([A-Za-z_]{2,6}).po/', $file, $match ) )
+ continue;
+
+ list( , $textdomain, $language ) = $match;
+ if ( '' === $textdomain )
+ $textdomain = 'default';
+ $language_data[ $textdomain ][ $language ] = wp_get_pomo_file_data( WP_LANG_DIR . "$dir/$file" );
+ }
+ return $language_data;
+}
+
+/**
+ * Extract headers from a PO file.
+ *
+ * @since 3.7.0
+ *
+ * @param string $po_file Path to PO file.
+ * @return array PO file headers.
+ */
+function wp_get_pomo_file_data( $po_file ) {
+ $headers = get_file_data( $po_file, array(
+ 'POT-Creation-Date' => '"POT-Creation-Date',
+ 'PO-Revision-Date' => '"PO-Revision-Date',
+ 'Project-Id-Version' => '"Project-Id-Version',
+ 'X-Generator' => '"X-Generator',
+ ) );
+ foreach ( $headers as &$header ) {
+ // Remove possible contextual '\n' and closing double quote.
+ $header = preg_replace( '~(\\\n)?"$~', '', $header );
+ }
+ return $headers;
+}