5. Java 7: Localization Flashcards

1
Q

A Java Locale String exists of three different parts, name these parts.

A
  1. The language (mandatory, only lower case letters)
  2. The country (optional, only capital letters)
  3. The variant (often used for dialect, optional, only capital letters)
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
2
Q

What are the four ways to create a Locale object in Java?

A

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 well did you know this?
1
Not at all
2
3
4
5
Perfectly
3
Q

How to get the default Locale of the JVM?

A

Locale.getDefault();

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
4
Q

How to set the default Locale of the JVM?

A

Locale.setDefault(Locale locale);

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
5
Q

How to retrieve a Resource bundle for the nl-NL locale for the bundle named MyApp?

A
Locale locale = new Locale("nl", "NL");
ResourceBundle.getBundle("MyApp", locale);
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
6
Q

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”);

A

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.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
7
Q

ResourceBundle has two subclasses name these two and their uses.

A
  1. 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
  2. ListResourceBundle
    Handles the properties in a easy to use List. Can also store other non-String objects.
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
8
Q

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”);

A

The ResourceBundle can’t be found. You should use the fullly qualified name for the bundle: foo.bar.MyResources

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
9
Q

What is the order ResourceBundle.getBundle uses to search for the requested Bundle?

A
  1. baseName_language_country_variant
  2. baseName_language_country
  3. baseName_language
  4. baseName_defaultlanguage_defaultcountry_defaultvariant
  5. baseName_defaultlanguage_defaultcountry
  6. baseName_defaultlanguage
  7. baseName
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
10
Q

Given the following classes and code what is the Result?
public void MyResources extends ResourceBundle {}

ResourceBundle.getBundle(“MyResource”, new Locale(“nl”));

A

Throws a MissingResourceException MyResource is not a known resource.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
11
Q

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?

A

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

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
12
Q

Given the following integer how can you format it using the “us” locale?

int i = 9_000_000;

A

Locale l = Locale.US;
NumberFormat formatter = NumberFormat.getNumberInstance(l);
formatter.format(i);

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
13
Q

Given the following integer how can you format it as money for the “us” locale?

int i = 9_000_000;

A

Locale l = Locale.US;
NumberFormat formatter = NumberFormat.geCurrencyInstance(l);
formatter.format(i);

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
14
Q

Given a date object how can you print it using the “us” locale?

A
Date d = new Date();
DateFormat formatter = DateFormat.getDateInstance(Locale.US);
formatter.format(d);
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
15
Q

Given a date object how can you print it in the format yyyy MMMM dd for the “us” locale?

A
Date d = new Date();
SimpleDateFormat sdf = new SimpleDateFormat("yyyy MMMM dd", Locale.US);
sdf.format(d);
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
16
Q

Given the following code, what is the result?
Locale l = Locale.getDefault(); DateFormat df = DateFormat.getDateInstance(l); System.out.println(l.getCountry()+” “+ df.format(dt));

A

Wont compile. DateFormat doest not have a method getDateInstance(Local locale);

Valid arguments for getDateInstance are:
getDateInstance();
getDateInstance(int style);
getDateInstance(int style, Local locale);

17
Q
Consider the following code.  
public class TestClass { 
   public static void main(String[] args) throws Exception{
      Date d = new Date(); 
      DateFormat //1 INSERT CODE HERE  
      String s = //2 INSERT CODE HERE  
      System.out.println(s); 
   } 
}  

What should be inserted at //1 and //2 above so that it will print the date in default date format for the UK Locale?

A

df = DateFormat.getDateInstance(DateFormat.DEFAULT, Locale.UK); and df.format(d);