Where do all the texts go? The approach of a central TEXTS class for ABAP applications

This is about classic ABAP applications where we manage text symbols like this one:

result-message = 'Everything OK'(001).

My applications consist of many classes. And they are being refactored continuously during the development process. So I happen every now and then to move coding like the above from one class to another.

Obviously, I have to copy also the text element and all its translations then, and normally I will forget to do this 🙁

So I thought about an approach where I keep all my application’s texts in a central place. Here’s my solution

The TEXTS class

I use an abstract class which I call using the prefix for all classes of my application, followed by

_TEXTS

Here’s a simple example:

class zcl_cs72_texts definition public abstract create public . public section. class-methods class_constructor. class-data: begin of messages, no_mobile_data_found type text132, unknown_error type text132, end of messages. protected section. private section.
endclass. class zcl_cs72_texts implementation. method class_constructor. messages = value #( no_mobile_data_found = 'No mobile data found'(001) unknown_error = 'Unknown error occurred'(002) ). endmethod.
endclass.

As you see, I use structured data objects to store my texts, so they get a semantic meaning. In the CLASS_CONSTRUCTOR then, the values from the text elements are being assigned.

Define a subclass for easy access

In the consumer class, I derive a local class from this public using this in the class-relevant types :

class texts definition inheriting from zcl_cs72_texts final.
endclass.

Access the texts by meaningful variable names

This makes it simpler to access the texts from the coding, like this:

 result = cond #( when output_line-message is not initial then output_line-message else texts=>messages-unknown_error ).

Conclusion

With this approach, the problem of having to copy texts each time you reorganize your coding is solved. Furthermore, only one point remains to translate text elements.

Of course you should not use one text class for more than one application, This will cause unwanted dependencies.