guide i18n - rfreier/oasp4j GitHub Wiki

Table of Contents

Internationalization

Internationalization (I18N) is about writing code independent from locale-specific informations. For I18N of text messages we are suggesting mmm native-language-support.

In OASP we have developed a solution to manage text internationalization. OASP solution comes into two aspects:

  • Bind locale information to the user.

  • Get the messages in the current user locale.

Binding locale information to the user

We have defined two different points to bind locale information to user, depending on user is authenticated or not.

  • User not authenticated: Oasp intercepts unsecured request and extract locale from it. At first, we try to extract a language parameter from the request and if it is not possible, we extract locale from Àccept-language` header.

  • User authenticated. During login process, applications developers are responsible to fill language parameter in the UserProfile class. This language parameter could be obtain from DB, LDAP, request, etc. In OASP sample we get the locale information from database.

This image shows the entire process:

oasp i18n

Getting internationalizated messages

OASP has a bean that manage i18n message resolution, the ApplicationLocaleResolver. This bean is responsible to get the current user and extract locale information from it and read the correct properties file to get the message.

The i18n properties file must be called ApplicationMessages_la_CO.properties where la=language and CO=country. This is an example of a i18n properties file for English language to translate OASP sample user roles:

ApplicationMessages_en_US.properties

waiter=Waiter
chief=Chief
cook=Cook
barkeeper=Barkeeper

You should define an ApplicationMessages_la_CO.properties file for every language that your application needs.

ApplicationLocaleResolver bean is injected in AbstractComponentFacade class so you have available this bean in logic layer so you only need to put this code to get an internationalizated message:

String msg = getApplicationLocaleResolver().getMessage("mymessage");
⚠️ **GitHub.com Fallback** ⚠️