简体   繁体   English

spring本地化消息持久性

[英]spring localized message persistence

I would ask if know some "standard" way how to persist messages, that are to be localized later. 我会问是否知道一些“标准”方式如何保留消息,这些消息将在以后进行本地化。 Note, that this includes also the message parameters. 请注意,这还包括消息参数。 For example, I have message with code "msg1" and 1 parameter in resource bundle: 例如,我在资源包中有代码“msg1”和1个参数的消息:

msg1 = Hello {0}

and I want to associate the message with an object, persist it. 我想将消息与一个对象关联起来,坚持下去。 Than, later, different clients will ask the object with different language settings. 以后,不同的客户端会询问具有不同语言设置的对象。

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

I can imagine: 我能想象:

  1. to store message as top-level object with message code and parameters. 将消息存储为具有消息代码和参数的顶级对象。 I am afraid of performance - having separate object in separate table seems to me much worse than single varchar column in case of simple error messages without translation. 我害怕性能 - 在单独的varchar列中,如果没有转换的简单错误消息,在单独的表中使用单独的对象似乎要差得多。
  2. to encode message code and its parameters in string representation and use some hibernate custom type mapping 在字符串表示中编码消息代码及其参数,并使用一些hibernate自定义类型映射
  3. But, isn't it already solved (preferably in spring) so I don't have to do it again? 但是,它是不是已经解决了(最好在春天),所以我不必再这样做了?

Thanks 谢谢

Update: currently, we do some half-way - we are storing message as Serialized message object in single column, within the mapped table. 更新:目前,我们做了一些中途 - 我们将消息作为序列化消息对象存储在映射表中的单列中。 I'm still to fully satisfied - viewing data directly in database is not possible. 我仍然完全满意 - 直接在数据库中查看数据是不可能的。

I don't think yours is a very common need, so there's probably nothing available out there. 我不认为你的是一个非常普遍的需求,因此可能没有什么可用的。 Pretty sure that nothing comes with Spring out of the box. 很确定Spring没有开箱即用。 So, option 3 is discarded. 因此,选项3被丢弃。

The decision between 1 and 2 is mainly a business one. 1和2之间的决定主要是商业决策。 Imagine the message format changes after it is persisted (eg due to some mistake). 想象一下,消息格式在持久化后会发生变化(例如由于某些错误)。 What do you want to get now? 你现在想得到什么? Probably the corrected one, which can only be obtained with option 1. 可能是修正过的,只能通过选项1获得。

On the other hand, if you want to save it as a proof of what the application said to the user, both 1 and 2 can be used (but in the first case, you'll need to save message format instead of its code). 另一方面,如果您想将其保存为应用程序对用户的内容的证明,则可以使用1和2(但在第一种情况下,您需要保存消息格式而不是其代码) 。

In my opinion, the first option is the best. 在我看来,第一个选择是最好的。 You wouldn't be so worried about its performance, because it's probably not a bottleneck in your application. 你不会那么担心它的性能,因为它可能不是你应用程序的瓶颈。 Maybe a many to many (instead one to many) relation between a message and parameters can be good and can save you some memory, but it isn't is main purpose. 也许消息和参数之间的多对多(而不是一对多)关系可以很好并且可以节省一些内存,但这不是主要目的。 In parameter table you can store some extra information, such as the kind of parameter. 在参数表中,您可以存储一些额外的信息,例如参数的类型 This way you can search easily, for example, all messages of type msg1 or messages where user Jakub is printed (because you stored that parameter as of the user kind). 这样,您可以轻松搜索,例如, msg1类型的所有消息或打印用户Jakub的消息(因为您存储了该用户类型的参数)。

Finally, never save serialized objects in your database. 最后,永远不要在数据库中保存序列化对象。 They are language dependent, you can't search for them, versioning problems can occur... and most importantly, at least in this case, you don't need it, since in the end all parameters must be transformed into strings. 它们依赖于语言,您无法搜索它们,可能会出现版本问题......最重要的是,至少在这种情况下,您不需要它,因为最后所有参数都必须转换为字符串。

The only use case to store i18n messages in database I see is when you'd like to have a UI in your application to edit them. 在我看到的数据库中存储i18n消息的唯一用例是,当您希望在应用程序中使用UI来编辑它们时。 Otherwise I see no point in complicating things and would advise using files instead. 否则我认为没有必要使事情变得复杂,而是建议使用文件。

First of all you should not ask the underlying storage (DB or file) every time you want to resolve a message. 首先,每次要解析消息时都不应询问底层存储(数据库或文件)。 They change very rarely and are read very often, so it's a great use case for caching. 它们很少变化并且经常被读取,因此它是缓存的一个很好的用例。

Next if you want message parameters to be localized, than simply keep them as separate messages in your storage and get the value of the parameter first and than pass it on when retrieveing the actual message. 接下来,如果您希望对消息参数进行本地化,则只需将它们作为单独的消息保存在存储中,然后首先获取参数的值,然后在检索实际消息时将其传递给它。

So by now you're probably wondering how can I make my application see the changes I do to my messages? 那么到现在为止,您可能想知道如何让我的应用程序看到我对邮件所做的更改? Well, just use a ReloadableResourceBundleMessageSource . 好吧,只需使用ReloadableResourceBundleMessageSource It does caching for you too! 它也为你缓存! But remember to put the files outside of the application archive (JAR, WAR, EAR, whatever)! 但请记住将文件放在应用程序存档之外(JAR,WAR,EAR等等)! It's a good practice to hold your configurations separated from code anyway. 无论如何,将配置与代码分开是一种很好的做法。 So yeah, ofcourse Spring has already sloved that! 所以是的,当然Spring已经解决了这个问题!

But if for some reason you still want to use DB to store messages, you'll have to implement your own MessageSource . 但如果由于某种原因你仍然想使用DB来存储消息,你将不得不实现自己的MessageSource I'd store the parameters in the same table. 我将参数存储在同一个表中。 And remember to use cache, and invalidate it when data in DB changes. 并记住使用缓存,并在数据库中的数据更改时使其无效。

AFAIK Spring does not support storing i18n messages in the DB OOTB. AFAIK Spring不支持在DB OOTB中存储i18n消息。 However, you can create your own ResourceBundle that will fetch them from the DB. 但是,您可以创建自己的ResourceBundle,它将从数据库中获取它们。

There are many examples on the on how to achieve this: http://forum.springsource.org/showthread.php?15223-AbstractMessageSource-using-DB-table-instead-of-props-file 关于如何实现这一目标有很多例子: http//forum.springsource.org/showthread.php?15223-AbstractMessageSource-using-DB-table-instead-of-props-file

Database backed i18n for java web-app 数据库支持i18n for java web-app

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM