Convert a string based locale into a Locale Object
/* Copyright 2004 Sun Microsystems, Inc. All rights reserved. You may not modify, use, reproduce, or distribute this software except in compliance with the terms of the License at: http://adventurebuilder.dev.java.net/LICENSE.txt $Id: I18nUtil.java,v 1.2 2004/05/26 00:07:34 inder Exp $ */ import java.text.DecimalFormat; import java.text.NumberFormat; import java.util.Locale; /** * This utility class for internationalization. This class provides a central * location to do specialized formatting in both a default and a locale specfic * manner. */ public class Main { /** * Convert a string based locale into a Locale Object <br> * <br> * Strings are formatted: <br> * <br> * language_contry_variant * */ public static Locale getLocaleFromString(String localeString) { if (localeString == null) return null; if (localeString.toLowerCase().equals("default")) return Locale.getDefault(); int languageIndex = localeString.indexOf('_'); if (languageIndex == -1) return null; int countryIndex = localeString.indexOf('_', languageIndex + 1); String country = null; if (countryIndex == -1) { if (localeString.length() > languageIndex) { country = localeString.substring(languageIndex + 1, localeString.length()); } else { return null; } } int variantIndex = -1; if (countryIndex != -1) countryIndex = localeString.indexOf('_', countryIndex + 1); String language = localeString.substring(0, languageIndex); String variant = null; if (variantIndex != -1) { variant = localeString.substring(variantIndex + 1, localeString.length()); } if (variant != null) { return new Locale(language, country, variant); } else { return new Locale(language, country); } } }