]>
git.argeo.org Git - lgpl/argeo-commons.git/blob - org/argeo/util/LocaleChoice.java
2 * Copyright (C) 2007-2012 Argeo GmbH
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
8 * http://www.apache.org/licenses/LICENSE-2.0
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
16 package org
.argeo
.util
;
18 import java
.util
.ArrayList
;
19 import java
.util
.Collections
;
20 import java
.util
.List
;
21 import java
.util
.Locale
;
23 import javax
.security
.auth
.callback
.LanguageCallback
;
25 import org
.argeo
.ArgeoException
;
27 /** Choose in a list of locales. TODO: replace with {@link LanguageCallback} */
28 public class LocaleChoice
{
29 private final List
<Locale
> locales
;
31 private Integer selectedIndex
= null;
32 private final Integer defaultIndex
;
34 public LocaleChoice(List
<Locale
> locales
, Locale defaultLocale
) {
35 Integer defaultIndex
= null;
36 this.locales
= Collections
.unmodifiableList(locales
);
37 for (int i
= 0; i
< locales
.size(); i
++)
38 if (locales
.get(i
).equals(defaultLocale
))
41 // based on language only
42 if (defaultIndex
== null)
43 for (int i
= 0; i
< locales
.size(); i
++)
44 if (locales
.get(i
).getLanguage()
45 .equals(defaultLocale
.getLanguage()))
48 if (defaultIndex
== null)
49 throw new ArgeoException("Default locale " + defaultLocale
50 + " is not in available locales " + locales
);
51 this.defaultIndex
= defaultIndex
;
53 this.selectedIndex
= defaultIndex
;
57 * Convenience constructor based on a comma separated list of iso codes (en,
58 * en_US, fr_CA, etc.). Default selection is default locale.
60 public LocaleChoice(String locales
, Locale defaultLocale
) {
61 this(asLocaleList(locales
), defaultLocale
);
64 public String
[] getSupportedLocalesLabels() {
65 String
[] labels
= new String
[locales
.size()];
66 for (int i
= 0; i
< locales
.size(); i
++) {
67 Locale locale
= locales
.get(i
);
68 if (locale
.getCountry().equals(""))
69 labels
[i
] = locale
.getDisplayLanguage(locale
) + " ["
70 + locale
.getLanguage() + "]";
72 labels
[i
] = locale
.getDisplayLanguage(locale
) + " ("
73 + locale
.getDisplayCountry(locale
) + ") ["
74 + locale
.getLanguage() + "_" + locale
.getCountry()
81 public Locale
getSelectedLocale() {
82 if (selectedIndex
== null)
84 return locales
.get(selectedIndex
);
87 public void setSelectedIndex(Integer selectedIndex
) {
88 this.selectedIndex
= selectedIndex
;
91 public Integer
getSelectedIndex() {
95 public Integer
getDefaultIndex() {
99 public List
<Locale
> getLocales() {
103 public Locale
getDefaultLocale() {
104 return locales
.get(getDefaultIndex());
107 /** Returns null if argument is null. */
108 public static List
<Locale
> asLocaleList(String locales
) {
111 ArrayList
<Locale
> availableLocales
= new ArrayList
<Locale
>();
112 String
[] codes
= locales
.split(",");
113 for (int i
= 0; i
< codes
.length
; i
++) {
114 String code
= codes
[i
];
115 // variant not supported
116 int indexUnd
= code
.indexOf("_");
119 String language
= code
.substring(0, indexUnd
);
120 String country
= code
.substring(indexUnd
+ 1);
121 locale
= new Locale(language
, country
);
123 locale
= new Locale(code
);
125 availableLocales
.add(locale
);
127 return availableLocales
;
130 public static void main(String
[] args
) {
131 for (String isoL
: Locale
.getISOLanguages()) {
132 Locale locale
= new Locale(isoL
);
133 System
.out
.println(isoL
+ "\t" + locale
.getDisplayLanguage() + "\t"
134 + locale
.getDisplayLanguage(locale
));