java examples
Home Java Examples Resources Java Interview Questions
Brainpower SEO

How to create a Locale object using a variant?

Font differences may force you to use different characters on different platforms. You could then define the Locale objects with the variant codes to identify those differences. The variant codes conform to no standard. They are arbitrary and specific to your application. If you create Locale objects with variant codes only your application will know how to deal with them.

In this example instead of demonstrating to use a different font for different platform we simplify it to just print a different message. We create three different resource bundles for each platform, the MessagesBundle_fr_FR_UNIX.properties, MessagesBundle_fr_FR_MAC.properties and MessagesBundle_fr_FR_WIN.properties. These files will contains different message for each platform.

package com.javacoderanch.example.util;

import java.util.Locale;
import java.util.ResourceBundle;

public class LocalePlatform {
    public static void main(String[] args) {
        ResourceBundle res;
        String language = "fr";
        String country = "FR";

        //
        // Construct a locale from language, country, variant.
        // Where the variant can be a variant vendor and browser 
        // specific code.
        //
        Locale unix = new Locale(language, country, "UNIX");
        res = ResourceBundle.getBundle("MessagesBundle", unix);
        System.out.println("UNIX: " + res.getString("birthday"));

        Locale mac = new Locale(language, country, "MAC");
        res = ResourceBundle.getBundle("MessagesBundle", mac);
        System.out.println("MAC : " + res.getString("birthday"));

        Locale windows = new Locale(language, country, "WIN");
        res = ResourceBundle.getBundle("MessagesBundle", windows);
        System.out.println("WIN : " + res.getString("birthday"));
    }
}

Here are the contents of the resource bundle files:

MessagesBundle_fr_FR_UNIX.properties
birthday=Unix, Joyeux anniversaire à toi!
MessagesBundle_fr_FR_MAC.properties
birthday=Mac, Joyeux anniversaire à toi!
MessagesBundle_fr_FR_WIN.properties
birthday=Windows, Joyeux anniversaire à toi!