|
| 1 | +package com.baeldung.localization; |
| 2 | + |
| 3 | +import java.util.List; |
| 4 | +import java.util.Locale; |
| 5 | +import java.util.ResourceBundle; |
| 6 | + |
| 7 | +import com.ibm.icu.text.MessageFormat; |
| 8 | + |
| 9 | +public class ICUFormat { |
| 10 | + |
| 11 | + public static String getLabel(Locale locale, Object[] data) { |
| 12 | + ResourceBundle bundle = ResourceBundle.getBundle("formats", locale); |
| 13 | + String format = bundle.getString("label-icu"); |
| 14 | + MessageFormat formatter = new MessageFormat(format, locale); |
| 15 | + return formatter.format(data); |
| 16 | + } |
| 17 | + |
| 18 | + public static void run(List<Locale> locales) { |
| 19 | + System.out.println("ICU formatter"); |
| 20 | + locales.forEach(locale -> System.out.println(getLabel(locale, new Object[] { "Alice", "female", 0 }))); |
| 21 | + locales.forEach(locale -> System.out.println(getLabel(locale, new Object[] { "Alice", "female", 1 }))); |
| 22 | + locales.forEach(locale -> System.out.println(getLabel(locale, new Object[] { "Alice", "female", 2 }))); |
| 23 | + locales.forEach(locale -> System.out.println(getLabel(locale, new Object[] { "Alice", "female", 3 }))); |
| 24 | + locales.forEach(locale -> System.out.println(getLabel(locale, new Object[] { "Bob", "male", 0 }))); |
| 25 | + locales.forEach(locale -> System.out.println(getLabel(locale, new Object[] { "Bob", "male", 1 }))); |
| 26 | + locales.forEach(locale -> System.out.println(getLabel(locale, new Object[] { "Bob", "male", 2 }))); |
| 27 | + locales.forEach(locale -> System.out.println(getLabel(locale, new Object[] { "Bob", "male", 3 }))); |
| 28 | + } |
| 29 | +} |
0 commit comments