]> git.mxchange.org Git - addressbook-war.git/blob - src/java/org/mxchange/addressbook/beans/localization/AddressbookLocalizationSessionBean.java
aed99966287304f2df4b11c42d2d9c11faea4210
[addressbook-war.git] / src / java / org / mxchange / addressbook / beans / localization / AddressbookLocalizationSessionBean.java
1 /*
2  * Copyright (C) 2016 Roland Haeder
3  *
4  * This program is free software: you can redistribute it and/or modify
5  * it under the terms of the GNU Affero General Public License as
6  * published by the Free Software Foundation, either version 3 of the
7  * License, or (at your option) any later version.
8  *
9  * This program is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12  * GNU Affero General Public License for more details.
13  *
14  * You should have received a copy of the GNU Affero General Public License
15  * along with this program.  If not, see <http://www.gnu.org/licenses/>.
16  */
17 package org.mxchange.addressbook.beans.localization;
18
19 import java.util.Locale;
20 import javax.annotation.PostConstruct;
21 import javax.faces.bean.SessionScoped;
22 import javax.faces.context.FacesContext;
23 import javax.inject.Named;
24 import org.mxchange.jcoreee.database.BaseDatabaseBean;
25
26 /**
27  * A session bean for handling localization/internationalization changes. This
28  * class is based on an example at [1] from mkyong.
29  * <p>
30  * 1: http://www.mkyong.com/jsf2/jsf-2-internationalization-example/
31  * <p>
32  * @author Roland Haeder<roland@mxchange.org>
33  */
34 @Named ("localizationController")
35 @SessionScoped
36 public class AddressbookLocalizationSessionBean extends BaseDatabaseBean implements AddressbookLocalizationSessionController {
37
38         /**
39          * Serial number
40          */
41         private static final long serialVersionUID = 158_768_216_759_107L;
42
43         /**
44          * Current Locale
45          */
46         private Locale locale;
47
48         @Override
49         public String getLanguage () {
50                 //this.getLoggerBeanLocal().logTrace(MessageFormat.format("AddressbookLocalizationSessionBean::getLanguage(): locale.language={0} - EXIT!", this.getLocale().getLanguage())); //NOI18N
51                 return this.getLocale().getLanguage().toLowerCase();
52         }
53
54         @Override
55         public void setLanguage (final String language) {
56                 // Log trace message
57                 //this.getLoggerBeanLocal().logTrace(MessageFormat.format("AddressbookLocalizationSessionBean::setLanguage: language={0} - CALLED!", language)); //NOI18N
58
59                 // Language splits
60                 String[] splits = language.split("_"); //NOI18N
61                 if (null == splits[1]) {
62                         splits[1] = ""; //NOI18N
63                 }
64
65                 // Get new locale with upper-case country code
66                 Locale loc = new Locale(splits[0], splits[1]);
67
68                 // Log debug message
69                 //this.getLoggerBeanLocal().logDebug(MessageFormat.format("AddressbookLocalizationSessionBean::setLanguage: loc={0}", loc)); //NOI18N
70
71                 // Set it here and in the JSF context
72                 this.setLocale(loc);
73                 FacesContext.getCurrentInstance().getViewRoot().setLocale(loc);
74
75                 // Log trace message
76                 //this.getLoggerBeanLocal().logTrace("AddressbookLocalizationSessionBean::setLanguage: EXIT!"); //NOI18N
77         }
78
79         @Override
80         public Locale getLocale () {
81                 //this.getLoggerBeanLocal().logTrace(MessageFormat.format("AddressbookLocalizationSessionBean::getLocale(): locale={0} - EXIT!", this.locale)); //NOI18N
82                 return this.locale;
83         }
84
85         @Override
86         public void setLocale (final Locale locale) {
87                 //this.getLoggerBeanLocal().logTrace(MessageFormat.format("AddressbookLocalizationSessionBean::setLocale(): locale={0} - CALLED!", locale)); //NOI18N
88                 this.locale = locale;
89         }
90
91         @Override
92         public Locale[] getSelectableLocalizations () {
93                 Locale[] locales = {
94                         Locale.GERMANY,
95                         Locale.US
96                 };
97                 return locales;
98         }
99
100         /**
101          * Initializer for this bean
102          */
103         @PostConstruct
104         public void init () {
105                 // Log trace message
106                 //this.getLoggerBeanLocal().logTrace("AddressbookLocalizationSessionBean::init: CALLED!"); //NOI18N
107
108                 // Create locale instance from context
109                 Locale loc = FacesContext.getCurrentInstance().getExternalContext().getRequestLocale();
110
111                 // Log debug message
112                 //this.getLoggerBeanLocal().logDebug(MessageFormat.format("AddressbookLocalizationSessionBean::init: loc={0}", loc)); //NOI18N
113
114                 // Set it here
115                 this.setLocale(loc);
116
117                 // Log trace message
118                 //this.getLoggerBeanLocal().logTrace("AddressbookLocalizationSessionBean::init: EXIT!"); //NOI18N
119         }
120
121 }