5. Java 7: Localization Flashcards
A Java Locale String exists of three different parts, name these parts.
- The language (mandatory, only lower case letters)
- The country (optional, only capital letters)
- The variant (often used for dialect, optional, only capital letters)
What are the four ways to create a Locale object in Java?
java.util.Locale.Builder
new Locale.Builder().setLanguage(“nl”).setRegion(“NL”).build();
java.util.Locale new Locale(String language); new Locale(String language, String country); new Locale(String language, String country, String variant)
forLanguageTag factory method
Locale.forLanguageTag(“nl-NL”);
Locale Constants
Locale.CANADA_FRENCH;
How to get the default Locale of the JVM?
Locale.getDefault();
How to set the default Locale of the JVM?
Locale.setDefault(Locale locale);
How to retrieve a Resource bundle for the nl-NL locale for the bundle named MyApp?
Locale locale = new Locale("nl", "NL"); ResourceBundle.getBundle("MyApp", locale);
Given the following classes what is the result:
Locale.setDefault(Locale.forLanguageTag(“nl”);
public void MyResources extends ResourceBundle {}
public void MyResources_nl extends MyResources {}
ResourceBundle.getBundle(“MyResources”, “de”);
getBundle() will return MyResources_nl
It will first try to find a MyResources class for the de language. It can’t find one so it tries the default “nl”. It can find a class. If MyResources_nl did not exist it would have returned the MyResources class. If the MyResources class did not exist it would throw a MissingResourceException.
ResourceBundle has two subclasses name these two and their uses.
- PropertyResourceBundle
Is backed by a properties file. The propertie file consists of key-value pairs of the String type. The naming of these files would be.
baseName.properties
baseName_nl.properties
baseName_en_GB.properties - ListResourceBundle
Handles the properties in a easy to use List. Can also store other non-String objects.
Given the following classes are in package foo.bar and the following code. What is the result?
public void MyResources extends ResourceBundle {}
public void MyResources_nl extends MyResources {}
ResourceBundle.getBundle(“MyResources”, “de”);
The ResourceBundle can’t be found. You should use the fullly qualified name for the bundle: foo.bar.MyResources
What is the order ResourceBundle.getBundle uses to search for the requested Bundle?
- baseName_language_country_variant
- baseName_language_country
- baseName_language
- baseName_defaultlanguage_defaultcountry_defaultvariant
- baseName_defaultlanguage_defaultcountry
- baseName_defaultlanguage
- baseName
Given the following classes and code what is the Result?
public void MyResources extends ResourceBundle {}
ResourceBundle.getBundle(“MyResource”, new Locale(“nl”));
Throws a MissingResourceException MyResource is not a known resource.
Given the following classes and code what is the Result?
public void MyResources extends ResourceBundle {}
And a properties file: MyResource.properties
ResourceBundle.getBundle(“MyResources”, new Locale(“nl”));
What is the type of the ResourceBundle?
MyResources.class
getBundle will always first look for an Class before looking for an properties file. If only a properties file is found the returned type would have been: PropertyResourceBundle
Given the following integer how can you format it using the “us” locale?
int i = 9_000_000;
Locale l = Locale.US;
NumberFormat formatter = NumberFormat.getNumberInstance(l);
formatter.format(i);
Given the following integer how can you format it as money for the “us” locale?
int i = 9_000_000;
Locale l = Locale.US;
NumberFormat formatter = NumberFormat.geCurrencyInstance(l);
formatter.format(i);
Given a date object how can you print it using the “us” locale?
Date d = new Date(); DateFormat formatter = DateFormat.getDateInstance(Locale.US); formatter.format(d);
Given a date object how can you print it in the format yyyy MMMM dd for the “us” locale?
Date d = new Date(); SimpleDateFormat sdf = new SimpleDateFormat("yyyy MMMM dd", Locale.US); sdf.format(d);