You’ve probably already read about the messages.properties file. It allows you to define any strings you wish to present to an end-user.
您可能已经阅读了messages.properties文件。 它允许您定义希望呈现给最终用户的任何字符串。
messages.properties:
messages.properties :
To retrieve one of these strings in code you first need to get a MessageSource. This is provided via spring dependency injection.
要在代码中检索这些字符串之一,您首先需要获取一个MessageSource 。 这是通过spring依赖注入来提供的。
Constructor injection:
构造函数注入:
@Service // Or some other `Component` annotationclass SomeService( private val messageSource: MessageSource )Property injection:
属性注入:
@Autowired private lateinit var messageSource: MessageSourceThen you can call getMessage:
然后可以调用getMessage:
messageSource.getMessage("title.example", null, LocaleContextHolder.getLocale())args can be provided for string replacement. For example:
可以提供args来替换字符串。 例如:
messageSource.getMessage("message.hello", arrayOf("Joe Bloggs"), LocaleContextHolder.getLocale())Notice above, we pass LocaleContextHolder.getLocale() as a Locale to the getMessage call. This tells the MessageSource which language to provide.
上面的通知中,我们将LocaleContextHolder.getLocale()作为Locale传递给getMessage调用。 这告诉MessageSource提供哪种语言。
LocaleContextHolder provides the current Locale that spring is aware of. This is a powerful mechanism. We don’t need to add any custom code to our RestController’s or otherwise to determine Locale. Callers can set the locale as a request header and spring will make that available.
LocaleContextHolder提供spring知道的当前Locale 。 这是一个强大的机制。 我们不需要在RestController上添加任何自定义代码,也RestController确定Locale 。 调用者可以将语言环境设置为请求标头,spring会使其可用。
To provide other language strings we add a new messages file, messages_fr.properties:
为了提供其他语言字符串,我们添加一个新的消息文件messages_fr.properties :
To learn more, check out this guide.
要了解更多信息,请查看本指南 。
When modularizing your application you may run into trouble. By default the messages.properties files from each module will conflict with each other. Only one set of messages will be available.
在对应用程序进行模块化时,您可能会遇到麻烦。 默认情况下,来自每个模块的messages.properties文件将相互冲突。 只有一组消息可用。
To avoid this, all your messages files need to have different names. A good convention to follow is to use your module name. For example: messages-application.properties, messages-application_fr.properties, messages-library.properties, messages-library_fr.properties.
为了避免这种情况,所有消息文件都需要使用不同的名称。 遵循的一个好习惯是使用模块名称。 例如: messages-application.properties , messages-application_fr.properties , messages-library.properties , messages-library_fr.properties 。
Now we need to expose these to the MessageSource. By default, only files named message.properties will be found.
现在我们需要将这些暴露给MessageSource 。 默认情况下,仅找到名为message.properties文件。
In your application.properties file add:
在您的application.properties文件中添加:
spring.messages.basename=messages-application,messages-libraryNotice we do not include the language extension or the file extension here.
请注意,此处我们不包括语言扩展名或文件扩展名。
That’s it. You can now provide strings in each of your different modules.
而已。 现在,您可以在每个不同的模块中提供字符串。
Originally published at https://www.brightec.co.uk.
最初在 https://www.brightec.co.uk上 发布 。
翻译自: https://levelup.gitconnected.com/internationalize-your-multi-module-spring-boot-application-33dc83c36dc6