Using Translatable Text in ABAP

Objectives

After completing this lesson, you will be able to:

  • Describe the translation process for ABAP developments.
  • Use text elements for making developments translatable.

Translation of ABAP Developments

Some ABAP development objects contain text which could be displayed on the user interface (UI) of an application. In the ABAP RESTful application programming model, for example, the following texts appear on the UI:

  • Labels and headers from annotations in data definitions and metadata extensions
  • Labels from data elements
  • Messages from message classes

These texts need to be different for end users who start the app with different logon languages.

Note

In ABAP Development Tools, you find the original language of a development object on the Properties Tab below the editor view.

Of course, one cannot expect that the developer is able to provide the texts in every different languages. Watch this video to explore how ABAP development deals with different languages.

Text Pools and Text Elements

As discussed, language-dependent texts in data elements, data definitions, metadata extensions, message classes, and so on, are translatable by default. But what about texts in the ABAP source code? Are they translatable by default too?

The answer is no, literals are not translatable! The content of literals is always the same, independent from the logon language of the user.

If literals contain language-dependent text, and if this text reaches the UI, at some point you must replace the literals with text symbols.

Text Symbols

Text symbols, sometimes also referred to as text elements, are stored in the text pool of global ABAP classes.

A text symbol is identified by a three character ID, which has to be unique within the text pool. The ID of a text symbol consists of digits, letters or a combination of both. Text symbol IDs are not case-sensitive. In the text pool editor they are always displayed in upper case.

In our example, the text pool contains a text symbol 001 and a text symbol HAU.

Note

Do not confuse text IDs with message IDs. For messages in message classes only digits are allowed.

Every text symbol defines a text in the original language of the ABAP class. The technical limit for the text length is 255 characters. In addition to this technical limit, a semantical maximum length has to be defined for each text symbol. The maximum length has to lie between the actual length and the technical limit.

In our example, the text symbol HAU has an actual length of 12 and a maximum length of 30.

Caution

The maximum length defines a hard limit for the translator. It should be significantly higher than the actual length to avoid the need for cryptic abbreviations in translated text elements.

There are two ways to access a text symbols in ABAP:

  • Standalone, that is with text, followed by a hyphen, and the text ID
  • Attached to a text literal, that is with the ID inside a pair of brackets immediately after a text literal.

In the second variant the text literal is used as a type of fallback. If the text symbol exists in the currently loaded text pool, then the content of the text symbol is used instead of the literal, otherwise the literal is used.

Note

To avoid confusion, the text of the text symbol and the text in the literal should always match. The "Extended Program Check (SLIN)" in ATC contains a check of character strings. This check issues a warning if the literal and the text symbol are different.

After the translation of the text symbols, the ABAP runtime uses the text that fits the logon language of the user.

How to Use Quick Fixes for Text Symbols

Although you can edit the text pool manually, it is much easier to create and maintain text symbols using quick fixes in the ABAP editor. Watch this demonstration to learn how to use quick fixes for text symbols.

Create and Use Text Symbols

You notice that your console output is not translatable. In this exercise, you replace literal text from string templates with text symbols from a text pool.

Template:

  • /LRN/CL_S4D401_TCS_TIMESTAMP (Global Class)

Solution:

  • /LRN/CL_S4D401_TCS_TEXT_POOL (Global Class)

Task 1: Copy Template (Optional)

Copy the template class. If you finished the previous exercise, you can skip this task and continue editing your class ZCL_##_SOLUTION.

Steps

  1. Copy class /LRN/CL_S4D401_TCS_TIMESTAMP to a class in your own package (suggested name: ZCL_##_SOLUTION, where ## stands for your group number).

    1. In the Project Explorer view, right-click class /LRN/CL_S4D401_TCS_TIMESTAMP to open the context menu.

    2. From the context menu, choose Duplicate ....

    3. Enter the name of your package in the Package field. In the Name field, enter the name ZCL_##_SOLUTION, where ## stands for your group number.

    4. Adjust the description and choose Next.

    5. Confirm the transport request and choose Finish.

  2. Activate the copy.

    1. Press Ctrl + F3 to activate the class.

Task 2: Enable Translation of the Output

Make sure you are logged on with the original language of your global class. Then adjust the implementation of method GET_OUTPUT of local class LCL_CARRIER. Replace the literal text in string templates with translatable text from the text pool.

Steps

  1. Display the original language of your ABAP class and make sure you are logged on in the same language.

    1. Open the source code of your global class ZCL_##_SOLUTION.

    2. From the tabs below the editor choose the Properties tab.

    3. Take note of the Original Language property. It should be set to EN.

    4. In the Project Explorer view, analyze the description of your ABAP cloud project. After the system ID, the logon client, and the user ID, it displays the logon language.

    5. If the logon language does not match the original language of your ABAP class, create a new project with the correct logon language.

  2. Navigate to the implementation of method GET_OUTPUT in local class LCL_CARRIER.

    1. For example, you can open the Local Types tab in the editor and search for METHOD get_output.

    2. Alternatively, you can expand ZCL_##_SOLUTIONLCL_CARRIER in the Outline view on the left and choose GET_OUTPUT.

  3. Adjust the string template in the first APPEND statement. Replace the literal text with an embedded text literal.

    Hint

    Keep in mind that the curly brackets for embedded expressions require blank spaces on the inside.
    1. Adjust the code as follows:

      Code Snippet
      Copy code
      Switch to dark mode
      123
      APPEND |Carrier Name: { me->name } | TO r_result.
  4. Link the text literal to a text symbol (suggested ID for the text symbol: 001).

    Hint

    Keep in mind that no blanks are allowed inside the brackets or between the closing quote and the opening bracket.
    1. Adjust the code as follows:

      Code Snippet
      Copy code
      Switch to dark mode
      123
      APPEND |{ 'Carrier Name:' } { me->name } | TO r_result.
  5. Repeat this for the literal text in the other string templates. (Suggested IDs for the text symbols: 002, 003, 004).

    1. Adjust the code as follows:

      Code Snippet
      Copy code
      Switch to dark mode
      123456
      APPEND |{ 'Carrier Name:'(001) } { me->name } | TO r_result. APPEND |{ 'Passenger Flights:'(002) } { lines( passenger_flights ) } | TO r_result. APPEND |{ 'Average free seats:'(003) } { get_average_free_seats( ) } | TO r_result. APPEND |{ 'Cargo Flights:'(004) } { lines( cargo_flights ) } | TO r_result.
  6. Use a quick fix to create the first text in the text pool with a maximum length of 20.

    1. Place the cursor on (001) and press Ctrl + 1.

    2. Choose Create text 001 in text pool.

    3. Choose the slider to set the Maximum Length to 20 and choose Finish.

  7. Repeat this to create the other texts in the text pool. Use the same maximum length.

    1. Perform step as before.

  8. Analyze and activate the text pool.

    1. Hold down the Ctrl key and choose any of the text IDs to navigate to the text pool.

    2. The source code of the text pool should look like this:

      Code Snippet
      Copy code
      Switch to dark mode
      12345678910111213
      @MaxLength:20 001=Carrier Name: @MaxLength:20 002=Passenger Flights: @MaxLength:20 003=Average free seats: @MaxLength:20 004=Cargo Flights:
    3. While in the text pool press Ctrl + F3.

  9. Activate and test the global class as a console app.

    Note

    You should not see any difference. The improvement is, that the texts are translatable now. But this has no effect until the system administrator actually creates a translation project. But this is beyond the scope of this course.
    1. Return to the implementation of method GET_OUTPUT.

    2. Press Ctrl + F3.

    3. Press F9.

Log in to track your progress & complete quizzes