2 * Copyright (C) 2016 Roland Haeder
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.
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.
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/>.
17 package org.mxchange.addressbook.beans.localization;
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;
28 * A session bean for handling localization/internationalization changes. This
29 * class is based on an example at [1] from mkyong.
31 * 1: http://www.mkyong.com/jsf2/jsf-2-internationalization-example/
33 * @author Roland Haeder<roland@mxchange.org>
35 @Named ("localization")
37 public class AddressbookLocalizationSessionBean extends BaseDatabaseBean implements AddressbookLocalizationSessionController {
42 private static final long serialVersionUID = 158_768_216_759_107L;
47 private Locale locale;
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();
56 public void setLanguage (final String language) {
58 this.getLoggerBeanLocal().logTrace(MessageFormat.format("JobsLocalizationSessionBean::setLanguage: language={0} - CALLED!", language)); //NOI18N
61 String[] splits = language.split("_");
62 if (null == splits[1]) {
66 // Get new locale with upper-case country code
67 Locale loc = new Locale(splits[0], splits[1]);
70 this.getLoggerBeanLocal().logDebug(MessageFormat.format("JobsLocalizationSessionBean::setLanguage: loc={0}", loc)); //NOI18N
72 // Set it here and in the JSF context
74 FacesContext.getCurrentInstance().getViewRoot().setLocale(loc);
77 this.getLoggerBeanLocal().logTrace("JobsLocalizationSessionBean::setLanguage: EXIT!"); //NOI18N
81 public Locale getLocale () {
82 this.getLoggerBeanLocal().logTrace(MessageFormat.format("JobsLocalizationSessionBean::getLocale(): locale={0} - EXIT!", this.locale)); //NOI18N
87 public void setLocale (final Locale locale) {
88 this.getLoggerBeanLocal().logTrace(MessageFormat.format("JobsLocalizationSessionBean::setLocale(): locale={0} - CALLED!", locale)); //NOI18N
93 public Locale[] getSelectableLocalizations () {
94 Locale[] locales = {Locale.GERMANY, Locale.US};
99 * Initializer for this bean
102 public void init () {
104 this.getLoggerBeanLocal().logTrace("JobsLocalizationSessionBean::init: CALLED!"); //NOI18N
106 // Create locale instance from context
107 Locale loc = FacesContext.getCurrentInstance().getExternalContext().getRequestLocale();
110 this.getLoggerBeanLocal().logDebug(MessageFormat.format("JobsLocalizationSessionBean::init: loc={0}", loc)); //NOI18N
116 this.getLoggerBeanLocal().logTrace("JobsLocalizationSessionBean::init: EXIT!"); //NOI18N