Make WordPress Core


Ignore:
Timestamp:
09/20/2013 07:12:45 PM (13 years ago)
Author:
nacin
Message:

Send installed language data to the plugin and theme update-check endpoints.

Introduces wp_get_installed_language_data() and wp_get_pomo_file_data().

see #18200.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/src/wp-includes/l10n.php

    r25493 r25520  
    626626        return $languages;
    627627}
     628
     629/**
     630 * Get installed language data.
     631 *
     632 * Looks in the wp-content/languages directory for translations of
     633 * plugins or themes.
     634 *
     635 * @since 3.7.0
     636 *
     637 * @param string $type What to search for. Accepts 'plugins', 'themes'.
     638 * @return array Array of language data.
     639 */
     640function wp_get_installed_language_data( $type ) {
     641        if ( $type !== 'themes' && $type !== 'plugins' )
     642                return array();
     643
     644        if ( ! is_dir( WP_LANG_DIR ) || ! is_dir( WP_LANG_DIR . "/$type" ) )
     645                return array();
     646
     647        $files = scandir( WP_LANG_DIR . "/$type" );
     648        if ( ! $files )
     649                return array();
     650
     651        $language_data = array();
     652
     653        foreach ( $files as $file ) {
     654                if ( '.' === $file[0] || is_dir( $file ) )
     655                        continue;
     656                if ( substr( $file, -3 ) !== '.po' )
     657                        continue;
     658                if ( ! preg_match( '/(.*)-([A-Za-z_]{2,6}).po/', $file, $match ) )
     659                        continue;
     660
     661                list( , $textdomain, $language ) = $match;
     662                $language_data[ $textdomain ][ $language ] = wp_get_pomo_file_data( WP_LANG_DIR . "/$type/$file" );
     663        }
     664        return $language_data;
     665}
     666
     667/**
     668 * Extract headers from a PO file.
     669 *
     670 * @since 3.7.0
     671 *
     672 * @param string $po_file Path to PO file.
     673 * @return array PO file headers.
     674 */
     675function wp_get_pomo_file_data( $po_file ) {
     676        $headers = get_file_data( $po_file, array(
     677                'POT-Creation-Date'  => '"POT-Creation-Date',
     678                'PO-Revision-Date'   => '"PO-Revision-Date',
     679                'Project-Id-Version' => '"Project-Id-Version',
     680                'X-Generator'        => '"X-Generator',
     681        ) );
     682        foreach ( $headers as &$header ) {
     683                // Remove possible contextual '\n' and closing double quote.
     684                $header = preg_replace( '~(\\\n)?"$~', '', $header );
     685        }
     686        return $headers;
     687}
Note: See TracChangeset for help on using the changeset viewer.