翻译文字

本快速入门介绍如何通过 SAP BTP 版本的 ABAP SDK for Google Cloud,使用 Cloud Translation API v2 创建一个程序,以将文本从英语翻译成德语。

准备工作

在运行本快速入门之前,请确保您或您的管理员已满足以下前提条件:

  • 确保已在 Google Cloud 项目中启用 Cloud Translation API。

    转到 API 库

创建一个 ABAP 类来翻译文本

  1. 创建软件包:

    1. 在 ADT 中,转到 Project Explorer。
    2. 右键点击软件包 ZLOCAL,然后选择新建 > ABAP 软件包
    3. 为软件包输入以下详细信息:

      • 名称:输入 ZABAPSDK_TEST
      • 说明:输入 ABAP SDK Test Package
    4. 点击下一步

    5. 选择传输请求对话框中,选中创建新请求复选框。

    6. 输入传输请求的说明。

    7. 点击完成

  2. 创建一个 ABAP 类来调用 Cloud Translation API:

    1. 右键点击 ABAP 软件包,然后选择新建 > ABAP 类
    2. 为 ABAP 类输入以下详细信息:

      • 名称:输入 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.
            " 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.
    

    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'
      

后续步骤