*.properties files.
It has following features added to ResourceBundle class packaged in JRE.
- To read all the ".properties" files in library modules and multiple modules in projects of an app.
- To read multiple kinds of ".properties"
(
application, messages, enum_names, item_names, ValidationMessages, ValidationMessagesWithField) - To remove default locale from candidate locales
- 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 resolve property keys in the obtained value
- To resolve property keys in arguments
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, item_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.getItemName(...) : item_names[_xxx].properties
messages.properties and application.properties are well-known.
enum_names.properties stores the localized name of the enum element, and
item_names.properties stores the localized name of the item.
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 |
| item_names | names of items |
| enum_names | names of the elements of enums |
3. To remove default locale from candidate locales
Java Standard ResourceBundle uses default locale
(which is obtained by Locale.getDefault())
when the property file of specified locale is not found.
The default locale is usually equal to the locale of the OS,
which means the result depends on the machine the program is executed on.
To avoid that situation deault locale is removed from candidate locales with this class.
4. To use "default" message by putting the postfix of the message ID ".default"
5. To Have the override function by java launch parameter (-D)
or System.setProperty(...)
6. To resolve property keys in the obtained value
You can put a property key into a property value.
For example, you can define keys and values like this in messages.properties.
By executing PropertyFileUtil.getMsg("message") you'll get "a-b-c".
message=a-${messages:message_test1}-c
message_test1=b
Recursive resolution is also supported so you can even define like the one below.
By executing PropertyFileUtil.getMsg("message") you'll get "a-b-c-d-e-f-g".
message=a-${messages:message_test1}-c-${messages:message_test2}-g
message_test1=b
message_test2=d-${messages:message_test3}-f
message_test3=e
Examples above uses ${messages:...} but you can also use other file kinds
like ${application:...}, ${item_names:...} and ${enum_names:...}.
Recursive resolution is supported, but multiple layer of key is not supported. (which does not seem to be needed really)
message=a-${messages:${messages:message_prefix}_test1}-c
message_prefix=message
message_test1=b7. To resolve property keys in arguments
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)
-
Nested Class Summary
Nested ClassesModifier and TypeClassDescriptionstatic classIs considered as an argument string, but you can set message ID replaced to message string withPropertyFileUtil.getMsg(String). -
Method Summary
Modifier and TypeMethodDescriptionstatic voidaddResourceBundlePostfix(String postfix) Adds postfix dinamically.static StringReturns the property value of default locale.static StringReturns the localized property value.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 StringgetItemName(String key) Returns the item name of default locale in item_names_xxx.properties.static StringgetItemName(Locale locale, String key) Returns the localized item name in item_names_xxx.properties.static StringReturns the value of default locale in messages_xxx.properties.static StringgetMsg(String key, PropertyFileUtil.Arg[] args) Returns the value of default locale in messages_xxx.properties.static StringReturns the localized value in messages_xxx.properties.static StringgetMsg(Locale locale, String key, PropertyFileUtil.Arg[] args) Returns the localized value in messages_xxx.properties.static StringgetValidationMessage(String key, Map<String, String> argMap) Returns the property value of default locale in ValidationMessages[_locale].properties.static StringReturns the localized enum name in ValidationMessages[_locale].properties.static StringgetValidationMessageWithField(String key, Map<String, String> argMap) Returns the property value of default locale in ValidationMessagesWithField_xxx.properties.static StringReturns the localized enum name in enum_names_xxx.properties.static booleanReturns the existence of the key.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 booleanhasItemName(String key) Returns the existence of the key in item_names_xxx.properties.static booleanReturns the existence of the key in messages_xxx.properties.
-
Method Details
-
getApp
Returns the value in application_xxx.properties.- Parameters:
key- the key of the property- Returns:
- the value of the property
-
hasApp
Returns the existence of the key in application_xxx.properties.- Parameters:
key- the key of the property- Returns:
- boolean value that shows whether properties has the key
-
getMsg
Returns the value of default locale in messages_xxx.properties.- Parameters:
key- the key of the propertyargs- message arguments- Returns:
- the value (message) of the property key (message ID)
-
getMsg
@Nonnull public static String getMsg(@Nullable Locale locale, @RequireNonnull String key, @RequireNonnull String... args) Returns the localized value in messages_xxx.properties.- Parameters:
locale- locale, may benullwhich means noLocalespecified.key- the key of the propertyargs- message arguments- Returns:
- the value (message) of the property key (message ID)
-
getMsg
@Nonnull public static String getMsg(@RequireNonnull String key, @RequireNonnull PropertyFileUtil.Arg[] args) Returns the value of default locale in messages_xxx.properties.- Parameters:
key- the key of the propertyargs- message arguments, which can be message ID. The data type isArg[], notArg...because ifArgcauses an error when you callgetMsg(key)since the second parameter is unclear (String...orArg....- Returns:
- the value (message) of the property key (message ID)
-
getMsg
@Nonnull public static String getMsg(@Nullable Locale locale, @RequireNonnull String key, @RequireNonnull PropertyFileUtil.Arg[] args) Returns the localized value in messages_xxx.properties.- Parameters:
locale- locale, may benullwhich means noLocalespecified.key- the key of the propertyargs- message arguments, which can be message ID. The data type isArg[], notArg...because ifArgcauses an error when you callgetMsg(key)since the second parameter is unclear (String...orArg....- Returns:
- the message corresponding to the message ID
-
hasMsg
Returns the existence of the key in messages_xxx.properties.- Parameters:
key- the key of the property- Returns:
- boolean value that shows whether properties has the key (message ID)
-
getItemName
Returns the item name of default locale in item_names_xxx.properties.- Parameters:
key- the key of the property- Returns:
- the value of the property
-
getItemName
Returns the localized item name in item_names_xxx.properties.- Parameters:
locale- locale, may benullwhich is treated asLocale.getDefault().key- the key of the property- Returns:
- the value of the property
-
hasItemName
Returns the existence of the key in item_names_xxx.properties.- 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.- Parameters:
key- the key of the property- Returns:
- the value of the property
-
getEnumName
Returns the localized enum name in enum_names_xxx.properties.- 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.- Parameters:
key- the key of the property- Returns:
- boolean value that shows whether properties has the key
-
getValidationMessage
@Nonnull public static String getValidationMessage(@RequireNonnull String key, Map<String, String> argMap) Returns the property value of default locale in ValidationMessages[_locale].properties.Usually
ValidationMessages[_locale].propertiesfile satisfies validation message's requirement. But when you want to show error messages on the top message space and- Parameters:
key- the key of the propertyargMap- argMap- Returns:
- the value of the property
-
getValidationMessage
@Nonnull public static String getValidationMessage(@Nullable Locale locale, @RequireNonnull String key, Map<String, String> argMap) Returns the localized enum name in ValidationMessages[_locale].properties.- Parameters:
locale- locale, may benullwhich is treated asLocale.getDefault().key- the key of the property- Returns:
- the value of the property
-
getValidationMessageWithField
@Nonnull public static String getValidationMessageWithField(@RequireNonnull String key, Map<String, String> argMap) Returns the property value of default locale in ValidationMessagesWithField_xxx.properties.Usually
ValidationMessages[_locale].propertiesfile satisfies validation message's requirement. But when you want to show error messages on the top message space and- Parameters:
key- the key of the propertyargMap- argMap- Returns:
- the value of the property
-
getValidationMessageWithField
@Nonnull public static String getValidationMessageWithField(@Nullable Locale locale, @RequireNonnull String key, Map<String, String> argMap) Returns the localized enum name in enum_names_xxx.properties.- Parameters:
locale- locale, may benullwhich is treated asLocale.getDefault().key- the key of the property- Returns:
- the value of the property
-
get
@Nonnull public static String get(@RequireNonnull String propertyUtilFileKind, @RequireNonnull String key) Returns the property value of default locale.- Parameters:
propertyUtilFileKind- String value ofPropertyUtilFileKind(application, messages, ...)key- the key of the property- Returns:
- the value of the property
-
get
@Nonnull public static String get(@RequireNonnull String propertyUtilFileKind, @Nullable Locale locale, @RequireNonnull String key) Returns the localized property value.- Parameters:
propertyUtilFileKind- String value ofPropertyUtilFileKind(application, messages, ...)locale- locale, may benullwhich is treated asLocale.getDefault().key- the key of the property- Returns:
- the value of the property
-
has
Returns the existence of the key.- Parameters:
propertyUtilFileKind- String value ofPropertyUtilFileKind(application, messages, ...)key- the key of the property- Returns:
- the value of the property
-
addResourceBundlePostfix
Adds postfix dinamically.If you add
testfor example,messages_test[_lang].properties, application_test[_lang].properties, ...are searched.In java 9 module system environment, you also need to Service Provider Interface(SPI) defined in `ecuacion-lib-core`.
- Parameters:
postfix- postfix
-