Localização (múltiplos idiomas)

Aula
Estado
  • São arquivos com a extensão .properties que ficam no package resources
  • O bundle padrão pode ser em qualquer idioma e deve ter o nome messages.properties; os bundles com outra localização devem ser como messages_en_GB.properties, messages_en_US.properties, messages_ru.properties
  • Podem ser utilizados com a classe java.util.ResourceBundle
  • É possível criar padrões dinâmicos de mensagem product={0}, price {1}, quantity {2}, os campos são preenchidos com a classe MessageFormat
String name = "Cookie"; BigDecimal = price = BigDecimal.valueOf(2.99); LocalDate bestBefore = LocalDate.of(2021, Month.APRIL, 2); int quantity = 4; Locale locale = new Locale("en", "GB"); ResourceBundle bundle = ResourceBundle.getBundle("resource.messages", locale); NumberFromat currencyFromat = NumberFormat.getCurrencyInstance(locale); NumberFormat numberFormat = NumberFormat.getNumberInstance(locale); DateTimeFormatter dateFormat = DateTimeFormatter.ofPattern("dd MMM yyyy", locale); String fPrice = currencyFormat.format(price); String fQuantity = numberFormat.format(quantity); String fBestBefore = dateFormat.fromat(bestBefore); // or bestBefore.format(dateFormat); String pattern = bundle.getString("product"); String message = MessageFormat.format(pattern, name, fPrice, fQuantity, fBestBefore); // message == "Cookie, price £2.99, quantity 4, best before 2 Apr 2021"