Locale Flashcards
How do we create new locale?
Using Locale util.
Locale.getDefault()
Locale locale = new Locale(“hr”), new Locale(“hr”,”HR”)
Locale.GERMAN
Locale.GERMANY
Local.Builder()
How do we set default categories for localization?
Display -> Locale.setDefault(Category.DISPLAY, german)
Format -> Locale.setDefault(Category.FORMAT, german)
How do we format number using locale?
Using NumberFormat class.
NumberFormat.getIntegerInstance(Locale.US);
NumberFormat.getCurrencyInstance(Locale.US);
NumberFormat.getNumberInstance(Locale.US);
NumberFormat.getPercentInstance(Locale.US);
NumberFormat.getCompactNumberInstance(Locale.US, NumberFormat.Style.SHORT);
NumberFormat.getInstance();
Formatter is then used to .format() some number.
What does CompactNumberFormat do?
It formats number with locale in context in several different ways: 1,000,000.00 into 1 million.
Formats are defined by NumberFormat.Style, for eg. NumberFormat.Style.SHORT
How do we parse numbers in locale formatting?
Each NumberFormat instance has .parse() method.
How do we format dates in locale?
DateTimeFormatter.ofLocalizedDate(FormatStyle)
DateTimeFormatter.ofLocalizedTime()
DateTimeFormatter.ofLocalizedDateTime()
Formatter is then used to .format() some date.
Also, DateTimeFormatter.withLocale(locale).format(date) can be used.
ResourceBundle, how do we use it?
ResourceBundle.getBundle(“Name”, locale);
or
ResourceBundle.getBundle(“Name”);
Then we rs.getString(“key”)
How ResoruceBundle resolves properties files?
If locale is defined as “hr_HR”:
Wanted:
Name_hr_HR.properties
Name_hr.properties
Default:
Name_us_US.properties
Name_us.properties
Name.properties
MissingResourceException
If wanted locale is defined as “hr”:
Name_hr.properties
Name_us.properties
Name.properties
How do we format messages from string?
key.in.properties=Hello, {0} and {1}
MessageFormat.format(string, “Tammy”, “Henry”)
How do we set properties?
We instantiate new Properties();
pros.setProperty(“name”, “Peter”)
…