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 String getLocalizationXmlFromPreferences(
085                    PortletPreferences preferences, PortletRequest portletRequest,
086                    String parameter) {
087    
088                    return getLocalization().getLocalizationXmlFromPreferences(
089                            preferences, portletRequest, parameter);
090            }
091    
092            /**
093             * @deprecated Use <code>getLocalizationMap</code>.
094             */
095            public static Map<Locale, String> getLocalizedParameter(
096                    PortletRequest portletRequest, String parameter) {
097    
098                    return getLocalization().getLocalizedParameter(
099                            portletRequest, parameter);
100            }
101    
102            public static List<Locale> getModifiedLocales(
103                    Map<Locale, String> oldMap, Map<Locale, String> newMap) {
104    
105                    if ((newMap == null) || newMap.isEmpty()) {
106                            return Collections.emptyList();
107                    }
108    
109                    List<Locale> modifiedLocales = new ArrayList<Locale>();
110    
111                    Locale[] locales = LanguageUtil.getAvailableLocales();
112    
113                    for (Locale locale : locales) {
114                            String oldValue = oldMap.get(locale);
115                            String newValue = newMap.get(locale);
116    
117                            if (!oldValue.equals(newValue)) {
118                                    modifiedLocales.add(locale);
119                            }
120                    }
121    
122                    return modifiedLocales;
123            }
124    
125            public static String getPreferencesKey(String key, String languageId) {
126                    return getLocalization().getPreferencesKey(key, languageId);
127            }
128    
129            public static String getPreferencesValue(
130                    PortletPreferences preferences, String key, String languageId) {
131    
132                    return getLocalization().getPreferencesValue(
133                            preferences, key, languageId);
134            }
135    
136            public static String getPreferencesValue(
137                    PortletPreferences preferences, String key, String languageId,
138                    boolean useDefault) {
139    
140                    return getLocalization().getPreferencesValue(
141                            preferences, key, languageId, useDefault);
142            }
143    
144            public static String[] getPreferencesValues(
145                    PortletPreferences preferences, String key, String languageId) {
146    
147                    return getLocalization().getPreferencesValues(
148                            preferences, key, languageId);
149            }
150    
151            public static String[] getPreferencesValues(
152                    PortletPreferences preferences, String key, String languageId,
153                    boolean useDefault) {
154    
155                    return getLocalization().getPreferencesValues(
156                            preferences, key, languageId, useDefault);
157            }
158    
159            public static String removeLocalization(
160                    String xml, String key, String requestedLanguageId) {
161    
162                    return getLocalization().removeLocalization(
163                            xml, key, requestedLanguageId);
164            }
165    
166            public static String removeLocalization(
167                    String xml, String key, String requestedLanguageId, boolean cdata) {
168    
169                    return getLocalization().removeLocalization(
170                            xml, key, requestedLanguageId, cdata);
171            }
172    
173            public static String removeLocalization(
174                    String xml, String key, String requestedLanguageId, boolean cdata,
175                    boolean localized) {
176    
177                    return getLocalization().removeLocalization(
178                            xml, key, requestedLanguageId, cdata, localized);
179            }
180    
181            public static void setLocalizedPreferencesValues(
182                            PortletRequest portletRequest, PortletPreferences preferences,
183                            String parameter)
184                    throws Exception {
185    
186                    getLocalization().setLocalizedPreferencesValues(
187                            portletRequest, preferences, parameter);
188            }
189    
190            public static void setPreferencesValue(
191                            PortletPreferences preferences, String key, String languageId,
192                            String value)
193                    throws Exception {
194    
195                    getLocalization().setPreferencesValue(
196                            preferences, key, languageId, value);
197            }
198    
199            public static void setPreferencesValues(
200                            PortletPreferences preferences, String key, String languageId,
201                            String[] values)
202                    throws Exception {
203    
204                    getLocalization().setPreferencesValues(
205                            preferences, key, languageId, values);
206            }
207    
208            public static String updateLocalization(
209                    String xml, String key, String value) {
210    
211                    return getLocalization().updateLocalization(xml, key, value);
212            }
213    
214            public static String updateLocalization(
215                    String xml, String key, String value, String requestedLanguageId) {
216    
217                    return getLocalization().updateLocalization(
218                            xml, key, value, requestedLanguageId);
219            }
220    
221            public static String updateLocalization(
222                    String xml, String key, String value, String requestedLanguageId,
223                    String defaultLanguageId) {
224    
225                    return getLocalization().updateLocalization(
226                            xml, key, value, requestedLanguageId, defaultLanguageId);
227            }
228    
229            public static String updateLocalization(
230                    String xml, String key, String value, String requestedLanguageId,
231                    String defaultLanguageId, boolean cdata) {
232    
233                    return getLocalization().updateLocalization(
234                            xml, key, value, requestedLanguageId, defaultLanguageId, cdata);
235            }
236    
237            public static String updateLocalization(
238                    String xml, String key, String value, String requestedLanguageId,
239                    String defaultLanguageId, boolean cdata, boolean localized) {
240    
241                    return getLocalization().updateLocalization(
242                            xml, key, value, requestedLanguageId, defaultLanguageId, cdata,
243                            localized);
244            }
245    
246            public void setLocalization(Localization localization) {
247                    _localization = localization;
248            }
249    
250            private static Localization _localization;
251    
252    }