]> git.mxchange.org Git - addressbook-war.git/blob - src/java/org/mxchange/addressbook/beans/localization/AddressbookLocalizationSessionBean.java
Localization support almost finished:
[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.text.MessageFormat;
20 import java.util.Locale;
21 import javax.annotation.PostConstruct;
22 import javax.faces.bean.SessionScoped;
23 import javax.faces.context.FacesContext;
24 import javax.inject.Named;
25 import org.mxchange.jcoreee.database.BaseDatabaseBean;
26
27 /**
28  * A session bean for handling localization/internationalization changes. This
29  * class is based on an example at [1] from mkyong.
30  * <p>
31  * 1: http://www.mkyong.com/jsf2/jsf-2-internationalization-example/
32  * <p>
33  * @author Roland Haeder<roland@mxchange.org>
34  */
35 @Named ("localization")
36 @SessionScoped
37 public class AddressbookLocalizationSessionBean extends BaseDatabaseBean implements AddressbookLocalizationSessionController {
38
39         /**
40          * Serial number
41          */
42         private static final long serialVersionUID = 158_768_216_759_107L;
43
44         /**
45          * Current Locale
46          */
47         private Locale locale;
48
49         @Override
50         public String getLanguage () {
51                 this.getLoggerBeanLocal().logTrace(MessageFormat.format("JobsLocalizationSessionBean::getLanguage(): locale.language={0} - EXIT!", this.getLocale().getLanguage())); //NOI18N
52                 return this.getLocale().getLanguage().toLowerCase();
53         }
54
55         @Override
56         public void setLanguage (final String language) {
57                 // Log trace message
58                 this.getLoggerBeanLocal().logTrace(MessageFormat.format("JobsLocalizationSessionBean::setLanguage: language={0} - CALLED!", language)); //NOI18N
59
60                 // Language splits
61                 String[] splits = language.split("_");
62                 if (null == splits[1]) {
63                         splits[1] = "";
64                 }
65
66                 // Get new locale with upper-case country code
67                 Locale loc = new Locale(splits[0], splits[1]);
68
69                 // Log debug message
70                 this.getLoggerBeanLocal().logDebug(MessageFormat.format("JobsLocalizationSessionBean::setLanguage: loc={0}", loc)); //NOI18N
71
72                 // Set it here and in the JSF context
73                 this.setLocale(loc);
74                 FacesContext.getCurrentInstance().getViewRoot().setLocale(loc);
75
76                 // Log trace message
77                 this.getLoggerBeanLocal().logTrace("JobsLocalizationSessionBean::setLanguage: EXIT!"); //NOI18N
78         }
79
80         @Override
81         public Locale getLocale () {
82                 this.getLoggerBeanLocal().logTrace(MessageFormat.format("JobsLocalizationSessionBean::getLocale(): locale={0} - EXIT!", this.locale)); //NOI18N
83                 return this.locale;
84         }
85
86         @Override
87         public void setLocale (final Locale locale) {
88                 this.getLoggerBeanLocal().logTrace(MessageFormat.format("JobsLocalizationSessionBean::setLocale(): locale={0} - CALLED!", locale)); //NOI18N
89                 this.locale = locale;
90         }
91
92         @Override
93         public Locale[] getSelectableLocalizations () {
94                 Locale[] locales = {Locale.GERMANY, Locale.US};
95                 return locales;
96         }
97
98         /**
99          * Initializer for this bean
100          */
101         @PostConstruct
102         public void init () {
103                 // Log trace message
104                 this.getLoggerBeanLocal().logTrace("JobsLocalizationSessionBean::init: CALLED!"); //NOI18N
105
106                 // Create locale instance from context
107                 Locale loc = FacesContext.getCurrentInstance().getExternalContext().getRequestLocale();
108
109                 // Log debug message
110                 this.getLoggerBeanLocal().logDebug(MessageFormat.format("JobsLocalizationSessionBean::init: loc={0}", loc)); //NOI18N
111
112                 // Set it here
113                 this.setLocale(loc);
114
115                 // Log trace message
116                 this.getLoggerBeanLocal().logTrace("JobsLocalizationSessionBean::init: EXIT!"); //NOI18N
117         }
118
119 }