The language with which internationalization will be applied comes from the frontend as a header in the requests, called X-Locale
.
All messages supported by internationalization are stored in the /resources/i18n
directory, separated according to language.
The file for the default locale, in this case Spanish, will have the name messages.properties
, and files for each locale will be named messages_XX_YY.properties
, where XX
is the language code and YY
is the country code. For example, messages_es_PY for Paraguay.
The keys for the values that will be localized have to be the same in every file, with values appropriate to the language they correspond to.
The content of the files must follow the following format:
greeting=Hello {0} and {1}! Welcome to the application!
family.notExist=The family with id {0} doesn't exists!
familyDTO.name.notNull=The family name must not be null
The parameters are obtained with {N}
where N is the position of the passed parameter, N starts at 0.
The class I18n
that contains the translate
method must be included, this method is responsible for carrying out the translation according to the language that is sent by the frontend.
Let's suppose that we want to internationalize the following message so that it arrives at the frontend:
new CustomParameterizedException("The family with id "+familyId+" doesn't exists");
To internationalize it we should replace it with the following:
new CustomParameterizedException(i18n.translate("family.notExist", familyId));
If it is necessary to pass more parameters, they should go separator by comma one after another, for example:
i18n.translate("greeting", "John", "Mark");
Suppose we want to internationalize the message launched by @NotNull, then we must do the following:
@NotNull(message="{familyDTO.name.notNull}")
private String name;