View Javadoc
1   /*
2    * Licensed to the Apache Software Foundation (ASF) under one
3    * or more contributor license agreements.  See the NOTICE file
4    * distributed with this work for additional information
5    * regarding copyright ownership.  The ASF licenses this file
6    * to you under the Apache License, Version 2.0 (the
7    * "License"); you may not use this file except in compliance
8    * with the License.  You may obtain a copy of the License at
9    *
10   *   http://www.apache.org/licenses/LICENSE-2.0
11   *
12   * Unless required by applicable law or agreed to in writing,
13   * software distributed under the License is distributed on an
14   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15   * KIND, either express or implied.  See the License for the
16   * specific language governing permissions and limitations
17   * under the License.
18   */
19  
20  package org.apache.myfaces.tobago.util;
21  
22  import org.apache.myfaces.tobago.internal.util.StringUtils;
23  
24  import java.util.ArrayList;
25  import java.util.Arrays;
26  import java.util.List;
27  import java.util.Locale;
28  
29  public final class LocaleUtils {
30  
31    private LocaleUtils() {
32    }
33  
34    public static Locale createLocale(final String value) {
35      Locale locale = null;
36      final String[] strings = StringUtils.split(value, "_");
37      switch (strings.length) {
38        case 1:
39          locale = new Locale(strings[0]);
40          break;
41        case 2:
42          locale = new Locale(strings[0], strings[1]);
43          break;
44        case 3:
45          locale = new Locale(strings[0], strings[1], strings[2]);
46          break;
47        default:
48          // TODO
49      }
50      return locale;
51    }
52  
53    /**
54     * @deprecated since 1.5
55     */
56    @Deprecated
57    public static List<Locale> getLocaleList(final Locale locale) {
58  
59      String string = locale.toString();
60      final List<Locale> locales = new ArrayList<>(3);
61      locales.add(locale);
62      int underscore;
63      while ((underscore = string.lastIndexOf('_')) > 0) {
64        string = string.substring(0, underscore);
65        locales.add(createLocale(string));
66      }
67  
68      return locales;
69    }
70  
71    /**
72     * Returns a list of strings, which are used as suffix for resources from resource manager.
73     * <p>
74     * Sample: "de_DE" -&gt; "_de_DE", "_de", ""
75     */
76    public static List<String> getLocaleSuffixList(final Locale locale) {
77  
78      if (locale == null) {
79        return Arrays.asList("");
80      }
81  
82      String string = locale.toString();
83      final String prefix = "_";
84      final List<String> locales = new ArrayList<>(4);
85      locales.add(prefix + string);
86      int underscore;
87      while ((underscore = string.lastIndexOf('_')) > 0) {
88        string = string.substring(0, underscore);
89        locales.add(prefix + string);
90      }
91  
92      locales.add(""); // default suffix is nothing
93  
94      return locales;
95    }
96  
97  }