简体   繁体   English

如何从.properties文件中获取字符串并在同一.properties文件中使用它

[英]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? 有没有一种方法可以检索.properties文件中的var值,并且可以在同一.properties文件中使用它?

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') 我想要这样的东西(在这里我会自动获得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." 在此示例中,“ SomeValue”属性的计算结果为“鞋子,船只和密封蜡”。

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

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