Making WordPress.org


Ignore:
Timestamp:
04/05/2023 07:33:49 PM (3 years ago)
Author:
amieiro
Message:

Traslate: Make all the requests async in the Translation Memory

See https://github.com/WordPress/wordpress.org/pull/137

File:
1 edited

Legend:

Unmodified
Added
Removed
  • sites/trunk/wordpress.org/public_html/wp-content/plugins/wporg-gp-translation-suggestions/inc/routes/class-translation-memory.php

    r12513 r12527  
    1212
    1313        public function get_suggestions( $project_path, $locale_slug, $set_slug ) {
     14                $type                                  = 'Translation';
    1415                $original_id                           = gp_get( 'original' );
    1516                $translation_id                        = gp_get( 'translation', 0 );
     
    3940                }
    4041
    41                 $suggestions                     = Translation_Memory_Client::query( $original->singular, $locale );
     42                $suggestions = Translation_Memory_Client::query( $original->singular, $locale );
     43
     44                if ( is_wp_error( $suggestions ) ) {
     45                        wp_send_json_error( $suggestions->get_error_code() );
     46                }
     47
     48                wp_send_json_success(
     49                        gp_tmpl_get_output(
     50                                'translation-memory-suggestions',
     51                                compact( 'suggestions', 'type' ),
     52                                PLUGIN_DIR . '/templates/'
     53                        )
     54                );
     55        }
     56
     57        public function get_openai_suggestions( $project_path, $locale_slug, $set_slug ) {
     58                $type                                  = 'OpenAI';
     59                $original_id                           = gp_get( 'original' );
     60                $translation_id                        = gp_get( 'translation', 0 );
     61                $nonce                                 = gp_get( 'nonce' );
     62                $gp_default_sort                       = get_user_option( 'gp_default_sort' );
     63                $external_services_exclude_some_status = gp_array_get( $gp_default_sort, 'external_services_exclude_some_status', 0 );
     64                $translation                           = null;
     65                $suggestions                           = array();
     66
     67                if ( ! wp_verify_nonce( $nonce, 'translation-memory-suggestions-' . $original_id ) ) {
     68                        wp_send_json_error( 'invalid_nonce' );
     69                }
     70
     71                if ( empty( $original_id ) ) {
     72                        wp_send_json_error( 'no_original' );
     73                }
     74
     75                $original = GP::$original->get( $original_id );
     76                if ( ! $original ) {
     77                        wp_send_json_error( 'invalid_original' );
     78                }
     79
    4280                $current_set_slug                = 'default';
    4381                $locale_glossary_translation_set = GP::$translation_set->by_project_id_slug_and_locale( 0, $current_set_slug, $locale_slug );
     
    4987                        }
    5088                        if ( ! $translation || ( 'current' != $translation->status && 'rejected' != $translation->status && 'old' != $translation->status ) ) {
    51                                 $openai_suggestions = $this->get_openai_suggestion( $original->singular, $locale_slug, $locale_glossary );
    52                                 $deepl_suggestions  = $this->get_deepl_suggestion( $original->singular, $locale_slug, $set_slug );
     89                                $suggestions = $this->get_openai_suggestion( $original->singular, $locale_slug, $locale_glossary );
    5390                        }
    5491                } else {
    55                         $openai_suggestions = $this->get_openai_suggestion( $original->singular, $locale_slug, $locale_glossary );
    56                         $deepl_suggestions  = $this->get_deepl_suggestion( $original->singular, $locale_slug, $set_slug );
    57                 }
    58 
    59                 if ( is_wp_error( $suggestions ) ) {
    60                         wp_send_json_error( $suggestions->get_error_code() );
    61                 }
    62 
    63                 wp_send_json_success( gp_tmpl_get_output( 'translation-memory-suggestions', compact( 'suggestions', 'openai_suggestions', 'deepl_suggestions' ), PLUGIN_DIR . '/templates/' ) );
     92                        $suggestions = $this->get_openai_suggestion( $original->singular, $locale_slug, $locale_glossary );
     93                }
     94
     95                wp_send_json_success(
     96                        gp_tmpl_get_output(
     97                                'translation-memory-suggestions',
     98                                compact( 'suggestions', 'type' ),
     99                                PLUGIN_DIR . '/templates/'
     100                        )
     101                );
     102        }
     103
     104        public function get_deepl_suggestions( $project_path, $locale_slug, $set_slug ) {
     105                $type                                  = 'DeepL';
     106                $original_id                           = gp_get( 'original' );
     107                $translation_id                        = gp_get( 'translation', 0 );
     108                $nonce                                 = gp_get( 'nonce' );
     109                $gp_default_sort                       = get_user_option( 'gp_default_sort' );
     110                $external_services_exclude_some_status = gp_array_get( $gp_default_sort, 'external_services_exclude_some_status', 0 );
     111                $translation                           = null;
     112                $suggestions                           = array();
     113
     114                if ( ! wp_verify_nonce( $nonce, 'translation-memory-suggestions-' . $original_id ) ) {
     115                        wp_send_json_error( 'invalid_nonce' );
     116                }
     117
     118                if ( empty( $original_id ) ) {
     119                        wp_send_json_error( 'no_original' );
     120                }
     121
     122                $original = GP::$original->get( $original_id );
     123                if ( ! $original ) {
     124                        wp_send_json_error( 'invalid_original' );
     125                }
     126
     127                $locale = $locale_slug;
     128                if ( 'default' !== $set_slug ) {
     129                        $locale .= '_' . $set_slug;
     130                }
     131
     132                if ( $external_services_exclude_some_status ) {
     133                        if ( $translation_id > 0 ) {
     134                                $translation = GP::$translation->get( $translation_id );
     135                        }
     136                        if ( ! $translation || ( 'current' != $translation->status && 'rejected' != $translation->status && 'old' != $translation->status ) ) {
     137                                $suggestions = $this->get_deepl_suggestion( $original->singular, $locale_slug, $set_slug );
     138                        }
     139                } else {
     140                        $suggestions = $this->get_deepl_suggestion( $original->singular, $locale_slug, $set_slug );
     141                }
     142
     143                wp_send_json_success(
     144                        gp_tmpl_get_output(
     145                                'translation-memory-suggestions',
     146                                compact( 'suggestions', 'type' ),
     147                                PLUGIN_DIR . '/templates/'
     148                        )
     149                );
    64150        }
    65151
     
    378464         */
    379465        private function update_one_external_translation( string $translation, string $suggestion, string $external_translations_used, string $external_same_translations_used ) {
    380                 $sameTranslationUsed      = $translation == $suggestion;
     466                $is_the_same_translation  = $translation == $suggestion;
    381467                $gp_external_translations = get_user_option( 'gp_external_translations' );
    382468                $translations_used        = gp_array_get( $gp_external_translations, $external_translations_used, 0 );
     
    387473                $translations_used++;
    388474                $gp_external_translations[ $external_translations_used ] = $translations_used;
    389                 if ( $sameTranslationUsed ) {
     475                if ( $is_the_same_translation ) {
    390476                        if ( ! is_int( $same_translations_used ) || $same_translations_used < 0 ) {
    391477                                $same_translations_used = 0;
Note: See TracChangeset for help on using the changeset viewer.