A helper function to convert a string of the form a_B_C to a locale object. - Android Internationalization

Android examples for Internationalization:Locale

Description

A helper function to convert a string of the form a_B_C to a locale object.

Demo Code

/*//from  w  w  w  .  j  ava2 s  .  c o  m
 ******************************************************************************
 * Copyright (C) 1996-2007, International Business Machines Corporation and   *
 * others. All Rights Reserved.                                               *
 ******************************************************************************
 *
 ******************************************************************************
 */
//package com.java2s;
import java.util.Locale;

public class Main {
    /**
     * A helper function to convert a string of the form
     * aa_BB_CC to a locale object.  Why isn't this in Locale?
     */
    public static Locale getLocaleFromName(String name) {
        String language = "";
        String country = "";
        String variant = "";

        int i1 = name.indexOf('_');
        if (i1 < 0) {
            language = name;
        } else {
            language = name.substring(0, i1);
            ++i1;
            int i2 = name.indexOf('_', i1);
            if (i2 < 0) {
                country = name.substring(i1);
            } else {
                country = name.substring(i1, i2);
                variant = name.substring(i2 + 1);
            }
        }

        return new Locale(language, country, variant);
    }
}

Related Tutorials