Application Development and Automation Blog Posts
Learn and share on deeper, cross technology development topics such as integration and connectivity, automation, cloud extensibility, developing at scale, and security.
cancel
Showing results for 
Search instead for 
Did you mean: 
Naveen_n
Explorer
0 Kudos
760

 

Implementing Dynamic Multi-Language Support in RAP Applications Using Managed Scenarios 

In today’s globalized business environment, applications must cater to users from different linguistic backgrounds. The SAP ABAP RESTful Application Programming (RAP) model provides a robust way to implement dynamic multi-language support in managed scenarios. In this blog, we’ll explore how to achieve this step-by-step, complete with coding examples and output illustrations. 

Overview of Multi-Language Support in RAP 

RAP applications use language-dependent text tables to store translations. These texts are fetched dynamically based on the user's logon language. Managed scenarios in RAP handle database operations automatically, simplifying the implementation of language-specific features. 

Prerequisites 

  1. SAP BTP ABAP Environment 
  2. ABAP Development Tools (ADT) 
  3. Basic RAP Knowledge 
  4. Authorization to create CDS views and service definitions 

     Scenario 

    We will create an application that manages product descriptions in multiple languages using RAP managed scenarios. 

    Tables Required: 

    1. Main Table: ZPRODUCT  Contains product details. 
    2. Text Table: ZPRODUCT_TEXT  Stores language-dependent product descriptions. 

      Step-by-Step Implementation 

      1. Create Database Tables 

      Main Table: ZPRODUCT 

      Naveen_n_0-1742451002370.png
@EndUserText.label : 'Product table' 
@AbapCatalog.enhancement.category : #NOT_EXTENSIBLE 
@AbapCatalog.tableCategory : #TRANSPARENT 
@AbapCatalog.deliveryClass : #A 
@AbapCatalog.dataMaintenance : #RESTRICTED 
define table zna_t_product { 
key product_id : abap.char(10) not null; 
category : abap.char(20); 
price : abap.dec(15,2); 
} 

Text Table: ZPRODUCT_TEXT

Naveen_n_2-1742451163696.png

@EndUserText.label : 'Product text table' 
@AbapCatalog.enhancement.category : #NOT_EXTENSIBLE 
@AbapCatalog.tableCategory : #TRANSPARENT 
@AbapCatalog.deliveryClass : #A 
@AbapCatalog.dataMaintenance : #RESTRICTED 
define table zproduct_text { 
key product_id : abap.char(10) not null; 
key language : abap.lang not null; 
description : abap.string(0); 
} 

2. Create CDS Views 

Define View for Text Table (ZPRODUCT_TEXT) 

@AccessControl.authorizationCheck: #NOT_REQUIRED 
@EndUserText.label: 'Product root view' 
@Metadata.ignorePropagatedAnnotations: true 
define root view entity ZC_NA_PRODUCT as select from zna_t_product
{ 
key product_id as ProductId, 
category as Category, 
price as Price 
} 

 Combine Root and Text Views 

Naveen_n_0-1742451481639.png

@AccessControl.authorizationCheck: #NOT_REQUIRED 
@EndUserText.label: 'projection view for product text' 
@Metadata.ignorePropagatedAnnotations: true 
define root view entity zna_productwithtext 
provider contract transactional_query as projection on ZC_NA_PRODUCT as _texts 
association [0..*] to ZC_NA_PRODUCT_TEXT on $projection.ProductId = _texts.ProductId 
{ 
key ProductId, 
Category, 
Price 
} 

 

 4. Implement Multi-Language Logic in Behavior Definition 

Behavior Definition for Root Entity: 

Naveen_n_1-1742451578984.png

managed implementation in class ZBP_Product unique; 
define behavior for ZC_Product alias Product 
persistent table zproduct 
lock master 
authorization master 
{ 
  create; 
  update; 
  delete; 
  association _Texts { create; }
} 
define behavior for ZC_ProductText alias ProductText 
persistent table zproduct_text 
lock dependent by parent Product 
{ 
  create; update; delete;
} 
"Implement Behavior Pool Class 
"Develop the behavior pool class to handle business logic. 
CLASS zbp_product DEFINITION PUBLIC FINAL CREATE PUBLIC. 
  PUBLIC SECTION. 
    INTERFACES: if_abap_behavior_handler. 
  PROTECTED SECTION. 
  PRIVATE SECTION. 
ENDCLASS. 
CLASS zbp_product IMPLEMENTATION. 
METHOD if_abap_behavior_handler~create. 
DATA: ls_product      TYPE zproduct, 
      ls_product_text TYPE zproduct_texts, 
      lt_product_text TYPE TABLE OF zproduct_texts. 
" Loop through incoming data for product creation 
LOOP AT entities INTO DATA(ls_data). 
  "  Step 1: Create Product in Main Table 
  ls_product-product_id = ls_data-product_id. 
  ls_product-category   = ls_data-category. 
  ls_product-price      = ls_data-price. 
  INSERT INTO zproduct VALUES ls_product. 
  "  Step 2: Create Product Texts (in multiple languages if needed) 
  LOOP AT ls_data-_Texts INTO DATA(ls_text_data). 
    CLEAR ls_product_text. 
    ls_product_text-product_id   = ls_data-product_id. 
    ls_product_text-spras        = ls_text_data-spras. 
    ls_product_text-product_name = ls_text_data-product_name. 
    ls_product_text-description  = ls_text_data-description. 
    INSERT INTO zproduct_texts VALUES ls_product_text. 
  ENDLOOP. 
ENDLOOP. 
ENDMETHOD. 
ENDCLASS. 

  Create Service Definition and Binding 

Expose the CDS view through a service definition and bind it to an OData service. 

@EndUserText.label: 'Product Service Definition' 
define service ZUI_Product { 
  expose ZC_Product; 
} 

Naveen_n_0-1742451936690.png

 

Output:- 

English Session: 
Product ID: 00001 
Category: Electronics 
Product Name: Laptop 
Description: High-performance laptop 

German Session: 
Produkt-ID: 00001 
Kategorie: Elektronik 
Produktname: Laptop 
Beschreibung: Hochleistungs-Laptop 

 

Conclusion 

This blog demonstrated how to implement dynamic multi-language support in RAP managed scenarios. By leveraging CDS views, associations, and behavior definitions, you can build applications that cater to a diverse user base. This approach ensures scalability, performance, and an excellent user experience. 

1 Comment
Sujin_Appukuttan
Participant
0 Kudos

Hi,

 Shouldn't the Bdef reference root entity ZC_NA_PRODUCT instead of ZC_PRODUCT

Blog 1.JPG

 

Labels in this area