Lost in Translation? Not with ABAP & Google Cloud: Your Business’s Multilingual Superhero

Satish Inamdar
Google Cloud - Community
10 min readDec 17, 2023

Google Cloud Translation APIs (Basic & Advanced)

Google Cloud offers the Translation API in two versions: the Translation API V2 (also known as Basic edition) and the Translation API V3 (also known as Advanced edition). The capabilities available in Translation API Basic are limited in comparison to what the Translation API Advanced has. This link provides you the complete comparison of both the APIs.

Translation API Advanced has a ton’s of features and below is a listing of some of them:

  • Translate with custom Models
  • Translate using Glossaries
  • Transliteration Support
  • Document Translation

It’s estimated that 500 billion words are translated daily, but most translation processes for enterprises are manual, time-consuming, and expensive. Google Cloud is changing all that, bringing Google quality translation to the enterprise with which you can drive more inclusive consumer experiences, save millions of dollars, and get your business started on localizing across the world in minutes.

Before we start realizing and connecting the threads between ABAP and Translation API, let’s learn a bit about ABAP SDK for Google Cloud V1.5

ABAP SDK for Google Cloud V1.5

ABAP SDK for Google Cloud is a set of ABAP libraries ( Classes, also referred to as API Client Stubs) that makes it easy to consume Google Cloud APIs using ABAP programming language on SAP systems. The SDK includes API Client Stubs for a variety of Google Cloud services, including Translation API.

To use the ABAP SDK for Google Cloud on your SAP systems, you need to install the SDK on your SAP system by importing the Transport Requests. You can find instructions on how to do this here.

Once you have installed the SDK, you can use the ABAP Classes to access Google Cloud services from your ABAP code. For example, you can use the Translation API Classes (/GOOG/CL_TRANSLATION_V2 or /GOOG/CL_TRANSLATION_V3) to translate text from one language to another.

To learn more about using the ABAP SDK for Google Cloud, you can refer to the documentation website. The documentation website provides tutorials, code samples, and reference documentation for all of the libraries in the SDK.

Some of the benefits of using the ABAP SDK for Google Cloud:

  • It makes it simple to connect to Google Cloud and Consume many supported Google APIs
  • It provides a consistent programming model for accessing Google Cloud services.
  • It is well-documented and supported by Google.

Translate with Glossary

In this blog post, we’ll take a deep dive into how to use the “Translate using Glossaries” feature of the Translation API Advanced and how to call the Translation method using the ABAP SDK for Google Cloud V1.5.

To translate using glossaries, we must first understand what glossaries are and how they are relevant to enterprises. Next, we will briefly review the basic configuration steps involved in setting up ABAP SDK. Finally, we will examine the ABAP code that can be used to achieve this translation.

What is a Glossary

Glossaries are a collection of terms and their translations. You can also treat it as a “Custom Dictionary”. They are used to ensure consistency in the translation of text. For example, if a term is translated as “apple” in one sentence, it should be translated as “apple” in all other sentences. Glossaries are also useful for ensuring that the correct terminology is used. For example, if a term is specific to a particular industry, it should be translated using the terminology that is specific to that industry.

Enterprises require glossaries to ensure that their communications are accurate and consistent. They also need glossaries to avoid legal and compliance issues. For example, if a company is translating a contract, it is important to ensure that the terms of the contract are translated accurately. If the terms are not translated accurately, the company could be subject to legal liability.

Translation API Advanced supports unidirectional glossaries as well as equivalent term set glossaries. You can read in detail about them here. For this blog, we will use a unidirectional glossary which is basically a CSV (comma separated values) file.

The glossary file has already been uploaded to a Cloud Storage Bucket and it contains the below data. Our technical execution story starts from here:

Glossary File Contents
Glossary File Contents
Cloud Storage Bucket containing Glossary File

Setting up ABAP SDK for Google Cloud

To set up ABAP SDK for usage in your system, perform below mentioned steps:

Step: Configure a Client Key required for connectivity.

  1. Login to SAP where ABAP SDK for Google Cloud V1.5 is installed, goto transaction SPRO > ABAP SDK for Google Cloud > Basic Settings > Configure Client Key and add the following new entry. (Please replace the service account and project id in the below entry as per your environment).

2. Ensure that the service account is assigned the role “Cloud Translation API Editor” role

3. Set values to following fields as indicated below:

  • Google Cloud Key Name: DEMO_TRANSLATE
  • Google Cloud Service Account Name: abap-sdk-dev@gcp-project.iam.gserviceaccount.com
  • Google Cloud Scope: https://www.googleapis.com/auth/cloud-platform
  • Google Cloud Project Identifier: gcp-project
  • Authorization Class: /GOOG/CL_AUTH_GOOGLE
  • Leave the other fields blank

Let’s get started with coding

Step 1: Create a Glossary ID

As a first step, we need to use the glossary file stored in the Cloud Storage bucket and create a glossary id. To achieve this, create a report with name ZCREATE_GLOSSARY in transaction code SE38 and copy paste the below code:


REPORT zcreate_glossary.

DATA lt_str TYPE TABLE OF string.
DATA ls_input TYPE /goog/cl_translation_v3=>ty_022.
DATA lv_state TYPE string.
FIELD-SYMBOLS <fs> TYPE any.

TRY.

" Open HTTP Connection and pass the configured client key
DATA(lo_translate) = NEW /goog/cl_translation_v3( iv_key_name = 'DEMO_TRANSLATE' ).

" Pass a display name
ls_input-display_name = 'Sample Glossary EN to DE'.

" Source language in BCP-47 format - English
ls_input-language_pair-source_language_code = 'en-US'.
" Target language in BCP-47 format - German
ls_input-language_pair-target_language_code = 'de-DE'.

" Complete name of glossary has following format:
" projects/<PROJECT_ID>/locations/<LOCATION_ID>/glossaries/<GLOSSARY_ID>
ls_input-name = |projects/| &&
lo_translate->gv_project_id &&
|/locations/us-central1/glossaries/| &&
|TEST_GLOSSARY_EN_DE|.

" Pass the complete path of glossary file which is stored in GCS bucket
" This example uses a file named: glossary_sample.csv
" stored in bucket abap_sdk_glossary_demo
ls_input-input_config-gcs_source-input_uri = 'gs://abap_sdk_glossary_demo/glossary_sample.csv'.

" Call API method
lo_translate->create_glossaries(
EXPORTING iv_p_projects_id = CONV #( lo_translate->gv_project_id )
iv_p_locations_id = 'us-central1'
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 /goog/cl_http_client=>is_success( lv_ret_code ).
" This returns a long running operation id
" You can use the LRO ID to check the status of the operation
DATA(lo_demo_op) = cl_demo_output=>new( ).

lo_demo_op->write_text( 'Glossary creation started'
)->write_text( 'Operation ID:' && ls_output-name ).

" Get operation id
SPLIT ls_output-name AT '/' INTO TABLE lt_str.
DATA(lv_lines) = lines( lt_str ).
DATA(lv_operation_id) = lt_str[ lv_lines ].

" Here we will retry every 2 seconds for 10 times to get the status of LRO
" Note that this time frame can sometimes vary
DO 10 TIMES.
WAIT UP TO 2 SECONDS.

lo_translate->get_operations(
EXPORTING iv_p_projects_id = CONV #( lo_translate->gv_project_id )
iv_p_locations_id = 'us-central1'
iv_p_operations_id = lv_operation_id
IMPORTING es_output = DATA(ls_operation_op)
ev_ret_code = lv_ret_code
ev_err_text = lv_err_text
es_err_resp = ls_err_resp ).

IF lo_translate->is_success( lv_ret_code ) = abap_true.
ASSIGN COMPONENT 'STATE' OF STRUCTURE
ls_operation_op-metadata->* TO <fs>.
IF sy-subrc = 0.
lv_state = <fs>->*.
ENDIF.
lo_demo_op->write_text( 'Operation Status Check Attempt:' && sy-index
)->write_text( 'Status:' && lv_state ).
IF lv_state = 'SUCCEEDED'.
EXIT. " exit from the loop
ENDIF.
IF lv_state = 'FAILED'.
lo_demo_op->write_text( 'Error:' && ls_operation_op-error-message ).
EXIT. " exit from the loop
ENDIF.
ELSE.
lo_demo_op->write_text( 'Operation Status Check Attempt:' && sy-index
)->write_text( 'Call failed, HTTP Return Code:' && lv_ret_code ).
ENDIF.
ENDDO.

ELSE.
MESSAGE lv_err_text TYPE 'E'.
ENDIF.

" Close HTTP Connection
lo_translate->close( ).

lo_demo_op->display( ).
CATCH /goog/cx_sdk INTO DATA(lo_exception).
MESSAGE lo_exception->get_text( ) TYPE 'E'.
ENDTRY.

Once activated if program is executed successfully then you should see an output similar to the screenshot shown below:

Program Output
Program Output

Step 2: Use Glossary in Translation

In the previous step, we have created the glossary using ABAP code. Now, let us use this glossary to perform some translations and observe the effect of the glossary.

Create a report with name ZTRANSLATE_WITH_GLOSSARY in transaction code SE38 and copy paste the below code:

REPORT ztranslate_with_glossary.

TYPES: BEGIN OF ty_output,
text TYPE string,
trx_default TYPE string,
trx_glossary TYPE string,
END OF ty_output.

" Data Declarations
DATA ls_input TYPE /goog/cl_translation_v3=>ty_050.
DATA lt_op TYPE STANDARD TABLE OF ty_output WITH NON-UNIQUE DEFAULT KEY.
DATA lo_exception TYPE REF TO /goog/cx_sdk.

TRY.

" Open HTTP Connection and Pass the configured client key
DATA(lo_translate) = NEW /goog/cl_translation_v3(
iv_key_name = 'DEMO_TRANSLATE' ).

" Populate request body
ls_input-mime_type = 'text/plain'. " Provide MIME type
ls_input-source_language_code = 'EN'.
ls_input-target_language_code = 'DE'.

" Provide glossary id
ls_input-glossary_config-glossary = |projects/| &&
lo_translate->gv_project_id &&
|/locations/us-central1/glossaries/| &&
|TEST_GLOSSARY_EN_DE|.

ls_input-contents = VALUE #( ( |Debit account| )
( |The company is Everywhere| )
( |FunctionModule is part of Function Group| )
( |There is an error in Plant data| )
( |How many follower on social media?| ) ).

" Call the API
lo_translate->translate_text_locations
( EXPORTING iv_p_projects_id = CONV #( lo_translate->gv_project_id )
iv_p_locations_id = 'us-central1'
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 /goog/cl_http_client=>is_success( lv_ret_code ).
" On successful call
" print glossary translated text along with regular translation
lt_op = VALUE #( BASE lt_op FOR i = 1 UNTIL i > 5
( text = ls_input-contents[ i ]
trx_default = ls_output-translations[ i ]-translated_text
trx_glossary = ls_output-glossary_translations[ i ]-translated_text ) ).

cl_demo_output=>new(
)->begin_section( 'Translation Result:'
)->write_data( lt_op
)->display( ).

ELSE.
MESSAGE lv_err_text TYPE 'E'.
ENDIF.

" Close HTTP Connection
lo_translate->close( ).

" Handle exception
CATCH /goog/cx_sdk INTO lo_exception.
MESSAGE lo_exception->get_text( ) TYPE 'E'.
ENDTRY.

On activation, if successfully executed you should be able to see an output as shown below:

Translation with Glossary — Program Output

In the above screenshot of the program output, one can observe subtle differences between the regular translation and the translation from the glossary.

Conclusion

Glossaries are a valuable tool for ensuring that translations are accurate and consistent. They can be used to define specific terms and phrases that have unique meanings in a particular industry or context. By using glossaries, businesses can avoid the pitfalls of using generic translations that may not be appropriate for their specific needs.

By using glossaries and custom models, businesses can achieve business contextual translation. This means that the translations produced by the API will be tailored to the specific needs of the business and will accurately reflect the meaning of the original text. This can be a valuable asset for businesses that need to communicate with customers or partners in multiple languages.

Cloud Translation API V3 is a powerful tool that can help businesses overcome many of the translation challenges they face. By using glossaries and custom models, businesses can achieve business contextual translation, which can improve the accuracy, consistency, and relevance of their translations.

Next Steps

Ready to start using ABAP SDK for Google Cloud?

Bookmark What’s new with the ABAP SDK for Google Cloud for the latest announcements and follow installation and configuration instructions.

Check out these blog posts to get started with ABAP SDK for Google Cloud

  • This blog, explains how you can evaluate ABAP SDK for Google Cloud using ABAP Platform Trial 1909 on Google Cloud Platform.
  • Read this blog post to get a sneak peek on how a business process such as Sales Order entry in SAP can be automated using ABAP SDK for Google Cloud.
  • This blog is an excellent start to understand how BigQuery ML which is a powerful machine learning service that lets you build and deploy models using SQL queries. you can now be accessed with ABAP SDK for Google Cloud.
  • Read this blog post to understand how to use Secret Manager with ABAP SDK.
  • Also check out blog post about ABAP SDK Code Wizard, and on Application logging as some of the many Engineering excellence delivered as part of ABAP SDK.

Join the community today!

The ABAP SDK for Google Cloud Community is now open! This is a place for you to ask questions, share knowledge, and collaborate with other ABAP developers who are using Google Cloud.

We encourage you to get involved in the community and help us make the ABAP SDK for Google Cloud even better. We have a lot of exciting things planned for the future, and we want you to be a part of it.

Checkout the youtube Channel!!

Subscribe to the below youtube channel where you would find, a quick 5 minutes overview covering the design principles and capabilities of ABAP SDK, reference architectures and art of the possible SAP solutions based on Google’s AI services, Google Workspace APIs and Google Maps Platform APIs…. along many more insightful references.

--

--