About this codelab
1. Introduction
This codelab provides steps to call the methods of Translation API V3 (Advanced) using ABAP SDK for Google Cloud. In this codelab we will see, how to translate a text, detect the language of the text and list the supported languages using the Translation API V3
The following Google Cloud services are used in this codelab:
- Compute Engine
- Network Services
- Cloud Shell
- Cloud Translation API V3
Prerequisites
- Ensure that you have access to a SAP system with ABAP SDK for Google Cloud installed.
- You can refer to codelab " Install ABAP Platform Trial on Google Cloud Platform and Install ABAP SDK" to set up a new system.
What you'll build
You'll create the following programs in your SAP system using ABAP SDK for Google Cloud:
- Translate text into multiple languages using Google's Cloud Translation API V3
- Detect the language of source text using Google Cloud Translation API V3
- List the supported languages of Google Cloud Translation API V3
2. Requirements
- A browser, such as Chrome or Firefox.
- A Google Cloud project with billing enabled or Create a 90-Day Free Trial account for Google Cloud Platform.
- SAP GUI (Windows or Java) installed in your system. If SAP GUI is already installed on your machine connect to SAP using the VM external IP address as the Application Server IP. If you are on Mac, then you can also install the SAP GUI for Java available in this link.
3. Before you begin
- Ensure that you have access to a SAP system with ABAP SDK for Google Cloud installed.
- You can refer to codelab " Install ABAP Platform Trial on Google Cloud Platform and Install ABAP SDK" to set up a new system.
- In the Google Cloud Console, on the project selector page, select or create a Google Cloud project (For example:
abap-sdk-poc).
- Make sure that billing is enabled for your Cloud project. Learn how to check if billing is enabled on a project. Skip this step if you are using the 90-Day Free Trial Account.
- You will use Cloud Shell, a command-line environment running in Google Cloud. From the Cloud Console, click Activate Cloud Shell on the top right corner:
- Run the following commands to authenticate for your account and set the default project to
abap-sdk-poc
. Zoneus-west4-b
is used as an example. If needed, please change the project and zone in the following commands based on your preference.
gcloud auth login
gcloud config set project abap-sdk-poc
gcloud config set compute/zone us-west4-b
- You must have access to an SAP system with the ABAP SDK for Google Cloud installed.
- You must complete codelab 1 (Install ABAP Platform Trial 1909 on Google Cloud Platform and Install ABAP SDK for Google Cloud) and codelab 2 (Configure ABAP SDK Authentication using tokens for SAP Hosted on Compute Engine VM) before proceeding with this codelab.
- If you have completed codelab 1 and codelab 2, this would have provisioned you with an ABAP Platform Trial 1909 System on Google Cloud, along with the required setup for authentication and connectivity.
- If you have not completed codelab 1 and codelab 2, you will not have all the required infrastructure and connectivity to perform the steps provided in this codelab. Therefore, you must complete codelab 1 and codelab 2 before proceeding with this codelab.
4. Enable Cloud Translation API V3 in your Google Cloud Project
- Execute the below command in the Cloud Shell, this will enable the Cloud Translation API in your Google Cloud Project:
gcloud services enable translate.googleapis.com
On successful execution you should see a message displayed as shown below:
You should now have the Cloud Translation API enabled in your Google Cloud Project
5. Create a Service Account with Cloud Translation User Role
To create a service account with required role, perform the following steps:
- Run the following command in the Cloud Shell terminal:
gcloud iam service-accounts create abap-sdk-translation-tester \
--display-name="Service Account for Translation"
- Now add the required roles to the service account created in the above step
gcloud projects add-iam-policy-binding abap-sdk-poc\
--member='serviceAccount:abap-sdk-translation-tester@abap-sdk-poc.iam.gserviceaccount.com' \
--role='roles/cloudtranslate.user'
The above command uses abap-sdk-poc as template name for the Google Cloud Project. Replace it with your project id.
- To verify, the role has been added, go to IAM page. The service account you created should be listed along with the role that has been assigned to it as shown below:
6. Create Client Key Configuration
Now that you have set up the pre-requisites on the Google Cloud side, we can move ahead with the configuration on the SAP side.
For authentication and connectivity related configuration, the ABAP SDK for Google Cloud uses the table /GOOG/CLIENT_KEY
To maintain the configuration in the table /GOOG/CLIENT_KEY
table, perform the following steps:
- In the SAP GUI, enter transaction code
SPRO
. - Click SAP Reference IMG.
- Click ABAP SDK for Google Cloud > Basic Settings > Configure Client Key.
- Maintain the following values against the fields:
Field | Value |
Google Cloud Key Name | TEST_TRANSLATION |
Google Cloud Service Account Name |
|
Google Cloud Scope |
|
Project ID | abap-sdk-poc |
Authorization Class |
|
Leave all other fields blank
7. Create a Z-Report to call the Cloud Translation API V3 and Translate a Text
- Log in to your SAP system.
- Go to transaction code
SE38
and create a Report Program with the nameZDEMO_TRANSLATE_TEXT.
- In the pop-up that opens up, provide details as shown below, and click Save.
In the next pop-up either select Local Object or Provide a package name as per your choice.
- In the ABAP Editor, add the following code:
DATA lo_translate TYPE REF TO /goog/cl_translation_v3.
DATA ls_input TYPE /goog/cl_translation_v3=>ty_050.
TRY.
lo_translate = NEW #( iv_key_name = 'TEST_TRANSLATION').
ls_input = VALUE #( mime_type = 'text/plain'
source_language_code = 'en-US'
target_language_code = 'es-ES'
contents = VALUE #( ( |Sun is our nearest star| ) ) ).
lo_translate->translate_text_projects(
EXPORTING
iv_p_projects_id = CONV #( lo_translate->gv_project_id )
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(lv_err_resp)
).
IF lo_translate->is_success( lv_ret_code ) = abap_true.
cl_demo_output=>new(
)->begin_section( 'API Call Successful:'
)->write_text( 'Translated Text:'
)->write_data( ls_output-translations[ 1 ]-translated_text
)->display( ).
ELSE.
cl_demo_output=>new(
)->begin_section( 'API Call Unsuccessful:'
)->write_text( 'Error Message:'
)->write_text( lv_err_text
)->display( ).
ENDIF.
CATCH /goog/cx_sdk INTO DATA(lo_sdk_excp).
DATA(lv_error) = lo_sdk_excp->get_text( ).
cl_demo_output=>new(
)->begin_section( 'Exception Occured:'
)->write_text( lv_error
)->display( ).
ENDTRY.
- Save and activate the Report.
- Execute the report (F8).
On successful execution you should see the report output as shown below:
8. Create a Z-Report to call the Cloud Translation API V3 and Detect the language
- Log in to your SAP system.
- Go to transaction code
SE38
and create a Report Program with the nameZDEMO_DETECT_LANGUAGE.
- In the pop-up that opens up, provide details as shown below, and click Save:
In the next pop-up either select Local Object or Provide a package name as per your choice.
- In the ABAP Editor, add the following code:
DATA lo_translate TYPE REF TO /goog/cl_translation_v3.
DATA ls_input TYPE /goog/cl_translation_v3=>ty_009.
TRY.
lo_translate = NEW #( iv_key_name = 'TEST_TRANSLATION').
ls_input = VALUE #( mime_type = |text/plain|
content = |La luce viaggia più veloce del suono| ).
lo_translate->detect_language_projects(
EXPORTING
iv_p_projects_id = CONV #( lo_translate->gv_project_id )
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(lv_err_resp)
).
IF lo_translate->is_success( lv_ret_code ) = abap_true.
cl_demo_output=>new(
)->begin_section( 'API Call Successful:'
)->write_text( 'Detected Language:'
)->write_data( ls_output-languages[ 1 ]-language_code
)->display( ).
ELSE.
cl_demo_output=>new(
)->begin_section( 'API Call Unsuccessful:'
)->write_text( 'Error Message:'
)->write_text( lv_err_text
)->display( ).
ENDIF.
CATCH /goog/cx_sdk INTO DATA(lo_sdk_excp).
DATA(lv_error) = lo_sdk_excp->get_text( ).
cl_demo_output=>new(
)->begin_section( 'Exception Occured:'
)->write_text( lv_error
)->display( ).
ENDTRY.
- Save and activate the Report.
- Execute the report (F8).
On successful execution you should see the report output as shown below:
9. Create a Z-Report to Call the GET_SUPPORTED_LANGUAGES Method
- Log in to your SAP System
- Go to transaction code
SE38
and create a Report Program with the nameZDEMO_GET_LANGUAGES.
- In the pop-up that opens up, provide details as shown below and click Save.
In the next pop-up either select Local Object or Provide a package name as per your choice.
- In the ABAP Editor, add the following code:
DATA lo_translate TYPE REF TO /goog/cl_translation_v3.
TRY.
lo_translate = NEW #( iv_key_name = 'TEST_TRANSLATION').
lo_translate->get_supported_languages_pro(
EXPORTING
iv_q_displaylanguagecode = 'en-US'
iv_p_projects_id = CONV #( lo_translate->gv_project_id )
IMPORTING
es_output = DATA(ls_output)
ev_ret_code = DATA(lv_ret_code)
ev_err_text = DATA(lv_err_text)
es_err_resp = DATA(lv_err_resp)
).
IF lo_translate->is_success( lv_ret_code ) = abap_true.
cl_demo_output=>new(
)->begin_section( 'API Call Successful:'
)->write_text( 'Supported Languages:'
)->write_data( ls_output-languages
)->display( ).
ELSE.
cl_demo_output=>new(
)->begin_section( 'API Call Unsuccessful:'
)->write_text( 'Error Message:'
)->write_text( lv_err_text
)->display( ).
ENDIF.
CATCH /goog/cx_sdk INTO DATA(lo_sdk_excp).
DATA(lv_error) = lo_sdk_excp->get_text( ).
cl_demo_output=>new(
)->begin_section( 'Exception Occured:'
)->write_text( lv_error
)->display( ).
ENDTRY.
- Save and activate the Report.
- Execute the report (F8).
On successful execution you should see the report output as shown below:
10. Congratulations
Congratulations! You have successfully completed the "Using Translation API with ABAP SDK for Google Cloud" codelab.
Cloud Translation API V3 also known as Cloud Translation API Advanced has a lot of features including:
- Translate with custom Models
- Translate using Glossaries
- Transliteration Support
- Document Translation
You can now proceed with the following codelabs to continue with your learning journey of using ABAP SDK for Google Cloud to access various Google Cloud Services.
- Send an event to Pub/Sub
- Receive an event from Cloud Pub/Sub
- Upload a large object to a Cloud Storage bucket using chunking
- Use DLP API for PII redaction
- Call BigQuery ML from ABAP
11. Clean up
If you do not wish to continue with the additional codelabs related to ABAP SDK for Google Cloud, please proceed with the cleanup.
Delete the project
- Delete the Google Cloud project:
gcloud projects delete abap-sdk-poc
Delete individual resources
- Delete the compute instance:
gcloud compute instances delete abap-trial-docker
- Delete the firewall-rules:
gcloud compute firewall-rules delete sapmachine
- Delete the service account:
gcloud iam service-accounts delete \
abap-sdk-dev@abap-sdk-poc.iam.gserviceaccount.com