001    /**
002     * Copyright (c) 2000-2012 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.portal.kernel.util;
016    
017    import com.liferay.portal.kernel.json.JSONObject;
018    import com.liferay.portal.kernel.language.LanguageUtil;
019    
020    import java.util.ArrayList;
021    import java.util.Collections;
022    import java.util.List;
023    import java.util.Locale;
024    import java.util.Map;
025    
026    import javax.portlet.PortletPreferences;
027    import javax.portlet.PortletRequest;
028    
029    /**
030     * @author Alexander Chow
031     * @author Jorge Ferrer
032     * @author Mauro Mariuzzo
033     * @author Julio Camarero
034     * @author Brian Wing Shun Chan
035     * @see    Localization
036     */
037    public class LocalizationUtil {
038    
039            public static Object deserialize(JSONObject jsonObject) {
040                    return getLocalization().deserialize(jsonObject);
041            }
042    
043            public static String[] getAvailableLocales(String xml) {
044                    return getLocalization().getAvailableLocales(xml);
045            }
046    
047            public static String getDefaultLocale(String xml) {
048                    return getLocalization().getDefaultLocale(xml);
049            }
050    
051            public static Localization getLocalization() {
052                    return _localization;
053            }
054    
055            public static String getLocalization(
056                    String xml, String requestedLanguageId) {
057    
058                    return getLocalization().getLocalization(xml, requestedLanguageId);
059            }
060    
061            public static String getLocalization(
062                    String xml, String requestedLanguageId, boolean useDefault) {
063    
064                    return getLocalization().getLocalization(
065                            xml, requestedLanguageId, useDefault);
066            }
067    
068            public static Map<Locale, String> getLocalizationMap(
069                    PortletPreferences preferences, String parameter) {
070    
071                    return getLocalization().getLocalizationMap(preferences, parameter);
072            }
073    
074            public static Map<Locale, String> getLocalizationMap(
075                    PortletRequest portletRequest, String parameter) {
076    
077                    return getLocalization().getLocalizationMap(portletRequest, parameter);
078            }
079    
080            public static Map<Locale, String> getLocalizationMap(String xml) {
081                    return getLocalization().getLocalizationMap(xml);
082            }
083    
084            public static Map<Locale, String> getLocalizationMap(
085                    String[] languageIds, String[] values) {
086    
087                    return getLocalization().getLocalizationMap(languageIds, values);
088            }
089    
090            public static String getLocalizationXmlFromPreferences(
091                    PortletPreferences preferences, PortletRequest portletRequest,
092                    String parameter) {
093    
094                    return getLocalization().getLocalizationXmlFromPreferences(
095                            preferences, portletRequest, parameter);
096            }
097    
098            /**
099             * @deprecated Use <code>getLocalizationMap</code>.
100             */
101            public static Map<Locale, String> getLocalizedParameter(
102                    PortletRequest portletRequest, String parameter) {
103    
104                    return getLocalization().getLocalizedParameter(
105                            portletRequest, parameter);
106            }
107    
108            public static List<Locale> getModifiedLocales(
109                    Map<Locale, String> oldMap, Map<Locale, String> newMap) {
110    
111                    if ((newMap == null) || newMap.isEmpty()) {
112                            return Collections.emptyList();
113                    }
114    
115                    List<Locale> modifiedLocales = new ArrayList<Locale>();
116    
117                    Locale[] locales = LanguageUtil.getAvailableLocales();
118    
119                    for (Locale locale : locales) {
120                            String oldValue = oldMap.get(locale);
121                            String newValue = newMap.get(locale);
122    
123                            if (!oldValue.equals(newValue)) {
124                                    modifiedLocales.add(locale);
125                            }
126                    }
127    
128                    return modifiedLocales;
129            }
130    
131            public static String getPreferencesKey(String key, String languageId) {
132                    return getLocalization().getPreferencesKey(key, languageId);
133            }
134    
135            public static String getPreferencesValue(
136                    PortletPreferences preferences, String key, String languageId) {
137    
138                    return getLocalization().getPreferencesValue(
139                            preferences, key, languageId);
140            }
141    
142            public static String getPreferencesValue(
143                    PortletPreferences preferences, String key, String languageId,
144                    boolean useDefault) {
145    
146                    return getLocalization().getPreferencesValue(
147                            preferences, key, languageId, useDefault);
148            }
149    
150            public static String[] getPreferencesValues(
151                    PortletPreferences preferences, String key, String languageId) {
152    
153                    return getLocalization().getPreferencesValues(
154                            preferences, key, languageId);
155            }
156    
157            public static String[] getPreferencesValues(
158                    PortletPreferences preferences, String key, String languageId,
159                    boolean useDefault) {
160    
161                    return getLocalization().getPreferencesValues(
162                            preferences, key, languageId, useDefault);
163            }
164    
165            public static String removeLocalization(
166                    String xml, String key, String requestedLanguageId) {
167    
168                    return getLocalization().removeLocalization(
169                            xml, key, requestedLanguageId);
170            }
171    
172            public static String removeLocalization(
173                    String xml, String key, String requestedLanguageId, boolean cdata) {
174    
175                    return getLocalization().removeLocalization(
176                            xml, key, requestedLanguageId, cdata);
177            }
178    
179            public static String removeLocalization(
180                    String xml, String key, String requestedLanguageId, boolean cdata,
181                    boolean localized) {
182    
183                    return getLocalization().removeLocalization(
184                            xml, key, requestedLanguageId, cdata, localized);
185            }
186    
187            public static void setLocalizedPreferencesValues(
188                            PortletRequest portletRequest, PortletPreferences preferences,
189                            String parameter)
190                    throws Exception {
191    
192                    getLocalization().setLocalizedPreferencesValues(
193                            portletRequest, preferences, parameter);
194            }
195    
196            public static void setPreferencesValue(
197                            PortletPreferences preferences, String key, String languageId,
198                            String value)
199                    throws Exception {
200    
201                    getLocalization().setPreferencesValue(
202                            preferences, key, languageId, value);
203            }
204    
205            public static void setPreferencesValues(
206                            PortletPreferences preferences, String key, String languageId,
207                            String[] values)
208                    throws Exception {
209    
210                    getLocalization().setPreferencesValues(
211                            preferences, key, languageId, values);
212            }
213    
214            public static String updateLocalization(
215                    String xml, String key, String value) {
216    
217                    return getLocalization().updateLocalization(xml, key, value);
218            }
219    
220            public static String updateLocalization(
221                    String xml, String key, String value, String requestedLanguageId) {
222    
223                    return getLocalization().updateLocalization(
224                            xml, key, value, requestedLanguageId);
225            }
226    
227            public static String updateLocalization(
228                    String xml, String key, String value, String requestedLanguageId,
229                    String defaultLanguageId) {
230    
231                    return getLocalization().updateLocalization(
232                            xml, key, value, requestedLanguageId, defaultLanguageId);
233            }
234    
235            public static String updateLocalization(
236                    String xml, String key, String value, String requestedLanguageId,
237                    String defaultLanguageId, boolean cdata) {
238    
239                    return getLocalization().updateLocalization(
240                            xml, key, value, requestedLanguageId, defaultLanguageId, cdata);
241            }
242    
243            public static String updateLocalization(
244                    String xml, String key, String value, String requestedLanguageId,
245                    String defaultLanguageId, boolean cdata, boolean localized) {
246    
247                    return getLocalization().updateLocalization(
248                            xml, key, value, requestedLanguageId, defaultLanguageId, cdata,
249                            localized);
250            }
251    
252            public void setLocalization(Localization localization) {
253                    _localization = localization;
254            }
255    
256            private static Localization _localization;
257    
258    }