翻譯文字

本快速入門將說明如何透過 SAP BTP 版的 ABAP SDK for Google Cloud,使用 Cloud Translation API 第 2 版 建立可將文字從英文翻譯成德文的程式。

事前準備

執行本快速入門前,請確認您或管理員已完成下列先決條件:

建立 ABAP 類別來翻譯文字

  1. 建立套件:

    1. 在 ADT 中前往「Project Explorer」。
    2. ZLOCAL 套件上按一下滑鼠右鍵,然後依序選取「New」>「ABAP Package」
    3. 請輸入下列包裹詳細資料:

      • 「Name」:輸入 ZABAPSDK_TEST
      • 說明:輸入 ABAP SDK Test Package
    4. 點選「下一步」

    5. 在「Select a Transport Request」對話方塊中,選取「Create a new request」核取方塊。

    6. 輸入傳輸要求的說明。

    7. 按一下「完成」

  2. 建立 ABAP 類別來呼叫 Cloud Translation API:

    1. 在 ABAP 套件上按一下滑鼠右鍵,然後依序選取「New」>「ABAP Class」
    2. 請為 ABAP 類別輸入下列詳細資料:

      • 「Name」:輸入 ZGOOG_CL_QS_TRANSLATION
      • 說明:輸入 Quick start for Translation API
    3. 點選「下一步」

    4. 選取運送要求,然後按一下「完成」

  3. 在程式碼編輯器中,將預設程式碼替換為以下程式碼片段:

    " --------------------------------------------------------------------
    "  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.
            " Open HTTP Connection
            " The client key DEMO_TRANSLATE is an example, replace this with actual value
            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.
    

    DEMO_TRANSLATE 替換為用戶端金鑰名稱。

  4. 儲存並啟用變更。

  5. 執行應用程式:

    1. 選取 ABAP 類別 ZGOOG_CL_QS_TRANSLATION
    2. 依序點選「執行」>「以身分執行」>「ABAP 應用程式 (控制台)」。或者按下 F9。如果成功,系統會顯示下列輸出內容:
      'Translation Successful'
      'Translated Text is: Die Erde ist der dritte Planet von der Sonne'
      

後續步驟