Making WordPress.org

Changeset 3534


Ignore:
Timestamp:
06/22/2016 10:19:36 AM (7 years ago)
Author:
ocean90
Message:

Plugin Directory: Enhance logic for the locale banner.

  • Show suggestions for other languages only in the English directory.
  • Translate banner into the first language.
  • Tweak the strings.

See #1728.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • sites/trunk/wordpress.org/public_html/wp-content/plugins/plugin-directory/api/routes/class-plugin-translations.php

    r3491 r3534  
    6464        $suggest_locales = array_values( array_intersect( $translated_locales, $locales_from_header ) );
    6565        $current_locale_is_suggested = in_array( $current_locale, $suggest_locales );
    66         if ( $current_locale_is_suggested ) {
    67             $suggest_locales = array_diff( $suggest_locales, [ $current_locale ] );
    68         }
     66        $current_locale_is_translated = in_array( $current_locale, $translated_locales );
    6967
    7068        // Get the native language names of the locales.
    7169        $suggest_named_locales = [];
    7270        foreach ( $suggest_locales as $locale ) {
    73             $slug = str_replace( '_', '-', $locale );
    74             $slug = strtolower( $slug );
    75             $name = $wpdb->get_var( $wpdb->prepare( 'SELECT name FROM languages WHERE slug = %s', $slug ) );
    76             if ( ! $name ) {
    77                 $fallback_slug = explode( '-', $slug )[0]; // de-de => de
    78                 $name = $wpdb->get_var( $wpdb->prepare( 'SELECT name FROM languages WHERE slug = %s', $fallback_slug ) );
     71            $name = $this->get_native_language_name( $locale );
     72            if ( $name ) {
    7973                $suggest_named_locales[ $locale ] = $name;
    80             } else {
    81                 $suggest_named_locales[ $locale ] = $name;
    8274            }
    8375        }
    8476
    8577        $suggest_string = '';
    86         if ( 1 === count( $suggest_named_locales ) ) {
    87             $locale = key( $suggest_named_locales );
    88             $language = current( $suggest_named_locales );
     78        if ( 'en_US' === $current_locale ) {
     79            if ( 1 === count( $suggest_named_locales ) ) {
     80                $locale = key( $suggest_named_locales );
     81                $language = current( $suggest_named_locales );
     82                $suggest_string = sprintf(
     83                    $this->translate( 'This plugin is also available in %s.', $locale ),
     84                    sprintf(
     85                        '<a href="https://%s.wordpress.org/plugins-wp/%s/">%s</a>',
     86                        $locale_subdomain_assoc[ $locale ]->subdomain,
     87                        $plugin_slug,
     88                        $language
     89                    )
     90                );
     91            } elseif ( ! empty( $suggest_named_locales ) ) {
     92                $primary_locale = key( $suggest_named_locales );
     93                $primary_language = current( $suggest_named_locales );
     94                array_shift( $suggest_named_locales );
     95
     96                $other_suggest = '';
     97                foreach ( $suggest_named_locales as $locale => $language ) {
     98                    $other_suggest .= sprintf(
     99                        '<a href="https://%s.wordpress.org/plugins-wp/%s/">%s</a>, ',
     100                        $locale_subdomain_assoc[ $locale ]->subdomain,
     101                        $plugin_slug,
     102                        $language
     103                    );
     104                }
     105
     106                $suggest_string = sprintf(
     107                    $this->translate( 'This plugin is also available in %1$s (also: %2$s).', $primary_locale ),
     108                    sprintf(
     109                        '<a href="https://%s.wordpress.org/plugins-wp/%s/">%s</a>',
     110                        $locale_subdomain_assoc[ $primary_locale ]->subdomain,
     111                        $plugin_slug,
     112                        $primary_language
     113                    ),
     114                    trim( $other_suggest, ' ,' )
     115                );
     116            }
     117        } elseif ( ! $current_locale_is_suggested && ! $current_locale_is_translated ) {
    89118            $suggest_string = sprintf(
    90                 /* translators: %s: native language name. */
    91                 __( 'This plugin is also available in %s.', 'wporg-plugins' ),
    92                 sprintf(
    93                     '<a href="https://%s.wordpress.org/plugins-wp/%s/">%s</a>',
    94                     $locale_subdomain_assoc[ $locale ]->subdomain,
    95                     $plugin_slug,
    96                     $language
    97                 )
    98             );
    99         } elseif ( ! empty( $suggest_named_locales ) ) {
    100             $primary_locale = key( $suggest_named_locales );
    101             $primary_language = current( $suggest_named_locales );
    102             array_shift( $suggest_named_locales );
    103 
    104             $other_suggest = '';
    105             foreach ( $suggest_named_locales as $locale => $language ) {
    106                 $other_suggest .= sprintf(
    107                     '<a href="https://%s.wordpress.org/plugins-wp/%s/">%s</a>, ',
    108                     $locale_subdomain_assoc[ $locale ]->subdomain,
    109                     $plugin_slug,
    110                     $language
    111                 );
    112             }
    113 
    114             $suggest_string = sprintf(
    115                 /* translators: 1: native language name, 2: native language names */
    116                 __( 'This plugin is also available in %1$s (also: %2$s).', 'wporg-plugins' ),
    117                 sprintf(
    118                     '<a href="https://%s.wordpress.org/plugins-wp/%s/">%s</a>',
    119                     $locale_subdomain_assoc[ $primary_locale ]->subdomain,
    120                     $plugin_slug,
    121                     $primary_language
    122                 ),
    123                 trim( $other_suggest, ' ,' )
    124             );
    125         } elseif ( 'en_US' !== $current_locale && ! $current_locale_is_suggested ) {
    126             $suggest_string = sprintf(
    127                 /* translators: %s: URL to translate.wordpress.org */
    128                 __( 'This plugin is not yet available in your language. <a href="%s">Can you help translating it?</a>', 'wporg-translate' ),
    129                 'https://translate.wordpress.org/projects/wp-plugins/' . $plugin_slug
     119                $this->translate( 'This plugin is not available in %1$s yet. <a href="%2$s">Help translate it!</a>', $current_locale ),
     120                $this->get_native_language_name( $current_locale ),
     121                esc_url( 'https://translate.wordpress.org/projects/wp-plugins/' . $plugin_slug )
    130122            );
    131123        }
     
    236228        return false;
    237229    }
     230
     231    /**
     232     * Translates a string into a language.
     233     *
     234     * @param string $string The string to translate.
     235     * @param string $wp_locale A WP locale of a language.
     236     *
     237     * @return mixed
     238     */
     239    protected function translate( $string, $wp_locale ) {
     240        global $wpdb;
     241
     242        $strings = array(
     243            2984793 => 'This plugin is also available in %s.',
     244            2984794 => 'This plugin is also available in %1$s (also: %2$s).',
     245            2984795 => 'This plugin is not available in %1$s yet. <a href="%2$s">Help translate it!</a>',
     246        );
     247
     248        $original_id = array_search( $string, $strings, true );
     249
     250        require_once GLOTPRESS_LOCALES_PATH;
     251        $gp_locale = \GP_Locales::by_field( 'wp_locale', $wp_locale )->slug;
     252
     253        $cache = wp_cache_get( 'original-' . $original_id, 'lang-guess-translations' );
     254        if ( false !== $cache ) {
     255            return isset( $cache[ $gp_locale ] ) ? $cache[ $gp_locale ] : $string;
     256        }
     257
     258        // Magic number: 348841 is meta/plugins-v3.
     259        $translations = $wpdb->get_results( $wpdb->prepare( "
     260            SELECT
     261                locale as gp_locale, translation_0 as translation
     262            FROM translate_translation_sets ts
     263            JOIN translate_translations t
     264                ON ts.id = t.translation_set_id
     265            WHERE
     266                project_id = 348841 AND slug = 'default' AND t.status = 'current'
     267            AND original_id = %d
     268        ", $original_id ), OBJECT_K );
     269
     270        foreach ( $translations as &$translation ) {
     271            $translation = $translation->translation;
     272        }
     273        unset( $translation );
     274
     275    #   wp_cache_add( 'original-' . $original_id, $translations, 'lang-guess-translations', 900 );
     276
     277        return isset( $translations[ $gp_locale ] ) ? $translations[ $gp_locale ] : $string;
     278    }
     279
     280    protected function get_native_language_name( $locale ) {
     281        global $wpdb;
     282
     283        $slug = str_replace( '_', '-', $locale );
     284        $slug = strtolower( $slug );
     285
     286        $name = $wpdb->get_var( $wpdb->prepare( 'SELECT name FROM languages WHERE slug = %s', $slug ) );
     287        if ( ! $name ) {
     288            $fallback_slug = explode( '-', $slug )[0]; // de-de => de
     289            $name = $wpdb->get_var( $wpdb->prepare( 'SELECT name FROM languages WHERE slug = %s', $fallback_slug ) );
     290            if ( $name ) {
     291                return $name;
     292            }
     293        } else {
     294            return $name;
     295        }
     296
     297        return '';
     298    }
    238299}
     300
     301// Strings for the POT file.
     302
     303/* translators: %s: native language name. */
     304__( 'This plugin is also available in %s.', 'wporg-plugins' );
     305/* translators: 1: native language name, 2: other native language names, comma separated */
     306__( 'This plugin is also available in %1$s (also: %2$s).', 'wporg-plugins' );
     307/* translators: 1: native language name, 2: URL to translate.wordpress.org */
     308__( 'This plugin is not available in %1$s yet. <a href="%2$s">Help translate it!</a>', 'wporg-plugins' );
Note: See TracChangeset for help on using the changeset viewer.