Skip to content

Latest commit

 

History

History
55 lines (33 loc) · 2.02 KB

I18N.md

File metadata and controls

55 lines (33 loc) · 2.02 KB

Internationalization

The language with which internationalization will be applied comes from the frontend as a header in the requests, called X-Locale.

How can I add support to another language?

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.

How can I internationalize a message?

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");

How can I internationalize a bean validation message?

Suppose we want to internationalize the message launched by @NotNull, then we must do the following:

@NotNull(message="{familyDTO.name.notNull}")
private String name;