简体   繁体   中英

How to get a string from .properties file and use it in the same .properties file

Is there a way to retrieve the value of a var in a .properties file and use is inside the same .properties file?

Insted of this (where I have to write manually the words 'Main Menu' in every line)

mainMenuTitle = Main Menu
NotBlank.menu.title = the field 'Main Menu' can't be null
Size.menu.title = the field 'Main Menu' must contains 10 characters

I want something like this (where I get automatically the value of the var 'mainMenuTitle')

mainMenuTitle = Main Menu     
NotBlank.menu.title = the field **mainMenuTitle** can't be null
Size.menu.title = the field **mainMenuTitle** must contains 10 characters

You can get both message separately and then make a replace to inject the title

   @Inject
  public MessageSource messageSource;


public static String getMessage(String messageKey1, String messageKey12) {
    String title =  messageSource.getMessage(messageKey1, arguments, locale);
    String message =  messageSource.getMessage(messageKey2, arguments, locale).replace("**mainMenuTitle**", title);

}

This May be what you want, its a bit old , but may work for your needs. Enabling constant substitution in Property Values You can substitute a constant anywhere in the property value, and even have more than one constant within a value, as in the following example:

CONST_1 = shoes and ships CONST_2 = sealing wax SomeValue = {CONST_1} and {CONST_2}

In this example, the "SomeValue" property evaluates to "shoes and ships and sealing wax."

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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