Spring saving a localized message

I would ask if you know any "standard" way to transfer messages that should be localized later. Please note that this also includes message options. For example, I have a message with the code "msg1" and 1 parameter in the resource set:

msg1 = Hello {0} 

and I want to associate a message with an object, save it. Then, later, different clients will request an object with different language settings.

 obj.setDisplayMsg(msgSource.getMessage("msg1", "World", locale)) 

I can imagine:

  • for storing a message as a top-level object with code and message parameters. I am afraid of performance - the presence of a separate object in a separate table seems to me much worse than a single varchar column in the case of simple error messages without translation.
  • to encode the message code and its parameters in a string representation and use some hibernate custom type mapping
  • But, isn't this allowed (preferably in spring), so I don't need to do this again?

thanks

Update: we are currently performing some actions halfway - we save the message as a Serialized message object in one column in the displayed table. I am still completely satisfied - viewing data directly in the database is not possible.

+4
source share
3 answers

I don’t think your need is very common, so there is probably nothing there. Pretty sure nothing comes with Spring out of the box. Thus, option 3 is discarded.

The solution from 1 to 2 is basically a business one. Imagine that the message format changes after it is saved (for example, due to some error). What do you want to get now? Probably a fixed one that can only be obtained with option 1.

On the other hand, if you want to save it as evidence that the application tells the user, you can use either 1 or 2 (but in the first case you need to save the message format instead of its code).

In my opinion, the first option is the best. You will not be so worried about its performance, because this is probably not the bottleneck in your application. Maybe many of the many (instead of one to many) relationships between the message and the parameters can be good and can save you some memory, but this is not the main goal. In the parameter table, you can save some additional information, such as the type of parameter. Thus, you can easily search, for example, all messages like msg1 or messages where the Jakub user is printing (since you saved this parameter as a user view).

Finally, never store serialized objects in your database. They depend on the language, you cannot look for them, problems with versions can arise ... and, most importantly, at least in this case you do not need it, since in the end all parameters must be converted to strings.

+5
source

The only use case for storing i18n messages in a database that I see is when you want your application to have a user interface for editing them. Otherwise, I see no reason to complicate the situation and would recommend using files instead.

First of all, you do not have to request the underlying storage (DB or file) every time you want to allow the message. They very rarely change and are read very often, so this is a great option for caching.

Further, if you want the message parameters to be localized, simply save them as separate messages in your repository and first get the parameter value, and then pass it when you receive the actual message.

So, now you are probably wondering, how can I make my application see the changes I make for my posts? Well, just use ReloadableResourceBundleMessageSource . It also caches you! But do not forget to put the files outside the application archive (JAR, WAR, EAR, whatever)! It is good practice to keep your configurations separate from code. So yes, of course, Spring has already launched this!

But if for some reason you still want to use the database to store messages, you will have to implement your own MessageSource . I would save the parameters in one table. And do not forget to use the cache and discard it when changing data in the database.

0
source

AFAIK Spring does not support storing i18n messages in OOTB DB. However, you can create your own ResourceBundle that will retrieve them from the database.

There are many examples of how to achieve this: http://forum.springsource.org/showthread.php?15223-AbstractMessageSource-using-DB-table-instead-of-props-file

I18n database support for java web application

-1
source

Source: https://habr.com/ru/post/1437944/


All Articles