]>
git.argeo.org Git - lgpl/argeo-commons.git/blob - org.argeo.cms/src/org/argeo/cms/internal/auth/LocaleChoice.java
9ec9f7a0b44324f6fa4931c5c35f1e78343077ba
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
.cms
.internal
.auth
;
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().equals(defaultLocale
.getLanguage()))
47 if (defaultIndex
== null)
48 throw new ArgeoException("Default locale " + defaultLocale
+ " is not in available locales " + locales
);
49 this.defaultIndex
= defaultIndex
;
51 this.selectedIndex
= defaultIndex
;
55 * Convenience constructor based on a comma separated list of iso codes (en,
56 * en_US, fr_CA, etc.). Default selection is default locale.
58 public LocaleChoice(String locales
, Locale defaultLocale
) {
59 this(asLocaleList(locales
), defaultLocale
);
62 public String
[] getSupportedLocalesLabels() {
63 String
[] labels
= new String
[locales
.size()];
64 for (int i
= 0; i
< locales
.size(); i
++) {
65 Locale locale
= locales
.get(i
);
66 if (locale
.getCountry().equals(""))
67 labels
[i
] = locale
.getDisplayLanguage(locale
) + " [" + locale
.getLanguage() + "]";
69 labels
[i
] = locale
.getDisplayLanguage(locale
) + " (" + locale
.getDisplayCountry(locale
) + ") ["
70 + locale
.getLanguage() + "_" + locale
.getCountry() + "]";
76 public Locale
getSelectedLocale() {
77 if (selectedIndex
== null)
79 return locales
.get(selectedIndex
);
82 public void setSelectedIndex(Integer selectedIndex
) {
83 this.selectedIndex
= selectedIndex
;
86 public Integer
getSelectedIndex() {
90 public Integer
getDefaultIndex() {
94 public List
<Locale
> getLocales() {
98 public Locale
getDefaultLocale() {
99 return locales
.get(getDefaultIndex());
102 /** Returns null if argument is null. */
103 public static List
<Locale
> asLocaleList(Object locales
) {
106 ArrayList
<Locale
> availableLocales
= new ArrayList
<Locale
>();
107 String
[] codes
= locales
.toString().split(",");
108 for (int i
= 0; i
< codes
.length
; i
++) {
109 String code
= codes
[i
];
110 // variant not supported
111 int indexUnd
= code
.indexOf("_");
114 String language
= code
.substring(0, indexUnd
);
115 String country
= code
.substring(indexUnd
+ 1);
116 locale
= new Locale(language
, country
);
118 locale
= new Locale(code
);
120 availableLocales
.add(locale
);
122 return availableLocales
;
125 public static void main(String
[] args
) {
126 for (String isoL
: Locale
.getISOLanguages()) {
127 Locale locale
= new Locale(isoL
);
128 System
.out
.println(isoL
+ "\t" + locale
.getDisplayLanguage() + "\t" + locale
.getDisplayLanguage(locale
));