001 /*
002 * Licensed to the Apache Software Foundation (ASF) under one
003 * or more contributor license agreements. See the NOTICE file
004 * distributed with this work for additional information
005 * regarding copyright ownership. The ASF licenses this file
006 * to you under the Apache License, Version 2.0 (the
007 * "License"); you may not use this file except in compliance
008 * with the License. You may obtain a copy of the License at
009 *
010 * http://www.apache.org/licenses/LICENSE-2.0
011 *
012 * Unless required by applicable law or agreed to in writing,
013 * software distributed under the License is distributed on an
014 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
015 * KIND, either express or implied. See the License for the
016 * specific language governing permissions and limitations
017 * under the License.
018 */
019
020 package org.apache.myfaces.tobago.util;
021
022 import org.apache.commons.lang.StringUtils;
023
024 import java.util.ArrayList;
025 import java.util.List;
026 import java.util.Locale;
027
028 public class LocaleUtil {
029
030 private LocaleUtil() {
031 }
032
033 public static Locale createLocale(String value) {
034 Locale locale = null;
035 String[] strings = StringUtils.split(value, "_");
036 switch (strings.length) {
037 case 1:
038 locale = new Locale(strings[0]);
039 break;
040 case 2:
041 locale = new Locale(strings[0], strings[1]);
042 break;
043 case 3:
044 locale = new Locale(strings[0], strings[1], strings[2]);
045 break;
046 default:
047 // TODO
048 }
049 return locale;
050 }
051
052 public static List<Locale> getLocaleList(Locale locale) {
053
054 String string = locale.toString();
055 List<Locale> locales = new ArrayList<Locale>(3);
056 locales.add(locale);
057 int underscore;
058 while ((underscore = string.lastIndexOf('_')) > 0) {
059 string = string.substring(0, underscore);
060 locales.add(createLocale(string));
061 }
062
063 return locales;
064 }
065 }