I'm currently using the I18NBundle (wiki | docs) package from LibGDX to do some internationalization for my game. the I18NBundle internally uses the java MessageFormat (docs) to format strings such as this:

// The string to format.
shields = Shields: {0, number, integer}

// Do the formatting
bundle.format("shields", 5);
>> Shields: 5

It can also do conditionals like so:

// # = if exact
// < = if greater than
shields = Shields: {0, choice, 0# No Shields! | 1# One Shield! | 1< {0, number, integer} Shields!}

bundle.format("shields", 0);
>> Shields: No Shield!

bundle.format("shields", 1);
>> Shields: One Shield!

bundle.format("shields", 5);
>> Shields: 5 Shields!

Now I'm trying to compare two input parameters to each other. I tried this:

// If parameter 1 is greater than parameter 0...
score = Score: {0, choice, {1}< {0} New high score! | {0}}

bundle.format("score", newScore, highScore);

// Expected Output if new score is lesser than high score
bundle.format("score", 1, 5);
>> Score: 1

// Expected output if new score is greater than high score
bundle.format("score", 7, 5);
>> Score: 7 New high score!

But this throws an exception since it is invalid formatting. So my question is: Is it possible to compare two input parameters to each other when using ChoiceFormat?

  • \$\begingroup\$ Can you pass a boolean? You could try a workaround by passing a boolean parameter of value param1 < param2 \$\endgroup\$ Jun 14 '17 at 20:36
  • \$\begingroup\$ @CedricMartens Technically yes but it gets coerced to a String sadly. It actually seems like all inputs gets coerced to Strings since if you pass an (say new Object()) it appears in the same way as if you would do System.out.println(new Object()). \$\endgroup\$
    – Charanor
    Jun 14 '17 at 20:52
  • \$\begingroup\$ and if from there you try doing {0} == "true"? Since if it coerces it to a String it should be "true" \$\endgroup\$ Jun 14 '17 at 20:55
  • \$\begingroup\$ @CedricMartens Unfortunately that's invalid syntax. From this link it also seems like the only valid inputs are number, date, time, choice (and String if you don't supply a type), no boolean or other types allowed! \$\endgroup\$
    – Charanor
    Jun 14 '17 at 21:09

You can use this workaround of using a number (0 for false and 1 for true) instead of a boolean, since a boolean is not permitted.

condition=Score: {0,choice,0#Cool Score, but {1, number} is less than {2, number}!|1#New Highscore of {1, number}!}

This code :

int highScore = 15;
int newScore = 20;

boolean isHighScore = newScore > highScore;

//I use a ternary if in order to translate the boolean into a number since booleans
//are not accepted by ChoiceFormat
String res = i18NBundle.format("condition", isHighScore ? 1 : 0, newScore, highScore);

//I test again with a new score that is not the highscore
    highScore = newScore;

newScore = 13;
isHighScore = newScore > highScore;
String res2 =i18NBundle.format("condition", isHighScore ? 1 : 0, newScore, highScore);

Outputs :

Score: New Highscore of 20!
Score: Cool Score, but 13 is less than 20!
  • 1
    \$\begingroup\$ I'm perfectly aware of this workaround (it's how I do it atm) I just wanted to know if there's a way to do it just using the format itself. +1 anyways though. \$\endgroup\$
    – Charanor
    Jun 14 '17 at 22:32

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .