【问题标题】:How to get a string from .properties file and use it in the same .properties file如何从 .properties 文件中获取字符串并在同一个 .properties 文件中使用它
【发布时间】:2015-09-19 14:58:52
【问题描述】:

有没有办法在 .properties 文件中检索 var 的值并在同一个 .properties 文件中使用?

Insted (我必须在每一行手动写下“主菜单”)

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

我想要这样的东西(我会自动获得 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

【问题讨论】:

  • 你能给出一些更大的代码吗?我不明白你到底想要什么。
  • 有可能。但是你接下来要做的是将附加信息放入文件中;为了“有用”……您还必须编写代码来解释您的属性字符串。含义:如果您定义 **string** 应该表示“变量名”......那么您的代码必须解析每个属性;并采取相应的行动!换句话说:没有对这些东西的内置支持;但你可以添加它(当然,这样的属性只适用于你的代码)。

标签: java internationalization properties-file


【解决方案1】:

您可以分别获取两条消息,然后进行替换以注入标题

   @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);

}

【讨论】:

    【解决方案2】:

    这可能是您想要的,它有点旧,但可能适合您的需求。 Enabling constant substitution in Property Values 您可以在属性值的任何位置替换一个常量,甚至可以在一个值中包含多个常量,如下例所示:

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

    在此示例中,“SomeValue”属性的计算结果为“鞋子、船舶和密封蜡”。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2011-12-21
      • 1970-01-01
      • 1970-01-01
      • 2016-12-14
      • 1970-01-01
      • 1970-01-01
      • 2018-07-26
      • 2012-06-30
      相关资源
      最近更新 更多