Custom Spring Boot starter:你如何向MessageSource贡献i18n消息? [英] Custom Spring Boot starter: how do you contribute i18n messages to the MessageSource?

查看:259
本文介绍了Custom Spring Boot starter:你如何向MessageSource贡献i18n消息?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在编写一个自定义的Spring Boot启动程序,其他开发人员会将其放入他们的应用程序中,这个启动程序包含开箱即用的控制器和UI屏幕。

I'm writing a custom Spring Boot starter that other developers will drop into their applications, and this starter contains out-of-the-box controllers and UI screens.

这些UI屏幕是国际化的,i18n键/值在包文件中: com / foo / wherever / i18n.properties

These UI screens are internationalized and the i18n keys/values are in a package file: com/foo/wherever/i18n.properties.

我想确保在启动时加载我的启动器时,这些i18n.properties在应用程序的 MessageSource 中自动显示,以便我的UI页面work(通过普通的Spring Controller + ViewResolver + View实现呈现),而app开发者不必自己指定这个文件

I want to ensure that when my starter is loaded at startup, that these i18n.properties are available in the application's MessageSource automatically so that my UI pages work (rendered via normal Spring Controller + ViewResolver + View implementations) without the app developer having to specify this file themselves.

换句话说,他们应该能够将我的启动器添加到他们的运行时类路径中,一切都正常工作而无需配置任何东西。

In other words, they should be able to add my starter to their runtime classpath and everything 'just works' without the need to configure anything.

现在,我发现应用程序开发人员可以创建自己的 src / main / resources / messages.properties 文件和在 application.properties 中手动配置其他消息文件:

Now, I have discovered that the app developer can create their own src/main/resources/messages.properties file and manually configure the additional messages file in application.properties:

spring.messages.basename = messages, com.foo.wherever.i18n

这将有效。

但是,这需要以下两项:

However, this requires both of the following:


  1. 他们必须手动配置 spring.messages.basename 属性 - 它不是自动的。和

  2. 他们的应用程序类路径中必须有自己的 messages.properties 文件。如果 messages.properties 文件不存在, spring.messages.basename 甚至不起作用。即使他们不关心i18n,这仍然是必需的 - 不可取。

  1. They must manually configure the spring.messages.basename property - it's not automatic. and
  2. They must have their own messages.properties file in their application classpath. If a messages.properties file does not exist, spring.messages.basename doesn't even work. Even if they don't care about i18n, this is still required - not desirable.

我想我可以将我的i18n.properties文件移动到启动器.jar中的类路径:/messages.properties文件中,但这似乎不是一个好的解决方案:如果app dev有自己的messages.properties文件,其中只有一个会被读取,导致缺少消息值。

I suppose I could move my i18n.properties file to a classpath:/messages.properties file in the starter .jar, but that doesn't seem like a good solution: if the app dev has their own messages.properties file only one of them would be read, resulting in missing message values.

好像是Spring Boot MessageSourceAutoConfiguration 应该具有<$ c $的概念c> CompositeMessageSource 迭代一个或多个可用的 MessageSource 实例(以及订单 ed)在Spring ApplicationContext中, 由DispatcherServlet使用。这将允许任何首发者通过在其自动配置中声明 MessageSource 来为可用消息做出贡献

It seems as if Spring Boot MessageSourceAutoConfiguration should have a concept of a CompositeMessageSource that iterates over one or more MessageSource instances that are available (and Ordered) in the Spring ApplicationContext and that is used by the DispatcherServlet. This would allow any starter to contribute to the available messages just by declaring a MessageSource in their auto config

是否可以做我的要求吗?应用程序开发人员最关键的解决方案是什么?

Is it possible to do what I ask? What is the most 'hands off' solution for the app developer?

推荐答案

也许这是远景但您可以尝试使用 BeanFactoryPostProcessor

Maybe it is long shot but you can try to use BeanFactoryPostProcessor.

想法如下:


  1. 拿messageSourcebean超出应用程序的上下文。请注意,它可能但不一定是弹簧靴,例如,开发人员想要使用自己的实现,不要使用spring boot autoconfiguration。

  1. Take "messageSource" bean out of the application context. Note that it might but does not have to be a spring boot's one if e.g. developer wants to use its own implementation and don't use spring boot autoconfiguration.

将其替换为您自己的实现,尝试解析您的密钥和将委托委托给原始消息源。反之亦然,如果你想让开发人员覆盖你的翻译(如果原始消息源没有为未知密钥抛出异常,可能会出现问题)。

Replace it with your own implementation that tries to resolve "your keys" and the rest delegate to original message source. Or vice versa if you want to make possible to override your translations by developer (there can be problems if original message source does not throw exception for unknown keys).

但可能有更好的方法。

这篇关于Custom Spring Boot starter:你如何向MessageSource贡献i18n消息?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆