Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
351 views
in Technique[技术] by (71.8m points)

java - Custom Spring Boot starter: how do you contribute i18n messages to the MessageSource?

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.

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

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.

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

And this will work.

However, this requires both of the following:

  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.

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.

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?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

I set this up in the following way. I'm only currently supporting en_US but it is setup to handle any number of languages using internationalization(i18n).

View the Code here

View the code gist here: Code on github gist

Add Message Source and Default locale Beans

Add these beans to your Application.java to set your default locale and configure the location of your message props

Message source and default locale


Create Message Service

Service will get the default locale from the session and then get the message text from your props

Setup Message svc


Use the Message service in Controller

Inject the message svc and then pass in the id to get the value from the props file

Use svc in controller


Add message.properties file in the locale

Goto /resources:

  • create the locale folder
  • create a file called messages_en_US.properties

message props


Read more

You can view a more complete article on this subject here: Spring Boot Internationalization i18n using Message Properties


Look at the Code

View the code gist here: Code on github gist


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...