Menerjemahkan teks

Panduan memulai ini menunjukkan cara membuat program yang menerjemahkan teks dari bahasa Inggris ke bahasa Jerman menggunakan Cloud Translation API v2 melalui ABAP SDK edisi SAP BTP untuk Google Cloud.

Sebelum memulai

Sebelum menjalankan panduan memulai ini, pastikan Anda atau administrator Anda telah memenuhi prasyarat berikut:

  • Pastikan Cloud Translation API diaktifkan di project Google Cloud Anda.

    Buka koleksi API

Membuat class ABAP untuk menerjemahkan teks

  1. Buat paket:

    1. Di ADT, buka Project Explorer.
    2. Klik kanan paket ZLOCAL, lalu pilih New > ABAP Package.
    3. Masukkan detail berikut untuk paket Anda:

      • Nama: masukkan ZABAPSDK_TEST.
      • Deskripsi: masukkan ABAP SDK Test Package.
    4. Klik Berikutnya.

    5. Pada dialog Select a Transport Request, centang kotak Create a new request.

    6. Masukkan deskripsi untuk permintaan transpor.

    7. Klik Selesai.

  2. Buat class ABAP untuk memanggil Cloud Translation API:

    1. Klik kanan paket ABAP, lalu pilih New > ABAP Class.
    2. Masukkan detail berikut untuk class ABAP Anda:

      • Nama: masukkan ZGOOG_CL_QS_TRANSLATION.
      • Deskripsi: masukkan Quick start for Translation API.
    3. Klik Berikutnya.

    4. Pilih permintaan transpor, lalu klik Selesai.

  3. Di editor kode, ganti kode default dengan cuplikan kode berikut:

    " --------------------------------------------------------------------
    "  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.
    

    Ganti DEMO_TRANSLATE dengan nama kunci klien.

  4. Simpan dan aktifkan perubahan.

  5. Jalankan aplikasi Anda:

    1. Pilih class ABAP ZGOOG_CL_QS_TRANSLATION.
    2. Klik Jalankan > Jalankan Sebagai > Aplikasi ABAP (Konsol). Atau, tekan F9. Jika berhasil, output berikut akan ditampilkan:
      'Translation Successful'
      'Translated Text is: Die Erde ist der dritte Planet von der Sonne'
      

Langkah selanjutnya