Spring boot i18n for multiple users

497 Views Asked by At

Currently, I am using Spring i18n for internationalization in my application. The application will have around 5 different languages (may be changed later).

To show labels according to the language selected by the user, the frontend will use the keys that are the same that of the keys from messages.properties in the backend.

The frontend will call REST API with the keys they need in order to show on the basis of the locale they pass. The API looks like:

@PostMapping(value = "/internationalization")
public ResponseEntity<Map<String, String>> translate(@RequestBody List<String> keys) {
    Map<String, String> translatedValues = new HashMap<>();
    keys.forEach(k -> {
        translatedValues.put(k, this.messageSource.getMessage(k, null, LocaleContextHolder.getLocale()));
    });
    return ResponseEntity.ok(translatedValues);
}

Everything was working fine. All of a sudden, I got some additional requirements that are too confusing to me. The requirement is

Let's say User-1 enters some values in the Chinese language which will be saved as is in the database. Now, is it possible for User-2 who has the French language as the default language to see those contents entered by User-1 in the French language? Likewise, I have to support it for five different languages for now.

How do I accommodate this kind of requirement?

I know Google Translation provides API for translation but how efficient will it be? Please, share your experience.

Thanks a lot in advance.

0

There are 0 best solutions below