Technical Articles
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.
Is that a little bit like enumeration classes? In any case: I like approaches to express meanings via sourc code.
Well, a bit. You could divide your text class in one class for each structure, so you are even nearer to enumeration classes. However, an enumeration class would use constants instead of data that has to be populated with variable data, right?
However, dividing the text class for each group of texts would lead to a lot of overhead. For each class you have to declare the inheriting one and afterwards you have to translate each class one by one.
Thanks for sharing.
I must say I understand your basic idea, but not your use case / example (How/where these texts are being used).
Isn't it a good use case for exception class?
In my example, which is a real-life one, we are already in the CATCH-branch of a TRY/CATCH/ENDTRY and are handling the exception.
The example shows how to use the text defined in the TEXTS class. Without the text class the coding would have been:
And you would have to translate the text in the actual class.
Doesn't the exception class already include this message text?
This is a fallback. If an impropriate exception (no message text) is being raised, I didn't want to leave the message empty.