Making WordPress.org

Changeset 12584


Ignore:
Timestamp:
05/12/2023 08:05:57 AM (3 years ago)
Author:
amieiro
Message:

Copy the TM to the tab in the right sidebar

  • Copy the TM to the tab in the right sidebar.
  • Avoid making same request to the OpenAI and DeepL APIs

a few times in the same translation.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • sites/trunk/wordpress.org/public_html/wp-content/plugins/wporg-gp-translation-suggestions/js/translation-suggestions.js

    r12581 r12584  
    11( function( $ ){
     2    /**
     3     * Stores the originalId of the translations for which OpenAI has already been queried,
     4     * to avoid making the query another time.
     5     *
     6     * @type {array}
     7     */
     8    var OpenAITMSuggestionRequested = [];
     9
     10    /**
     11     * Stores the originalId of the translations for which DeepL has already been queried,
     12     * to avoid making the query another time.
     13     *
     14     * @type {array}
     15     */
     16    var DeeplTMSuggestionRequested = [];
     17
    218    function fetchSuggestions( $container, apiUrl, originalId, translationId, nonce ) {
    319        var xhr = $.ajax( {
     
    1632            if ( response.success ) {
    1733                $container.append( response.data );
     34                removeNoSuggestionsMessage( $container );
     35                copyTranslationMemoryToSidebarTab();
    1836            } else {
    1937                $container.append( $( '<span/>', { 'text': 'Error while loading suggestions.' } ) );
     
    3048        xhr.always( function() {
    3149            $container.removeClass( 'fetching' );
    32             removeNoSuggestionsMessage( $container );
    33         } );
     50        } );
     51    }
     52
     53    /**
     54     * Copies the translation memory to the sidebar tab and adds the number of items in the TM to the tab.
     55     *
     56     * @return {void}
     57     */
     58    function copyTranslationMemoryToSidebarTab(){
     59        var divSidebarWithTM = $gp.editor.current.find( '.meta.translation-memory' ).first();
     60        var divId = divSidebarWithTM.attr( 'data-row-id' );
     61        var TMcontainer = $gp.editor.current.find( '.suggestions__translation-memory' );
     62        if ( !TMcontainer.length ) {
     63            return;
     64        }
     65        if ( !TMcontainer.hasClass( 'initialized' ) ) {
     66            return;
     67        }
     68
     69        var itemsInTM = TMcontainer.find( '.translation-suggestion.with-tooltip.translation' ).length;
     70        itemsInTM += TMcontainer.find( '.translation-suggestion.with-tooltip.deepl' ).length;
     71        itemsInTM += TMcontainer.find( '.translation-suggestion.with-tooltip.openai' ).length;
     72        $( '[data-tab="sidebar-tab-translation-memory-' + divId + '"]' ).html( 'TM&nbsp;(' + itemsInTM + ')' );
     73        $( '#sidebar-div-translation-memory-' + divId ).html( TMcontainer.html() );
    3474    }
    3575
     
    71111        }
    72112
     113        if ( !$gp.editor.current.find('translation-suggestion.with-tooltip.translation').first() ) {
     114            return;
     115        }
     116
    73117        $container.addClass( 'fetching' );
    74118
     
    86130     **/
    87131    function maybeFetchOpenAISuggestions() {
    88         maybeFetchExternalSuggestions( gpTranslationSuggestions.get_external_translations.get_openai_translations, window.WPORG_TRANSLATION_MEMORY_OPENAI_API_URL );
     132        maybeFetchExternalSuggestions( 'OpenAI', gpTranslationSuggestions.get_external_translations.get_openai_translations, window.WPORG_TRANSLATION_MEMORY_OPENAI_API_URL );
    89133    }
    90134
     
    95139     **/
    96140    function maybeFetchDeeplSuggestions() {
    97         maybeFetchExternalSuggestions( gpTranslationSuggestions.get_external_translations.get_deepl_translations, window.WPORG_TRANSLATION_MEMORY_DEEPL_API_URL );
     141        maybeFetchExternalSuggestions( 'DeepL', gpTranslationSuggestions.get_external_translations.get_deepl_translations, window.WPORG_TRANSLATION_MEMORY_DEEPL_API_URL );
    98142    }
    99143
     
    101145     * Gets the suggestions from an external service.
    102146     *
    103      * @param getExternalSuggestions
    104      * @param apiUrl
    105      */
    106     function maybeFetchExternalSuggestions( getExternalSuggestions, apiUrl ) {
     147     * @param type                   The type of the external service: OpenAI or DeepL.
     148     * @param getExternalSuggestions Whether to get the suggestions from the external service.
     149     * @param apiUrl                 The URL of the API.
     150     *
     151     * @return {void}
     152     */
     153    function maybeFetchExternalSuggestions( type, getExternalSuggestions, apiUrl ) {
    107154        var $container = $gp.editor.current.find( '.suggestions__translation-memory' );
    108155        if ( !$container.length ) {
     
    112159            return;
    113160        }
    114 
    115161        var originalId = $gp.editor.current.original_id;
    116162        var translationId = $gp.editor.current.translation_id;
    117163        var nonce = $container.data( 'nonce' );
    118164
     165        if( true === wasRequestMade( type, originalId ) ) {
     166            return;
     167        }
     168
    119169        fetchSuggestions( $container, apiUrl, originalId, translationId, nonce );
     170    }
     171
     172    /**
     173     * Checks if the request was already made for this originalId and type.
     174     *
     175     * @param type        The type of the external service: OpenAI or DeepL.
     176     * @param originalId  The original ID.
     177     *
     178     * @returns {boolean} Whether the request was already made.
     179     */
     180    function wasRequestMade( type, originalId ) {
     181        if ('OpenAI' === type) {
     182            if ( originalId in OpenAITMSuggestionRequested ) {
     183                return true;
     184            } else {
     185                OpenAITMSuggestionRequested[originalId] = true;
     186            }
     187        }
     188        if ('DeepL' === type) {
     189            if ( originalId in DeeplTMSuggestionRequested ) {
     190                return true;
     191            } else {
     192                DeeplTMSuggestionRequested[originalId] = true;
     193            }
     194        }
     195        return false;
    120196    }
    121197
Note: See TracChangeset for help on using the changeset viewer.