0

Is it possible to disable the arg {0} output when it is not available? Example:

messageSource.getMessage("my.message.property", new Object[]{12}, "fallback", LocaleContextHolder.getLocale());

this displays:

Child 12 Years old

now sometimes I don't have the age of the children, so my args would be:

messageSource.getMessage("my.message.property", null, "fallback", LocaleContextHolder.getLocale());

the display is now:

Child {0} Years old

Is it possible to disable the {0} on the output?

FunkyMan
  • 309
  • 5
  • 13
  • 2
    You want your UI to show "Child Years old"? Wouldn't it be better to wrap an if/else around it and show "Child age unknown", or something similar? – David Lavender Jul 10 '18 at 10:45
  • No, the real output is "Child 12". this above is an example. – FunkyMan Jul 10 '18 at 11:00
  • I would use two different message codes: "message.child-without-age=Child" and "message.child-with-age=Child: {0} years old". Trying to use a single "my.message.property" for both limits your flexibility. – David Lavender Jul 10 '18 at 12:21

2 Answers2

0

I found a simple solution:

message.replaceAll("\\s*\\{\\d+\\}\\s*", ""));

Another solution is to define multiple message properties. One with the Argument and another one without the argument

FunkyMan
  • 309
  • 5
  • 13
0

I hope your looking for avoid the null/undefined filed hiding. we can use the if condition in UI if the argument is not available/null, please check this link for your reference.

Thymeleaf: check if a variable is defined

srinivas
  • 1
  • 3