Enterprise Resource Planning Blogs by Members
Gain new perspectives and knowledge about enterprise resource planning in blog posts from community members. Share your own comments and ERP insights today!
cancel
Showing results for 
Search instead for 
Did you mean: 
shashankkasar
Participant
Business Scenario:

Scenario: There are many times when we need to translate some text from one language to another based on business demands during the runtime and display it over the UI.

For this, I came up with a Runtime Language Solution using existing API resources. I have developed 2 solutions as below

  1. Using Google Translate API for which Google API Licence is pre-requisite and cost more if customer doesn't alright have it.

  2. Using SAP Translation Hub API for which SAP Translation Hub Licence is required and API Key is included with S/4HANA licence.


I will write 2 blogs separately for both the solutions. In this blog, I will explain the first option using Google Translation API step by step.

Step 1: Set Up Your Google Cloud Project and Obtain API Key

The first step in integrating the Google Translation API is to set up a Google Cloud Project. This can be done by visiting the Google Cloud Console and creating a new project. After setting up the project, you will need to obtain an API key, which can be done by navigating to the “Credentials” section of the Google Cloud Console.

  • Visit the Google Cloud Console at https://console.cloud.google.com/

  • Click on the project drop-down and select or create the project that you want to use for this integration.

  • Click on the hamburger menu and select APIs & Services > Library.

  • Search for Google Cloud Translation API and click on the API to open its overview page.

  • Click on the Enable button to enable the API for your project.

  • Go to the API credentials page.

  • Click on the Create credentials button and select API key.

  • The API key will be displayed on the API credentials page. Note down this API key as it will be used in the SAP ABAP code.


Step 2: Create a New SAP ABAP Project

The next step is to create a new SAP ABAP project. This can be done by navigating to the SAP ABAP development environment and creating a new project.

  • Log in to your SAP system with a user that has sufficient authorisation to create ABAP programs.

  • Navigate to the ABAP Development workbench (transaction SE80).

  • In the ABAP workbench, click on the Create button and select Package.

  • Enter a descriptive name for the package and click on the Create button.

  • Right-click on the package and select Create > Function Group.

  • Enter a descriptive name for the function group and click on the Create button.

  • Right-click on the function group and select Create > Function Module.


Step 3: Define the Function Module

Once you have created your SAP ABAP project, you need to define the function module. To do this, navigate to the ABAP Dictionary and create a new function module. In the function module, you will need to specify the parameters and the return values.

  • Enter a descriptive name for the function module.

  • In the Function module dialog, click on the Import tab.

  • Define the input parameters for the function module. For example:

    • IMPORTING

      • IV_TEXT TYPE STRING,

      • IV_SOURCE_LANGUAGE TYPE STRING,

      • IV_TARGET_LANGUAGE TYPE STRING.





  • In the Function module dialog, click on the Export tab.

  • Define the output parameters for the function module. For example:

    • EXPORTING

      • EV_TRANSLATED_TEXT TYPE STRING.





  • Save the function module.


Step 4: Write the Code for the Function Module

The next step is to write the code for the function module. The code will utilise the Google Translation API to translate the text from one language to another. To do this, you will need to make an HTTP request to the Google Translation API and pass in the text to be translated, along with the source and target languages.

Please look for the FM code into the Appendix 1 at the end of the blog.

Step 5: Test the Function Module

Once you have written the code for the function module, you can test it to ensure it works as expected. To do this, you can run the function module in the SAP ABAP development environment and verify that it returns the expected results.

  • Save and activate the function module.

  • In the ABAP workbench, click on the Test button to open the Function Builder.

  • Enter test input values for the function module and click on the Test button.

  • Check the test output values to ensure that the integration is working as expected.


And that's it! You have successfully integrated the Google Translation API into your SAP ABAP system.

In conclusion, integrating the Google Translation API into SAP ABAP is a simple and straightforward process. With just a few steps, you can add a new level of functionality to your SAP ABAP applications, making it easier for businesses with a global presence to communicate with their customers in multiple languages.

Appendix 1:
****************************************************************************************************

Appendix 1: Sample code:

****************************************************************************************************

* Define the data for the API request
data: lv_url type string value 'https://translation.googleapis.com/language/translate/v2',
lv_key type string value 'YOUR_API_KEY',
lv_q type string value iv_text,
lv_source type string value iv_source_language,
lv_target type string value iv_target_language,
lv_param type string,
lv_response type string.

* Build the API request URL
concatenate lv_url '?q=' lv_q '&source=' lv_source '&target=' lv_target '&key=' lv_key into lv_param.

* Call the API and receive the response
call method cl_http_client=>create_by_url
exporting
url = lv_param
importing
client = lv_client
exceptions
argument_not_found = 1
plugin_not_active = 2
internal_error = 3

others = 4.

call method lv_client->request->set_method
exporting
method = 'GET'.

call method lv_client->send
exceptions
http_communication_failure = 1
http_invalid_state = 2.

call method lv_client->receive
exceptions
http_communication_failure = 1
http_invalid_state = 2.

lv_response = lv_client->response->get_cdata( ).

* Extract the translated text from the response
data(lv_json) = cl_sxml_string_reader=>from_string( lv_response ).
data(lv_data) = cl_sxml_table_reader=>from_json( lv_json ).

ev_translated_text = lv_data[ 'data' ]-['translations' ]-0-['translatedText' ].

* Close the HTTP client connection
call method lv_client->close.

Please note that you will need to replace YOUR_API_KEY, YOUR_TEXT_TO_BE_TRANSLATED, YOUR_SOURCE_LANGUAGE, and YOUR_TARGET_LANGUAGE with your own values. You may also need to modify the code to parse the response according to your specific needs.

Please share your feedback or comments down below if you have any suggestions.

In the next blog, I will be sharing the second solution i.e. Runtime Translation Solution using SAP Translation Hub API, which will be live soon.

Thank you and keep learning!
3 Comments
Labels in this area