Making WordPress.org

Changeset 8937


Ignore:
Timestamp:
06/10/2019 02:45:10 PM (5 years ago)
Author:
ocean90
Message:

Translate: Send approved translations to translation memory.

Props yoavf, ocean90.
See #4430.

Location:
sites/trunk/wordpress.org/public_html/wp-content/plugins
Files:
5 edited

Legend:

Unmodified
Added
Removed
  • sites/trunk/wordpress.org/public_html/wp-content/plugins/wporg-gp-customizations/templates/translation-row-editor.php

    r8766 r8937  
    7676                        <div class="button-menu">
    7777                            <button type="button" class="button-menu__toggle with-tooltip" aria-label="Show contextual links">
    78                                 <span class="screen-reader-text">Links</span><span aria-hidden="true" class="dashicons dashicons-paperclip"></span>
     78                                <span class="screen-reader-text">Links</span><span aria-hidden="true" class="dashicons dashicons-menu-alt"></span>
    7979                            </button>
    8080                            <ul class="button-menu__dropdown">
  • sites/trunk/wordpress.org/public_html/wp-content/plugins/wporg-gp-plugin-directory/inc/cli/class-sync-plugin-translations.php

    r4018 r8937  
    1414
    1515    /**
    16      * Delete a plugin project and its translations.
     16     * Sync plugin translations.
    1717     *
    1818     * ## OPTIONS
  • sites/trunk/wordpress.org/public_html/wp-content/plugins/wporg-gp-translation-suggestions/inc/class-plugin.php

    r8767 r8937  
    55use GP;
    66use GP_Locales;
    7 use Text_Diff;
    8 use WP_Error;
    9 use WP_Http;
    10 use WP_Text_Diff_Renderer_inline;
    11 
    12 require_once ABSPATH . '/wp-includes/wp-diff.php' ;
    137
    148class Plugin {
     9
     10    const TM_UPDATE_EVENT = 'wporg_translate_tm_update';
    1511
    1612    /**
     
    1814     */
    1915    private static $instance;
     16
     17    /**
     18     * @var array
     19     */
     20    private $queue = [];
    2021
    2122    /**
     
    4546        add_action( 'gp_pre_tmpl_load', [ $this, 'pre_tmpl_load' ], 10, 2 );
    4647        add_action( 'wporg_translate_suggestions', [ $this, 'extend_translation_suggestions' ] );
     48
     49        if ( 'cli' !== PHP_SAPI ) {
     50            add_action( 'gp_translation_created', [ $this, 'translation_updated' ], 3 );
     51            add_action( 'gp_translation_saved', [ $this, 'translation_updated' ], 3 );
     52
     53            // DB Writes are delayed until shutdown to bulk-update the stats during imports.
     54            add_action( 'shutdown', [ $this, 'schedule_tm_update' ], 3 );
     55        }
     56
     57        add_action( self::TM_UPDATE_EVENT, [ Translation_Memory_Client::class, 'update' ] );
     58    }
     59
     60    /**
     61     * Adds a translation in queue when a translation was created
     62     * or updated.
     63     *
     64     * @param \GP_Translation $translation Created/updated translation.
     65     */
     66    public function translation_updated( $translation ) {
     67        if ( ! $translation->user_id || 'current' !== $translation->status ) {
     68            return;
     69        }
     70
     71        $this->queue[ $translation->original_id ] = $translation->id;
     72    }
     73
     74    /**
     75     * Schedules a single event to update translation memory for new translations.
     76     */
     77    public function schedule_tm_update() {
     78        remove_action( 'gp_translation_created', [ $this, 'translation_updated' ], 3 );
     79        remove_action( 'gp_translation_saved', [ $this, 'translation_updated' ], 3 );
     80
     81        if ( ! $this->queue ) {
     82            return;
     83        }
     84
     85        wp_schedule_single_event( time() + 60, self::TM_UPDATE_EVENT, [ 'translations' => $this->queue ] );
    4786    }
    4887
  • sites/trunk/wordpress.org/public_html/wp-content/plugins/wporg-gp-translation-suggestions/inc/class-translation-memory-client.php

    r8732 r8937  
    33namespace WordPressdotorg\GlotPress\TranslationSuggestions;
    44
     5use GP;
    56use Text_Diff;
    67use WP_Error;
     
    89use WP_Text_Diff_Renderer_inline;
    910
    10 require_once ABSPATH . '/wp-includes/wp-diff.php' ;
     11require_once ABSPATH . '/wp-includes/wp-diff.php';
    1112
    1213class Translation_Memory_Client {
    1314
    1415    const API_ENDPOINT = 'https://translate.wordpress.com/api/tm/';
     16    const API_BULK_ENDPOINT = 'https://translate.wordpress.com/api/tm/-bulk';
     17
     18    /**
     19     * Updates translation memory with new strings.
     20     *
     21     * @param array $translations  List of translation IDs, keyed by original ID.
     22     * @return true|\WP_Error True on success, WP_Error on failure.
     23     */
     24    public static function update( array $translations ) {
     25        $requests = [];
     26
     27        foreach ( $translations as $original_id => $translation_id ) {
     28            $translation = GP::$translation->get( $translation_id );
     29
     30            // Check again in case the translation was changed.
     31            if ( 'current' !== $translation->status ) {
     32                continue;
     33            }
     34
     35            $original        = GP::$original->get( $original_id );
     36            $translation_set = GP::$translation_set->get( $translation->translation_set_id );
     37
     38            $locale = $translation_set->locale;
     39            if ( 'default' !== $translation_set->slug ) {
     40                $locale .= '_' . $translation_set->slug;
     41            }
     42
     43            $requests[] = [
     44                'source'       => $original->fields(),
     45                'translations' => [
     46                    [
     47                        'singular' => $translation->translation_0,
     48                        'plural'   => $translation->translation_1,
     49                        'locale'   => $locale,
     50                    ],
     51                ],
     52            ];
     53        }
     54
     55        if ( ! $requests ) {
     56            return new WP_Error( 'no_translations' );
     57        }
     58
     59        $body = wp_json_encode( [
     60            'token'    => WPCOM_TM_TOKEN,
     61            'requests' => $requests,
     62        ] );
     63
     64        $request = wp_remote_post(
     65            self::API_BULK_ENDPOINT,
     66            [
     67                'timeout'    => 10,
     68                'user-agent' => 'WordPress.org Translate',
     69                'body'       => $body,
     70            ]
     71        );
     72
     73        if ( is_wp_error( $request ) ) {
     74            return $request;
     75        }
     76
     77        if ( WP_Http::OK !== wp_remote_retrieve_response_code( $request ) ) {
     78            return new WP_Error( 'response_code_not_ok' );
     79        }
     80
     81        $body   = wp_remote_retrieve_body( $request );
     82        $result = json_decode( $body, true );
     83
     84        if ( JSON_ERROR_NONE !== json_last_error() ) {
     85            return new WP_Error( 'json_parse_error' );
     86        }
     87
     88        return $result ?: new WP_Error( 'unknown_error' );
     89    }
    1590
    1691    /**
     
    2297     */
    2398    public static function query( string $text, string $target_locale ) {
    24         if ( ! defined ( 'WPCOM_TM_TOKEN' ) ) {
     99        if ( ! defined( 'WPCOM_TM_TOKEN' ) ) {
    25100            return new WP_Error( 'no_token' );
    26101        }
     
    30105            'target' => $target_locale,
    31106            'token'  => WPCOM_TM_TOKEN,
    32         ] ) , self::API_ENDPOINT );
     107            'ts'     => time(),
     108        ] ), self::API_ENDPOINT );
     109
    33110
    34111        $request = wp_remote_get(
    35112            $url,
    36113            [
    37                 'timeout'     => 2,
    38                 'user-agent'  => 'WordPress.org Translate',
     114                'timeout'    => 2,
     115                'user-agent' => 'WordPress.org Translate',
    39116            ]
    40117        );
     
    82159        $diff     = new  Text_Diff( 'auto', [ [ $previous_text ], [ $text ] ] );
    83160        $renderer = new WP_Text_Diff_Renderer_inline();
     161
    84162        return $renderer->render( $diff );
    85163    }
  • sites/trunk/wordpress.org/public_html/wp-content/plugins/wporg-gp-translation-suggestions/js/translation-suggestions.js

    r8766 r8937  
    77                'nonce': nonce
    88            },
    9             dataType: 'json'
     9            dataType: 'json',
     10            cache: false,
    1011        } );
    1112
Note: See TracChangeset for help on using the changeset viewer.