We use the req.error() method from the message API both in the implementation class for the AdminService from our scenario and in the implementation class for our CatalogService. We use this method to output error messages if validations fail. So far, we have hard-coded the reported messages as text literals.
In this lesson, we will learn how to internationalize messages. Instead of embedding them hard-coded, they are maintained in separate files - so-called text bundles - as key-value pairs. The keys are then used by the application code to access the corresponding texts. This means that the values for the keys are the actual language-specific texts. In order to support different languages, different files are maintained that provide translated texts for the same keys (see following figure).

Creating Bundle Files for Internationalizing Error Messages
The detailed procedure is as follows: Create a file with the name messages.properties in a folder called _i18n, i18n or assets/i18n. The folder for the messages.properties file must either be located directly below the project directory or in a directory that contains files that are used to define service models (such as the srv directory in our project).
Note
The names of the folders in which CAP searches for text bundles can be configured in thepackage.json
file of the project using the cds.i18n.folders
property. By default, these are the folder names _18n
, i18n
and assets/i18n
.The required texts are maintained as key-value pairs in the messages.properties file. Usually, the texts are maintained there in English. Placeholders can be used, which are noted in the form "{n}". n runs through the natural numbers starting with 0.
To support additional languages, additional files are created in the same folder as the messages.properties file according to the following naming convention: messages<_languageCode><_countryCode>.properties. The country code is optional. Examples of such file names are messages_de.properties, messages_fr_CA.properties or messages_es_MX.properties.
The additional files use the same keys as the messages.properties file. However, the values for the keys are translated according to the language code and, if applicable, the country code from the file name.
In the example shown, two .properties files have been created: messages.properties with English texts and messages_de.properties with the corresponding German translations.
Fallback Chain
Assume that a text is queried via a key when processing a request, whereby German was determined for the language. If a text is maintained for the key in the messages_de.properties file, this text is used by the application. However, if there is no messages_de.properties file or the key used is not maintained in the existing messages_de.properties file, CAP falls back to the messages.properties file and attempts to determine a value for the key via this file. If this is possible, the corresponding text is used by the application. If messages.properties does not contain a matching entry either, the application uses the key name as text.
Note
The base namemessages
for the text bundles is mandatory. This means that the files created must be named as follows: messages<_languageCode><_countryCode>.properties
. Technically, however, it is not necessary to create a raw file without suffixes with the name messages.properties
. English texts can also be maintained in a file called messages_en.properties
. In this case, however, you do not benefit from the fallback mechanism described above. This mechanism ensures that the texts from the messages.properties
file are used if no .properties
file has been created for a specific language or a specific key has not been maintained for a specific language in the related .properties
file.