Traduci testo

Questa guida rapida mostra come creare un programma che traduca un testo dall'inglese al tedesco utilizzando l'API Cloud Translation v2 tramite l'edizione SAP BTP dell'SDK ABAP per Google Cloud.

Prima di iniziare

Prima di eseguire questa guida rapida, assicurati che tu o i tuoi amministratori abbiate completato i seguenti prerequisiti:

  • Assicurati che l'API Cloud Translation sia abilitata nel tuo progetto Google Cloud.

    Vai alla libreria API

Crea una classe ABAP per tradurre il testo

  1. Crea un pacchetto:

    1. In ADT, vai a Project Explorer.
    2. Fai clic con il pulsante destro del mouse sul pacchetto ZLOCAL e seleziona Nuovo > Pacchetto ABAP.
    3. Inserisci i seguenti dettagli per il pacco:

      • Nome: inserisci ZABAPSDK_TEST.
      • Descrizione: inserisci ABAP SDK Test Package.
    4. Tocca Avanti.

    5. Nella finestra di dialogo Seleziona una richiesta di trasporto, seleziona la casella di controllo Crea una nuova richiesta.

    6. Inserisci una descrizione per la richiesta di trasporto.

    7. Fai clic su Fine.

  2. Crea una classe ABAP per chiamare l'API Cloud Translation:

    1. Fai clic con il pulsante destro del mouse sul pacchetto ABAP e seleziona Nuovo > Classe ABAP.
    2. Inserisci i seguenti dettagli per il tuo corso ABAP:

      • Nome: inserisci ZGOOG_CL_QS_TRANSLATION.
      • Descrizione: inserisci Quick start for Translation API.
    3. Tocca Avanti.

    4. Seleziona una richiesta di trasporto e fai clic su Fine.

  3. Nell'editor di codice, sostituisci il codice predefinito con il seguente snippet di codice:

    " --------------------------------------------------------------------
    "  Copyright 2024 Google LLC                                         -
    "                                                                    -
    "  Licensed under the Apache License, Version 2.0 (the "License");   -
    "  you may not use this file except in compliance with the License.  -
    "  You may obtain a copy of the License at                           -
    "      https://www.apache.org/licenses/LICENSE-2.0                   -
    "  Unless required by applicable law or agreed to in writing,        -
    "  software distributed under the License is distributed on an       -
    "  "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND,      -
    "  either express or implied.                                        -
    "  See the License for the specific language governing permissions   -
    "  and limitations under the License.                                -
    " --------------------------------------------------------------------
    class ZCL_QS_TRANSLATE_TEXT definition
      public
      final
      create public .
    
    public section.
    
      interfaces IF_OO_ADT_CLASSRUN .
    ENDCLASS.
    
    
    
    CLASS ZCL_QS_TRANSLATE_TEXT IMPLEMENTATION.
    
    
      METHOD IF_OO_ADT_CLASSRUN~MAIN.
        DATA ls_input        TYPE /goog/cl_translation_v2=>ty_006.
        DATA lt_translations TYPE /goog/cl_translation_v2=>ty_translations.
        DATA ls_texts        TYPE /goog/cl_translation_v2=>ty_008.
        DATA lo_translate    TYPE REF TO /goog/cl_translation_v2.
    
        TRY.
            " Instantiate API client stub
            lo_translate = NEW #( iv_key_name = 'DEMO_TRANSLATE' ).
    
            " Pass the text to be translated to the required parameter
            ls_input = VALUE #( format = 'text'
                                source = 'en'
                                target = 'de'
                                q      = VALUE #( ( |The Earth is the third planet from the Sun| ) ) ).
    
            " Call the API method to translate text
            lo_translate->translate_translations( EXPORTING is_input    = ls_input
                                                  IMPORTING es_output   = DATA(ls_output)
                                                            ev_ret_code = DATA(lv_ret_code)
                                                            ev_err_text = DATA(lv_err_text)
                                                            es_err_resp = DATA(ls_err_resp) ).
            IF lo_translate->is_success( lv_ret_code ) = abap_true.
              lt_translations = ls_output-data.
              TRY.
                  ls_texts = lt_translations-translations[ 1 ].
                  out->write( |Translation Successful| ).
                  out->write( |Translated Text is:  { ls_texts-translated_text }| ).
                CATCH cx_sy_itab_line_not_found.
                  out->write( |Translation not fetched| ).
              ENDTRY.
            ENDIF.
    
            " Close HTTP connection
            lo_translate->close( ).
    
          CATCH /goog/cx_sdk INTO DATA(lo_exception).
            " Handle exception here
        ENDTRY.
      ENDMETHOD.
    ENDCLASS.
    

    Sostituisci DEMO_TRANSLATE con il nome della chiave client.

  4. Salva e attiva le modifiche.

  5. Esegui l'applicazione:

    1. Seleziona la classe ABAP ZGOOG_CL_QS_TRANSLATION.
    2. Fai clic su Esegui > Esegui come > Applicazione ABAP (console). In alternativa, premi F9. In caso di esito positivo, viene visualizzato il seguente output:
      'Translation Successful'
      'Translated Text is: Die Erde ist der dritte Planet von der Sonne'
      

Passaggi successivi