001    /**
002     * Copyright (c) 2000-present Liferay, Inc. All rights reserved.
003     *
004     * This library is free software; you can redistribute it and/or modify it under
005     * the terms of the GNU Lesser General Public License as published by the Free
006     * Software Foundation; either version 2.1 of the License, or (at your option)
007     * any later version.
008     *
009     * This library is distributed in the hope that it will be useful, but WITHOUT
010     * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
011     * FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
012     * details.
013     */
014    
015    package com.liferay.taglib.ui;
016    
017    import com.liferay.portal.kernel.language.LanguageUtil;
018    import com.liferay.portal.kernel.util.ArrayUtil;
019    import com.liferay.portal.kernel.util.GetterUtil;
020    import com.liferay.portal.kernel.util.LocaleUtil;
021    import com.liferay.portal.kernel.util.PropsKeys;
022    import com.liferay.portal.kernel.util.PropsUtil;
023    import com.liferay.portal.kernel.util.WebKeys;
024    import com.liferay.portal.theme.ThemeDisplay;
025    import com.liferay.taglib.util.IncludeTag;
026    
027    import java.util.Locale;
028    
029    import javax.servlet.http.HttpServletRequest;
030    
031    /**
032     * @author Brian Wing Shun Chan
033     */
034    public class LanguageTag extends IncludeTag {
035    
036            /**
037             * @deprecated As of 7.0.0
038             */
039            @Deprecated
040            public static final int LIST_ICON = 0;
041    
042            /**
043             * @deprecated As of 7.0.0
044             */
045            @Deprecated
046            public static final int LIST_LONG_TEXT = 1;
047    
048            /**
049             * @deprecated As of 7.0.0
050             */
051            @Deprecated
052            public static final int LIST_SHORT_TEXT = 2;
053    
054            /**
055             * @deprecated As of 7.0.0
056             */
057            @Deprecated
058            public static final int SELECT_BOX = 3;
059    
060            public void setDisplayCurrentLocale(boolean displayCurrentLocale) {
061                    _displayCurrentLocale = displayCurrentLocale;
062            }
063    
064            /**
065             * @deprecated As of 7.0.0, replaced by {@link #setDisplayStyle(String)}.
066             */
067            @Deprecated
068            public void setDisplayStyle(int displayStyle) {
069                    _displayStyle = String.valueOf(displayStyle);
070            }
071    
072            public void setDisplayStyle(String displayStyle) {
073                    _displayStyle = displayStyle;
074            }
075    
076            public void setFormAction(String formAction) {
077                    _formAction = formAction;
078            }
079    
080            public void setFormName(String formName) {
081                    _formName = formName;
082            }
083    
084            public void setLanguageId(String languageId) {
085                    _languageId = languageId;
086            }
087    
088            public void setLanguageIds(String[] languageIds) {
089                    _languageIds = languageIds;
090            }
091    
092            public void setName(String name) {
093                    _name = name;
094            }
095    
096            @Override
097            protected void cleanUp() {
098                    _displayCurrentLocale = true;
099                    _displayStyle = _DISPLAY_STYLE;
100                    _formAction = null;
101                    _formName = "fm";
102                    _languageId = null;
103                    _languageIds = null;
104                    _name = "languageId";
105            }
106    
107            @Override
108            protected String getPage() {
109                    return _PAGE;
110            }
111    
112            @Override
113            protected void setAttributes(HttpServletRequest request) {
114                    request.setAttribute(
115                            "liferay-ui:language:displayCurrentLocale",
116                            String.valueOf(_displayCurrentLocale));
117    
118                    String displayStyle = _displayStyle;
119    
120                    if (!ArrayUtil.contains(_DISPLAY_STYLE_OPTIONS, displayStyle)) {
121                            displayStyle = _DISPLAY_STYLE_OPTIONS[0];
122                    }
123    
124                    request.setAttribute(
125                            "liferay-ui:language:displayStyle", String.valueOf(displayStyle));
126                    request.setAttribute("liferay-ui:language:formAction", _formAction);
127                    request.setAttribute("liferay-ui:language:formName", _formName);
128                    request.setAttribute("liferay-ui:language:languageId", _languageId);
129    
130                    Locale[] locales = null;
131    
132                    if (ArrayUtil.isEmpty(_languageIds)) {
133                            ThemeDisplay themeDisplay = (ThemeDisplay)request.getAttribute(
134                                    WebKeys.THEME_DISPLAY);
135    
136                            locales = LanguageUtil.getAvailableLocales(
137                                    themeDisplay.getSiteGroupId());
138                    }
139                    else {
140                            locales = LocaleUtil.fromLanguageIds(_languageIds);
141                    }
142    
143                    request.setAttribute("liferay-ui:language:locales", locales);
144                    request.setAttribute("liferay-ui:language:name", _name);
145            }
146    
147            private static final String _DISPLAY_STYLE = GetterUtil.getString(
148                    PropsUtil.get(PropsKeys.LANGUAGE_DISPLAY_STYLE_DEFAULT));
149    
150            private static final String[] _DISPLAY_STYLE_OPTIONS = PropsUtil.getArray(
151                    PropsKeys.LANGUAGE_DISPLAY_STYLE_OPTIONS);
152    
153            private static final String _PAGE = "/html/taglib/ui/language/page.jsp";
154    
155            private boolean _displayCurrentLocale = true;
156            private String _displayStyle = _DISPLAY_STYLE;
157            private String _formAction;
158            private String _formName = "fm";
159            private String _languageId;
160            private String[] _languageIds;
161            private String _name = "languageId";
162    
163    }