]>
git.argeo.org Git - lgpl/argeo-commons.git/blob - org.argeo.util/src/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
.List
;
20 import java
.util
.Locale
;
22 import javax
.security
.auth
.callback
.LanguageCallback
;
24 /** Choose in a list of locales. TODO: replace with {@link LanguageCallback} */
25 public class LocaleChoice
{
26 private List
<Locale
> availableLocales
= new ArrayList
<Locale
>();
28 private Integer selectedIndex
= null;
29 private Integer defaultIndex
= null;
31 // public LocaleCallback(Integer defaultIndex, List<Locale>
32 // availableLocales) {
33 // this.availableLocales = Collections
34 // .unmodifiableList(new ArrayList<Locale>(availableLocales));
35 // this.defaultIndex = defaultIndex;
36 // this.selectedIndex = defaultIndex;
40 * Convenience constructor based on a comma separated list of iso codes (en,
41 * en_US, fr_CA, etc.). Default selection is default locale.
43 public LocaleChoice(String locales
, Locale defaultLocale
) {
44 if (locales
== null || locales
.trim().equals(""))
46 String
[] codes
= locales
.split(",");
47 for (int i
= 0; i
< codes
.length
; i
++) {
48 String code
= codes
[i
];
49 // variant not supported
50 int indexUnd
= code
.indexOf("_");
53 String language
= code
.substring(0, indexUnd
);
54 String country
= code
.substring(indexUnd
+ 1);
55 locale
= new Locale(language
, country
);
57 locale
= new Locale(code
);
59 availableLocales
.add(locale
);
60 if (locale
.equals(defaultLocale
))
64 if (defaultIndex
== null)
67 this.selectedIndex
= defaultIndex
;
70 public String
[] getSupportedLocalesLabels() {
71 String
[] labels
= new String
[availableLocales
.size()];
72 for (int i
= 0; i
< availableLocales
.size(); i
++) {
73 Locale locale
= availableLocales
.get(i
);
74 if (locale
.getCountry().equals(""))
75 labels
[i
] = locale
.getDisplayLanguage(locale
) + " ["
76 + locale
.getLanguage() + "]";
78 labels
[i
] = locale
.getDisplayLanguage(locale
) + " ("
79 + locale
.getDisplayCountry(locale
) + ") ["
80 + locale
.getLanguage() + "_" + locale
.getCountry()
87 public Locale
getSelectedLocale() {
88 if (selectedIndex
== null)
90 return availableLocales
.get(selectedIndex
);
93 public void setSelectedIndex(Integer selectedIndex
) {
94 this.selectedIndex
= selectedIndex
;
97 public Integer
getDefaultIndex() {
101 public List
<Locale
> getAvailableLocales() {
102 return availableLocales
;
105 public Locale
getDefaultLocale() {
106 return availableLocales
.get(getDefaultIndex());
109 public static void main(String
[] args
) {
110 for (String isoL
: Locale
.getISOLanguages()) {
111 Locale locale
= new Locale(isoL
);
112 System
.out
.println(isoL
+ "\t" + locale
.getDisplayLanguage() + "\t"
113 + locale
.getDisplayLanguage(locale
));