]> git.argeo.org Git - lgpl/argeo-commons.git/blob - org.argeo.util/src/org/argeo/util/LocaleChoice.java
Clean up before implementing i18n support
[lgpl/argeo-commons.git] / org.argeo.util / src / org / argeo / util / LocaleChoice.java
1 /*
2 * Copyright (C) 2007-2012 Argeo GmbH
3 *
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
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
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.
15 */
16 package org.argeo.util;
17
18 import java.util.ArrayList;
19 import java.util.List;
20 import java.util.Locale;
21
22 import javax.security.auth.callback.LanguageCallback;
23
24 /** Choose in a list of locales. TODO: replace with {@link LanguageCallback} */
25 public class LocaleChoice {
26 private List<Locale> availableLocales = new ArrayList<Locale>();
27
28 private Integer selectedIndex = null;
29 private Integer defaultIndex = null;
30
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;
37 // }
38
39 /**
40 * Convenience constructor based on a comma separated list of iso codes (en,
41 * en_US, fr_CA, etc.). Default selection is default locale.
42 */
43 public LocaleChoice(String locales, Locale defaultLocale) {
44 if (locales == null || locales.trim().equals(""))
45 return;
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("_");
51 Locale locale;
52 if (indexUnd > 0) {
53 String language = code.substring(0, indexUnd);
54 String country = code.substring(indexUnd + 1);
55 locale = new Locale(language, country);
56 } else {
57 locale = new Locale(code);
58 }
59 availableLocales.add(locale);
60 if (locale.equals(defaultLocale))
61 defaultIndex = i;
62 }
63
64 if (defaultIndex == null)
65 defaultIndex = 0;
66
67 this.selectedIndex = defaultIndex;
68 }
69
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() + "]";
77 else
78 labels[i] = locale.getDisplayLanguage(locale) + " ("
79 + locale.getDisplayCountry(locale) + ") ["
80 + locale.getLanguage() + "_" + locale.getCountry()
81 + "]";
82
83 }
84 return labels;
85 }
86
87 public Locale getSelectedLocale() {
88 if (selectedIndex == null)
89 return null;
90 return availableLocales.get(selectedIndex);
91 }
92
93 public void setSelectedIndex(Integer selectedIndex) {
94 this.selectedIndex = selectedIndex;
95 }
96
97 public Integer getDefaultIndex() {
98 return defaultIndex;
99 }
100
101 public List<Locale> getAvailableLocales() {
102 return availableLocales;
103 }
104
105 public Locale getDefaultLocale() {
106 return availableLocales.get(getDefaultIndex());
107 }
108
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));
114 }
115 }
116
117 }