1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20 package org.apache.myfaces.tobago.example.demo;
21
22 import org.apache.myfaces.tobago.internal.util.ObjectUtils;
23 import org.apache.myfaces.tobago.internal.util.StringUtils;
24 import org.slf4j.Logger;
25 import org.slf4j.LoggerFactory;
26
27 import javax.enterprise.context.SessionScoped;
28 import javax.enterprise.event.Event;
29 import javax.faces.application.Application;
30 import javax.faces.application.FacesMessage;
31 import javax.faces.component.UIViewRoot;
32 import javax.faces.context.FacesContext;
33 import javax.faces.model.SelectItem;
34 import javax.inject.Inject;
35 import javax.inject.Named;
36 import javax.servlet.ServletContext;
37 import java.io.Serializable;
38 import java.lang.invoke.MethodHandles;
39 import java.net.MalformedURLException;
40 import java.net.URL;
41 import java.util.ArrayList;
42 import java.util.Iterator;
43 import java.util.List;
44 import java.util.Locale;
45
46 @Named
47 @SessionScoped
48 public class LocaleController implements Serializable {
49
50 private static final Logger LOG = LoggerFactory.getLogger(MethodHandles.lookup().lookupClass());
51
52 private Locale locale;
53
54 @Inject
55 private Event<LocaleChanged> events;
56
57 public LocaleController() {
58
59 final FacesContext facesContext = FacesContext.getCurrentInstance();
60
61
62
63 final UIViewRoot viewRoot = facesContext.getViewRoot();
64 if (viewRoot != null) {
65 locale = viewRoot.getLocale();
66 } else {
67 locale = facesContext.getApplication().getDefaultLocale();
68 }
69 }
70
71 public String submit() {
72 if (LOG.isDebugEnabled()) {
73 LOG.debug("submit locale");
74 }
75 final FacesContext facesContext = FacesContext.getCurrentInstance();
76 final UIViewRoot viewRoot = facesContext.getViewRoot();
77 if (viewRoot != null) {
78 return viewRoot.getViewId();
79 } else {
80 facesContext.addMessage(null,
81 new FacesMessage(FacesMessage.SEVERITY_WARN, "ViewRoot not found!", null));
82 return Outcome.CONCEPT_LOCALE.toString();
83 }
84 }
85
86
87
88 public List<SelectItem> getLocaleItems() {
89 final FacesContext facesContext = FacesContext.getCurrentInstance();
90 final Application application = facesContext.getApplication();
91 final Locale defaultLocale = application.getDefaultLocale();
92 final Iterator supportedLocales = application.getSupportedLocales();
93
94 boolean defaultInList = false;
95 final List<SelectItem> localeItems = new ArrayList<>();
96 while (supportedLocales.hasNext()) {
97 final Locale supported = (Locale) supportedLocales.next();
98 localeItems.add(createLocaleItem(supported));
99 if (supported.equals(defaultLocale)) {
100 defaultInList = true;
101 }
102 }
103
104
105
106 if (!defaultInList && defaultLocale != null) {
107 localeItems.add(0, createLocaleItem(defaultLocale));
108 }
109 return localeItems;
110 }
111
112 private SelectItem createLocaleItem(final Locale localeItem) {
113 if (locale != null) {
114 return new SelectItem(localeItem, localeItem.getDisplayName(locale));
115 } else {
116 return new SelectItem(localeItem, localeItem.getDisplayName(localeItem));
117 }
118 }
119
120 public Locale getLocale() {
121 return locale;
122 }
123
124 public String getLocalizedLocale() {
125 if (locale != null) {
126 return locale.getDisplayName(locale);
127 } else {
128 return null;
129 }
130 }
131
132 public String getImageCountryName() {
133 final String language = locale.getLanguage();
134 final String country = locale.getCountry();
135 final String suffix
136 = StringUtils.isNotBlank(language) && StringUtils.isNotBlank(country) ? "_" + language + "_" + country : "";
137 String url = "/image/country" + suffix + ".png";
138 final ServletContext servletContext =
139 (ServletContext) FacesContext.getCurrentInstance().getExternalContext().getContext();
140 URL resource = null;
141 try {
142 resource = servletContext.getResource(url);
143 } catch (final MalformedURLException e) {
144
145 }
146 if (resource == null) {
147 url = "/image/country.png";
148 }
149 return url;
150 }
151
152 public void setLocale(final Locale locale) {
153 if (!ObjectUtils.equals(this.locale, locale)) {
154 events.fire(new LocaleChanged());
155 }
156 this.locale = locale;
157 }
158
159 }