How do I 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 Birthday_fr_FR_UNIX.properties, Birthday_fr_FR_MAC.properties and Birthday_fr_FR_WIN.properties. These files will contains different message for each platform.

package org.kodejava.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("Birthday", unix);
        System.out.println("UNIX: " + res.getString("birthday"));

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

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

Here are the contents of the resource bundle files:

Birthday_fr_FR_UNIX.properties

birthday=Unix, Joyeux anniversaire à vous!

Birthday_fr_FR_MAC.properties

birthday=Mac, Joyeux anniversaire à vous!

Birthday_fr_FR_WIN.properties

birthday=Windows, Joyeux anniversaire à vous!
Wayan

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.