venice-main-classic/src/com/silverwrist/util/LocaleFactory.java

79 lines
2.8 KiB
Java

/*
* The contents of this file are subject to the Mozilla Public License Version 1.1
* (the "License"); you may not use this file except in compliance with the License.
* You may obtain a copy of the License at <http://www.mozilla.org/MPL/>.
*
* Software distributed under the License is distributed on an "AS IS" basis, WITHOUT
* WARRANTY OF ANY KIND, either express or implied. See the License for the specific
* language governing rights and limitations under the License.
*
* The Original Code is the Venice Web Communities System.
*
* The Initial Developer of the Original Code is Eric J. Bowersox <erbo@silcom.com>,
* for Silverwrist Design Studios. Portions created by Eric J. Bowersox are
* Copyright (C) 2001 Eric J. Bowersox/Silverwrist Design Studios. All Rights Reserved.
*
* Contributor(s):
*/
package com.silverwrist.util;
import java.util.*;
/**
* A utility class which creates <CODE>Locale</CODE> objects from standard descriptor strings.
* The descriptor strings are of the form returned by <CODE>Locale.toString()</CODE>.
*
* @author Eric J. Bowersox &lt;erbo@silcom.com&gt;
* @version X
* @see java.util.Locale
* @see java.util.Locale#toString()
*/
public class LocaleFactory
{
/*--------------------------------------------------------------------------------
* Constructor
*--------------------------------------------------------------------------------
*/
private LocaleFactory()
{ // this object cannot be instantiated
} // end constructor
/*--------------------------------------------------------------------------------
* External static operations
*--------------------------------------------------------------------------------
*/
/**
* Creates a <CODE>Locale</CODE> from a standard descriptor string.
*
* @param streq The string equivalent of the Locale to be created.
* @return The corresponding <CODE>Locale</CODE>, or the default <CODE>Locale</CODE> if the parameter is
* <CODE>null</CODE> or the empty string.
*/
public static Locale createLocale(String streq)
{
if ((streq==null) || (streq.length()==0))
return Locale.getDefault(); // no locale
int p1 = streq.indexOf('_');
if (p1<0)
return new Locale(streq,""); // language but no country specified
String x_lang = streq.substring(0,p1);
int p2 = streq.indexOf('_',p1+1);
if (p2<0)
{ // there's only one underscore - figure out what part the last part is
String lastpart = streq.substring(p1+1);
if (lastpart.length()==2)
return new Locale(x_lang,lastpart); // language + country
else
return new Locale(x_lang,"",lastpart); // language + country(null) + variant
} // end if
// do all three variants
return new Locale(x_lang,streq.substring(p1+1,p2),streq.substring(p2+1));
} // end createLocale
} // end class LocaleFactory