]>
git.argeo.org Git - lgpl/argeo-commons.git/blob - org.argeo.util/src/main/java/org/argeo/util/LocaleCallback.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
.Callback
;
25 /** Choose in a list of locales */
26 public class LocaleCallback
implements Callback
{
27 private List
<Locale
> availableLocales
= new ArrayList
<Locale
>();
29 private Integer selectedIndex
= null;
30 private Integer defaultIndex
= null;
31 private String prompt
= "Language";
33 public LocaleCallback(Integer defaultIndex
, List
<Locale
> availableLocales
) {
34 this.availableLocales
= Collections
35 .unmodifiableList(new ArrayList
<Locale
>(availableLocales
));
36 this.defaultIndex
= defaultIndex
;
37 this.selectedIndex
= defaultIndex
;
41 * Convenience constructor based on a comma separated list of iso codes (en,
42 * en_US, fr_CA, etc.). Default selection is default locale.
44 public LocaleCallback(String locales
) {
45 if (locales
== null || locales
.trim().equals(""))
47 String
[] codes
= locales
.split(",");
48 for (int i
= 0; i
< codes
.length
; i
++) {
49 String code
= codes
[i
];
50 // variant not supported
51 int indexUnd
= code
.indexOf("_");
54 String language
= code
.substring(0, indexUnd
);
55 String country
= code
.substring(indexUnd
+ 1);
56 locale
= new Locale(language
, country
);
58 locale
= new Locale(code
);
60 availableLocales
.add(locale
);
61 if (locale
.equals(Locale
.getDefault()))
65 if (defaultIndex
== null)
68 this.selectedIndex
= defaultIndex
;
71 public String
[] getSupportedLocalesLabels() {
72 String
[] labels
= new String
[availableLocales
.size()];
73 for (int i
= 0; i
< availableLocales
.size(); i
++) {
74 Locale locale
= availableLocales
.get(i
);
75 if (locale
.getCountry().equals(""))
76 labels
[i
] = locale
.getDisplayLanguage(locale
) + " ["
77 + locale
.getLanguage() + "]";
79 labels
[i
] = locale
.getDisplayLanguage(locale
) + " ("
80 + locale
.getDisplayCountry(locale
) + ") ["
81 + locale
.getLanguage() + "_" + locale
.getCountry()
88 public Locale
getSelectedLocale() {
89 if (selectedIndex
== null)
91 return availableLocales
.get(selectedIndex
);
94 public void setSelectedIndex(Integer selectedIndex
) {
95 this.selectedIndex
= selectedIndex
;
98 public Integer
getDefaultIndex() {
102 public String
getPrompt() {
107 public void setPrompt(String prompt
) {
108 this.prompt
= prompt
;
111 public List
<Locale
> getAvailableLocales() {
112 return availableLocales
;
115 public static void main(String
[] args
) {
116 for (String isoL
: Locale
.getISOLanguages()) {
117 Locale locale
= new Locale(isoL
);
118 System
.out
.println(isoL
+ "\t" + locale
.getDisplayLanguage() + "\t"
119 + locale
.getDisplayLanguage(locale
));