简体   繁体   English

在 Spring 启动应用程序中传递常量消息的正确方法(messages.properties 或 Constant 类)

[英]Proper way of passing constant messages in Spring boot application (messages.properties or Constant class)

I have just noticed that the code I am working on has a messages.properties file where all the messages are written that should be passed.我刚刚注意到我正在处理的代码有一个messages.properties文件,其中写入了应该传递的所有消息。

My question is, why was this used instead of a Constants file where all the messages can be stored as constant Strings?我的问题是,为什么使用它而不是所有消息都可以存储为常量字符串的Constants文件? I mean when I write them as Strings I get the suggestions in my IDE for them.我的意思是,当我将它们编写为字符串时,我会在 IDE 中为它们提供建议。 Any specific reason why messages.properties approach was chosen?选择 messages.properties 方法的任何具体原因?

The messages.properties files are meant more to internationalization: messages.properties 文件更多地用于国际化:

By default, a Spring Boot application will look for message files containing internationalization keys and values in the src/main/resources folder: The file for the default locale will have the name messages.properties, and files for each locale will be named messages_XX.properties, where XX is the locale code.默认情况下,Spring Boot 应用程序将在 src/main/resources 文件夹中查找包含国际化键和值的消息文件:默认语言环境的文件名为 messages.properties,每个语言环境的文件名为 messages_XX。属性,其中 XX 是区域设置代码。

The keys for the values that will be localized have to be the same in every file, with values appropriate to the language they correspond to.将被本地化的值的键在每个文件中必须相同,并且值适合它们对应的语言。

If a key does not exist in a certain requested locale, then the application will fall back to the default locale value.如果某个键在某个请求的区域设置中不存在,则应用程序将回退到默认区域设置值。

It's possible to achieve internationalization with a Constants file but not that easily.使用 Constants 文件可以实现国际化,但不是那么容易。

Property files are not meant to be a substitute or an alternative to constants.属性文件并不是常量的替代品或替代品。 They are used to store configurable values such as an ip, languages, etc. Loading constants from a file defeats the purpose of a constant.它们用于存储可配置的值,例如 ip、语言等。从文件加载常量违背了常量的目的。

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

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