Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

It it possible to have multiple Locales in one same MessageFormat?

I have the following JSON example below:

{
    "value": 946.2,
    "description": "O valor é R$ 946,20."
}

I need to use MessageFormat to JUnit test this JSON example, but I get an invalid value because my Locale is not in english. If I change my Locale to english instead of brazilian portuguese I get an invalid description because the currency value is displayed in English.

Here's my code:

import java.math.BigDecimal;
import java.text.MessageFormat;
import java.util.Locale;

Locale.setDefault(Locale.ENGLISH);

System.out.println(MessageFormat.format("""
'{'
    "value": {0},
    "description": "O valor é {0,number,currency}."
'}'
""", new BigDecimal(946.2)));

How can I format the value or the description in order to get the JSON as displayed above?

like image 381
Philippe Gioseffi Avatar asked Oct 15 '25 04:10

Philippe Gioseffi


1 Answers

Sure, no problem! But:

  • Locale.setDefault is harsh! (for test it can be tolerable.... but i don't want to look up that bug..)
  • (I "don't like" MessageFormat... it uses StringBuffer internally(Mr. performance killer & memory dumper).)

As long as you can handle the "complexity", it can be done like:

import java.math.BigDecimal;
import java.text.MessageFormat;
import java.text.NumberFormat;
import java.util.Locale;

public class MainSimple {

  // Brazilian Locale:
  private static final Locale LOCALE_PT_BR = new Locale.Builder().
      setLanguage("pt").
      setRegion("BR").
      build();
  // US number format:
  private static final NumberFormat DEFAULT_NUMBER_FMT = NumberFormat.getNumberInstance(Locale.US);

  public static void main(String[] args) {
    // MessageFormat instance, with format (with 2! parameters) and Locale:
    MessageFormat mainFmt = new MessageFormat("""
                      '{'
                          "value": {0},
                          "description": "O valor é {1,number,currency}. <-- trick 1 "
                      '}'
                      """, LOCALE_PT_BR);
    
    final BigDecimal num = new BigDecimal(946.2);
    // trick 2:
    Object[] params = {DEFAULT_NUMBER_FMT.format(num), num};
    // do it:
    System.out.println(mainFmt.format(params));
  }
}

Outline:

  • Constants for:
    • Brazilian (outer format) locale.
    • US/Standard (inner) number format for custom (non brazilian) formatting/this particular number.
  • MessageFormat has a constructor with locale parameter! (we use that, and call it outer format/locale(mainFmt))
  • Int the format we "trick a little" and use 2 parameters: {0} and {1,number,currency}
  • We trick on: Pass the first parameter as a (US-formatted number) String, the second as had. (Object[] params = {DEFAULT_NUMBER_FMT.format(num), num})
  • finally format (& print to out).

Prints:

{
    "value": 946.2,
    "description": "O valor é R$ 946,20."
}
like image 141
xerx593 Avatar answered Oct 17 '25 18:10

xerx593



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!