*.properties files.
It has following functions added to ResourceBundle class packaged in JRE.
- To read all the ".properties" files in library modules and multiple modules in app projects
- To read multiple kinds of ".properties"
(
application, messages, enum_names, field_names) - To use "default" message by putting the postfix of the message ID ".default"
- To have the override function by java launch parameter (-D) or System.setProperty(...)
- To have the default locale setting function by .properties file (application_for_property-file-util_base.properties)
1. To read all the ".properties" files in library modules
and multiple modules in app projects
If we talk about messages[_xxx].properties,
this class reads ones in ecuacion libraries, and ones in your apps.
In ecuacion libraries an app is assumed to devided to some modules
(=usually called "projects" in IDE),
which are base, core, web (or none), batch.
If the name of your app is sample-app, module names would be :
sample-app-base : messages_base.properties
sample-app-core : messages_core.properties
sample-app-web : messages.properties
sample-app-batch: messages.properties
PropertyFileUtil.getMsg(...) will read all the messages properties above.
Duplicated definition detectable. (causes throwing exception)
And of course you can use localized files like messages_core_ja.properties
because This class uses ResourceBundle inside to read properties files.
2. To read multiple kinds of ".properties"
(application, messages, enum_names, field_names)
Firstly, In ecuacion-lib we have 4 kinds of property files.
PropertyFileUtil.getMsg(...) : messages[_xxx].properties
PropertyFileUtil.getApp(...) : application[_xxx].properties
PropertyFileUtil.getEnumName(...) : enum_names[_xxx].properties
PropertyFileUtil.getFieldName(...) : fiels_names[_xxx].properties
messages.properties and application.properties are well-known.
enum_names.properties stores the localized name of the enum element, and
field_names.properties stores the localized name of the entity field.
Usually these are also stored in messages.properties,
but it's kind of messy so divided files are prepared.
PropertyFileUtil supports these 4 kinds of properties files.
| kind | data the file has |
|---|---|
| application | system settings |
| messages | messages |
| field_names | names of the fields of items |
| enum_names | names of the elements of enums |
3. To use "default" message by putting the postfix of the message ID ".default"
4. To Have the override function by java launch parameter (-D)
or System.setProperty(...)
5. To Have the default locale setting function by .properties file
(application_for_property-file-util_base.properties)
When you don't prepare the properties file,
values are obtained from properties files without locale specification
(like message.properties, not like messages_en.properties).
ResourceBundle uses Locale.getDefault() but this is not good
because the resulting locale depends on the PC or the server the program is executing.
Of course when you want to depends on the PC or the server,
you just have to explicitly pass Locale.getDefault() to it.
Miscellaneous
messages[_xxx].properties, enum_names[_xxx].properties,
fiels_names[_xxx].properties need to have default locale file
(like messages.properties. This is the rule of the library.
It leads the conclusion that hasXxx(...) (like hasMsg(...))
doesn't need to have locale argument. (default locale used)
-
Method Summary
Modifier and TypeMethodDescriptionstatic voidaddResourceBundlePostfix(String postfix) Adds postfix dinamically.static StringReturns the value in application_xxx.properties.static StringgetEnumName(String key) Returns the enum name of default locale in enum_names_xxx.properties.static StringgetEnumName(Locale locale, String key) Returns the localized enum name in enum_names_xxx.properties.static StringgetFieldName(String key) Returns the field name of default locale in field_names_xxx.properties.static StringgetFieldName(Locale locale, String key) Returns the localized field name in field_names_xxx.properties.static StringReturns the value in application_xxx.properties.static StringReturns the value in messages_xxx.properties.static booleanReturns the existence of the key in application_xxx.properties.static booleanhasEnumName(String key) Returns the existence of the key in enam_names_xxx.properties.static booleanhasFieldName(String key) Returns the existence of the key in field_names_xxx.properties.static booleanReturns the existence of the key in field_names_xxx.properties.
-
Method Details
-
getApp
Returns the value in application_xxx.properties.Names should exist but some function uses this to show message ID when the key does not exist in the file.
- Parameters:
key- the key of the property- Returns:
- the value of the property
-
hasApp
Returns the existence of the key in application_xxx.properties.Names should exist but some function uses this to show message ID when the key does not exist in the file.
- Parameters:
key- the key of the property- Returns:
- boolean value that shows whether properties has the key
-
getMsg
Returns the value in application_xxx.properties.Names should exist but some function uses this to show message ID when the key does not exist in the file.
- Parameters:
key- the key of the propertyargs- message arguments, may benullwhich is treated asnew String[] {}.- Returns:
- the message corresponding to the message ID
-
getMsg
@Nonnull public static String getMsg(@Nullable Locale locale, @RequireNonnull String key, @RequireNonnull String... args) Returns the value in messages_xxx.properties.Names should exist but some function uses this to show message ID when the key does not exist in the file.
- Parameters:
locale- locale, may benullwhich means noLocalespecified.key- the key of the propertyargs- message arguments, may benullwhich is treated asnew String[] {}.- Returns:
- the message corresponding to the message ID
-
hasMsg
Returns the existence of the key in field_names_xxx.properties.Names should exist but some function uses this to show message ID when the key does not exist in the file.
- Parameters:
msgId- message ID- Returns:
- boolean value that shows whether properties has the message ID
-
getFieldName
Returns the field name of default locale in field_names_xxx.properties.Names should exist but some function uses this to show message ID when the key does not exist in the file.
- Parameters:
key- the key of the property- Returns:
- the value of the property
-
getFieldName
Returns the localized field name in field_names_xxx.properties.Names should exist but some function uses this to show message ID when the key does not exist in the file.
- Parameters:
locale- locale, may benullwhich is treated asLocale.getDefault().key- the key of the property- Returns:
- the value of the property
-
hasFieldName
Returns the existence of the key in field_names_xxx.properties.Names should exist but some function uses this to show message ID when the key does not exist in the file.
- Parameters:
key- the key of the property- Returns:
- boolean value that shows whether properties has the key
-
getEnumName
Returns the enum name of default locale in enum_names_xxx.properties.Names should exist but some function uses this to show message ID when the key does not exist in the file.
- Parameters:
key- the key of the property- Returns:
- the value of the property
-
getEnumName
Returns the localized enum name in enum_names_xxx.properties.Names should exist but some function uses this to show message ID when the key does not exist in the file.
- Parameters:
locale- locale, may benullwhich is treated asLocale.getDefault().key- the key of the property- Returns:
- the value of the property
-
hasEnumName
Returns the existence of the key in enam_names_xxx.properties.Names should exist but some function uses this to show message ID when the key does not exist in the file.
- Parameters:
key- the key of the property- Returns:
- boolean value that shows whether properties has the key
-
addResourceBundlePostfix
Adds postfix dinamically.If you add
testfor example,messages_test[_xxx].properties, application_test[_xxx.properties, ...} are searched.- Parameters:
postfix- postfix
-