Post

Exporting SAP STERM Translations to JSON

Exporting SAP STERM Translations to JSON

SAP offers a rich set of predefined translations in their STERM (Terminology/Glossary) database, which is a valuable resource for localization and translation. These translations are accessible through a CDS View and can be exported for use in other tools like Weblate translation memory. In this post, we’ll explore how you can create a CDS View and a simple ABAP report to export translations in a convenient format like JSON.


🛠️ CDS View for Easy Access to STERM Data

To make accessing translations from STERM more straightforward, the first step is to create a CDS (Core Data Services) view. This view will allow you to pull data from the STERM tables and filter the translations based on source and target languages.

Here’s how you can define a simple CDS View for STERM translations:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
@AbapCatalog.sqlViewName: 'ZCA_STERM_V'
@AbapCatalog.compiler.compareFilter: true
@AbapCatalog.preserveKey: true
@AccessControl.authorizationCheck: #CHECK
@EndUserText.label: 'Translations from STERM'
define view ZCA_STERM_TRANSLATION
  as select from sterm_ob_v as source
    inner join   sterm_ob_v as destination
      on  destination.concept    = source.concept
      and destination.object_typ = '' {
  source.langu as langu_source,
  source.text as text_source,
  source.text_uc as text_uc_source,
  destination.langu as langu_destination,
  destination.text as text_destination,
  count(*) as counter
}
where
      source.object_typ =  ''
  and source.langu      <> destination.langu
group by
  source.langu,
  source.text,
  source.text_uc,
  destination.langu,
  destination.text

📋 Key Points:

  • Source Language and Destination Language: The view filters translation pairs where the source and destination languages are different.
  • Group By Clause: It groups translations by the language and text, ensuring unique pairs are fetched.
  • Inner Join: The translation data is joined on the concept, ensuring accurate translations between the source and destination languages.

📊 ABAP Report to Export Translations

Once you have your CDS View set up, the next step is to create a simple ABAP report. This report will extract the translations from your STERM data, then export them as a JSON file. Here’s how you can structure the report:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
REPORT zexport_sterm.

PARAMETERS: p_slangu TYPE zca_sterm_v-langu_source OBLIGATORY DEFAULT 'EN',
            p_tlangu TYPE zca_sterm_v-langu_destination OBLIGATORY.

START-OF-SELECTION.
  PERFORM start.

FORM start.

  TYPES: BEGIN OF t_translation_memory,
           source          TYPE string,
           source_language TYPE string,
           target          TYPE string,
           target_language TYPE string,
           origin          TYPE string,
           category        TYPE i,
         END OF t_translation_memory.

  DATA: lt_export TYPE HASHED TABLE OF t_translation_memory
                  WITH UNIQUE KEY source source_language.

  " get all translations from the STERM tables
  SELECT *
    FROM zca_sterm_v
    INTO TABLE @DATA(lt_data)
    WHERE langu_source EQ @p_slangu
      AND langu_destination EQ @p_tlangu
      ORDER BY counter DESCENDING.

  LOOP AT lt_data INTO DATA(ls_data)
    GROUP BY ( langu_source = ls_data-langu_source
               text_source = ls_data-text_source ).

    DATA(ls_export) = VALUE t_translation_memory( ).

    CHECK ls_data-text_source IS NOT INITIAL AND ls_data-text_destination IS NOT INITIAL.

    ls_export-source_language = cl_i18n_languages=>sap1_to_sap2( ls_data-langu_source ).
    ls_export-source = ls_data-text_source.
    ls_export-target_language = cl_i18n_languages=>sap1_to_sap2( ls_data-langu_destination ).
    ls_export-target = ls_data-text_destination.
    ls_export-origin = space.
    ls_export-category = 1.

    INSERT ls_export
      INTO TABLE lt_export.

  ENDLOOP.

  IF lt_export IS INITIAL.
    RETURN.

  ENDIF.  

  " transform data to JSON
  DATA(lf_json) = /ui2/cl_json=>serialize( 
                    data = lt_export 
                    pretty_name = /ui2/cl_json=>pretty_mode-low_case ).

  TRY.
      DATA: lv_action TYPE i.
      DATA: lv_filename TYPE string.
      DATA: lv_fullpath TYPE string.
      DATA: lv_path TYPE string.
      DATA: lt_json TYPE TABLE OF string.

      " call save dialoag
      cl_gui_frontend_services=>file_save_dialog( 
        EXPORTING
          default_file_name = 'texts.json'
          default_extension = 'json'
          file_filter       = |JSON (*.json)\|*.json\|{ cl_gui_frontend_services=>filetype_all }|
        CHANGING
          filename          = lv_filename
          path              = lv_path
          fullpath          = lv_fullpath
          user_action       = lv_action ).

      IF lv_action EQ cl_gui_frontend_services=>action_ok.
        APPEND lf_json
          TO lt_json.


        " export file to local storage
        cl_gui_frontend_services=>gui_download( 
          EXPORTING
            filename     = lv_fullpath
            filetype     = 'ASC'
          CHANGING
            data_tab     =  lt_json ).

      ENDIF.
    CATCH cx_root INTO DATA(lo_text).
      MESSAGE lo_text->get_text( ) TYPE 'I'.
  ENDTRY.

ENDFORM.

📑 Explanation of the Report:

  • Input Parameters: The report accepts the source language (p_slangu) and target language (p_tlangu) as input.
  • Main Logic:
    • The translations are selected from the CDS View based on the language parameters.
    • It loops over the translations, creating a table of translation pairs.
    • The resulting data is serialized into a JSON format using the /ui2/cl_json class.
  • File Download: A dialog is triggered to allow the user to save the resulting JSON file locally.

🧠 Final Thoughts

Exporting translations from STERM can be incredibly useful, especially if you’re looking to integrate SAP’s terminology into other systems like Weblate. With the CDS View and ABAP report, you now have a simple, repeatable process to extract translations in a variety of formats, like JSON.

Whether you’re localizing SAP apps or creating a custom translation workflow, this method gives you the flexibility to work with the data in the tool of your choice. Plus, with minor adjustments, you can adapt the report to export in formats like XML or properties files.

Happy coding, and may your translations be seamless!

This post is licensed under CC BY 4.0 by the author.