1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19 package org.apache.myfaces.shared_orchestra.util;
20
21 import java.util.Locale;
22 import java.util.StringTokenizer;
23
24 import org.apache.commons.logging.Log;
25 import org.apache.commons.logging.LogFactory;
26
27
28
29
30
31
32 public final class LocaleUtils
33 {
34 private static final Log log = LogFactory.getLog(LocaleUtils.class);
35
36
37 private LocaleUtils()
38 {
39
40 }
41
42
43
44
45
46
47
48
49 public static Locale toLocale(String localeString)
50 {
51 if ((localeString == null) || (localeString.length() == 0))
52 {
53 Locale locale = Locale.getDefault();
54 if(log.isWarnEnabled())
55 log.warn("Locale name in faces-config.xml null or empty, setting locale to default locale : "+locale.toString());
56 return locale;
57 }
58
59 int separatorCountry = localeString.indexOf('_');
60 char separator;
61 if (separatorCountry >= 0) {
62 separator = '_';
63 }
64 else
65 {
66 separatorCountry = localeString.indexOf('-');
67 separator = '-';
68 }
69
70 String language, country, variant;
71 if (separatorCountry < 0)
72 {
73 language = localeString;
74 country = variant = "";
75 }
76 else
77 {
78 language = localeString.substring(0, separatorCountry);
79
80 int separatorVariant = localeString.indexOf(separator, separatorCountry + 1);
81 if (separatorVariant < 0)
82 {
83 country = localeString.substring(separatorCountry + 1);
84 variant = "";
85 }
86 else
87 {
88 country = localeString.substring(separatorCountry + 1, separatorVariant);
89 variant = localeString.substring(separatorVariant + 1);
90 }
91 }
92
93 return new Locale(language, country, variant);
94 }
95
96
97
98
99
100
101
102
103
104
105
106 public static Locale converterTagLocaleFromString(String name)
107 {
108 try
109 {
110 Locale locale;
111 StringTokenizer st = new StringTokenizer(name, "_");
112 String language = st.nextToken();
113
114 if(st.hasMoreTokens())
115 {
116 String country = st.nextToken();
117
118 if(st.hasMoreTokens())
119 {
120 String variant = st.nextToken();
121 locale = new Locale(language, country, variant);
122 }
123 else
124 {
125 locale = new Locale(language, country);
126 }
127 }
128 else
129 {
130 locale = new Locale(language);
131 }
132
133
134 return locale;
135 }
136 catch(Exception e)
137 {
138 throw new IllegalArgumentException("Locale parsing exception - " +
139 "invalid string representation '" + name + "'");
140 }
141 }
142 }