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.portal.kernel.util;
016    
017    import com.liferay.portal.kernel.json.JSONObject;
018    import com.liferay.portal.kernel.language.LanguageUtil;
019    import com.liferay.portal.kernel.security.pacl.permission.PortalRuntimePermission;
020    import com.liferay.portal.kernel.settings.Settings;
021    import com.liferay.portal.kernel.xml.Document;
022    
023    import java.util.ArrayList;
024    import java.util.Collections;
025    import java.util.List;
026    import java.util.Locale;
027    import java.util.Map;
028    
029    import javax.portlet.PortletPreferences;
030    import javax.portlet.PortletRequest;
031    
032    import javax.servlet.http.HttpServletRequest;
033    
034    /**
035     * @author Alexander Chow
036     * @author Jorge Ferrer
037     * @author Mauro Mariuzzo
038     * @author Julio Camarero
039     * @author Brian Wing Shun Chan
040     * @see    Localization
041     */
042    public class LocalizationUtil {
043    
044            public static Object deserialize(JSONObject jsonObject) {
045                    return getLocalization().deserialize(jsonObject);
046            }
047    
048            public static String[] getAvailableLanguageIds(Document document) {
049                    return getLocalization().getAvailableLanguageIds(document);
050            }
051    
052            public static String[] getAvailableLanguageIds(String xml) {
053                    return getLocalization().getAvailableLanguageIds(xml);
054            }
055    
056            /**
057             * @deprecated As of 6.2.0, replaced by {@link
058             *             #getAvailableLanguageIds(String)}
059             */
060            @Deprecated
061            public static String[] getAvailableLocales(String xml) {
062                    return getAvailableLanguageIds(xml);
063            }
064    
065            public static Locale getDefaultImportLocale(
066                    String className, long classPK, Locale contentDefaultLocale,
067                    Locale[] contentAvailableLocales) {
068    
069                    return getLocalization().getDefaultImportLocale(
070                            className, classPK, contentDefaultLocale, contentAvailableLocales);
071            }
072    
073            public static String getDefaultLanguageId(Document document) {
074                    return getLocalization().getDefaultLanguageId(document);
075            }
076    
077            public static String getDefaultLanguageId(
078                    Document document, Locale defaultLocale) {
079    
080                    return getLocalization().getDefaultLanguageId(document, defaultLocale);
081            }
082    
083            public static String getDefaultLanguageId(String xml) {
084                    return getLocalization().getDefaultLanguageId(xml);
085            }
086    
087            public static String getDefaultLanguageId(
088                    String xml, Locale defaultLocale) {
089    
090                    return getLocalization().getDefaultLanguageId(xml, defaultLocale);
091            }
092    
093            /**
094             * @deprecated As of 6.2.0, replaced by {@link
095             *             #getDefaultLanguageId(String)}
096             */
097            @Deprecated
098            public static String getDefaultLocale(String xml) {
099                    return getDefaultLanguageId(xml);
100            }
101    
102            public static Localization getLocalization() {
103                    PortalRuntimePermission.checkGetBeanProperty(LocalizationUtil.class);
104    
105                    return _localization;
106            }
107    
108            public static String getLocalization(
109                    String xml, String requestedLanguageId) {
110    
111                    return getLocalization().getLocalization(xml, requestedLanguageId);
112            }
113    
114            public static String getLocalization(
115                    String xml, String requestedLanguageId, boolean useDefault) {
116    
117                    return getLocalization().getLocalization(
118                            xml, requestedLanguageId, useDefault);
119            }
120    
121            public static String getLocalization(
122                    String xml, String requestedLanguageId, boolean useDefault,
123                    String defaultValue) {
124    
125                    return getLocalization().getLocalization(
126                            xml, requestedLanguageId, useDefault, defaultValue);
127            }
128    
129            public static Map<Locale, String> getLocalizationMap(
130                    HttpServletRequest request, String parameter) {
131    
132                    return getLocalization().getLocalizationMap(request, parameter);
133            }
134    
135            public static Map<Locale, String> getLocalizationMap(
136                    PortletPreferences preferences, String preferenceName) {
137    
138                    return getLocalization().getLocalizationMap(
139                            preferences, preferenceName);
140            }
141    
142            public static Map<Locale, String> getLocalizationMap(
143                    PortletPreferences preferences, String preferenceName,
144                    String propertyName) {
145    
146                    return getLocalization().getLocalizationMap(
147                            preferences, preferenceName, propertyName);
148            }
149    
150            public static Map<Locale, String> getLocalizationMap(
151                    PortletRequest portletRequest, String parameter) {
152    
153                    return getLocalization().getLocalizationMap(portletRequest, parameter);
154            }
155    
156            public static Map<Locale, String> getLocalizationMap(
157                    PortletRequest portletRequest, String parameter,
158                    Map<Locale, String> defaultValues) {
159    
160                    return getLocalization().getLocalizationMap(
161                            portletRequest, parameter, defaultValues);
162            }
163    
164            public static Map<Locale, String> getLocalizationMap(String xml) {
165                    return getLocalization().getLocalizationMap(xml);
166            }
167    
168            public static Map<Locale, String> getLocalizationMap(
169                    String xml, boolean useDefault) {
170    
171                    return getLocalization().getLocalizationMap(xml, useDefault);
172            }
173    
174            public static Map<Locale, String> getLocalizationMap(
175                    String bundleName, ClassLoader classLoader, String key,
176                    boolean includeBetaLocales) {
177    
178                    return getLocalization().getLocalizationMap(
179                            bundleName, classLoader, key, includeBetaLocales);
180            }
181    
182            public static Map<Locale, String> getLocalizationMap(
183                    String[] languageIds, String[] values) {
184    
185                    return getLocalization().getLocalizationMap(languageIds, values);
186            }
187    
188            public static String getLocalizationXmlFromPreferences(
189                    PortletPreferences preferences, PortletRequest portletRequest,
190                    String parameter) {
191    
192                    return getLocalization().getLocalizationXmlFromPreferences(
193                            preferences, portletRequest, parameter);
194            }
195    
196            public static String getLocalizationXmlFromPreferences(
197                    PortletPreferences preferences, PortletRequest portletRequest,
198                    String parameter, String defaultValue) {
199    
200                    return getLocalization().getLocalizationXmlFromPreferences(
201                            preferences, portletRequest, parameter, defaultValue);
202            }
203    
204            public static String getLocalizationXmlFromPreferences(
205                    PortletPreferences preferences, PortletRequest portletRequest,
206                    String parameter, String prefix, String defaultValue) {
207    
208                    return getLocalization().getLocalizationXmlFromPreferences(
209                            preferences, portletRequest, parameter, prefix, defaultValue);
210            }
211    
212            public static String getLocalizedName(String name, String languageId) {
213                    return getLocalization().getLocalizedName(name, languageId);
214            }
215    
216            /**
217             * @deprecated As of 6.2.0, replaced by {@link #getLocalizationMap}
218             */
219            @Deprecated
220            public static Map<Locale, String> getLocalizedParameter(
221                    PortletRequest portletRequest, String parameter) {
222    
223                    return getLocalization().getLocalizedParameter(
224                            portletRequest, parameter);
225            }
226    
227            public static List<Locale> getModifiedLocales(
228                    Map<Locale, String> oldMap, Map<Locale, String> newMap) {
229    
230                    if ((newMap == null) || newMap.isEmpty()) {
231                            return Collections.emptyList();
232                    }
233    
234                    List<Locale> modifiedLocales = new ArrayList<Locale>();
235    
236                    Locale[] locales = LanguageUtil.getAvailableLocales();
237    
238                    for (Locale locale : locales) {
239                            String oldValue = oldMap.get(locale);
240                            String newValue = newMap.get(locale);
241    
242                            if (!oldValue.equals(newValue)) {
243                                    modifiedLocales.add(locale);
244                            }
245                    }
246    
247                    return modifiedLocales;
248            }
249    
250            /**
251             * @deprecated As of 7.0.0 replaced by {@link
252             *             #getLocalizedName(String, String)}
253             */
254            @Deprecated
255            public static String getPreferencesKey(String key, String languageId) {
256                    return getLocalization().getPreferencesKey(key, languageId);
257            }
258    
259            public static String getPreferencesValue(
260                    PortletPreferences preferences, String key, String languageId) {
261    
262                    return getLocalization().getPreferencesValue(
263                            preferences, key, languageId);
264            }
265    
266            public static String getPreferencesValue(
267                    PortletPreferences preferences, String key, String languageId,
268                    boolean useDefault) {
269    
270                    return getLocalization().getPreferencesValue(
271                            preferences, key, languageId, useDefault);
272            }
273    
274            public static String[] getPreferencesValues(
275                    PortletPreferences preferences, String key, String languageId) {
276    
277                    return getLocalization().getPreferencesValues(
278                            preferences, key, languageId);
279            }
280    
281            public static String[] getPreferencesValues(
282                    PortletPreferences preferences, String key, String languageId,
283                    boolean useDefault) {
284    
285                    return getLocalization().getPreferencesValues(
286                            preferences, key, languageId, useDefault);
287            }
288    
289            public static String getSettingsValue(
290                    Settings settings, String key, String languageId) {
291    
292                    return getLocalization().getSettingsValue(settings, key, languageId);
293            }
294    
295            public static String getSettingsValue(
296                    Settings settings, String key, String languageId, boolean useDefault) {
297    
298                    return getLocalization().getSettingsValue(
299                            settings, key, languageId, useDefault);
300            }
301    
302            public static String[] getSettingsValues(
303                    Settings settings, String key, String languageId) {
304    
305                    return getLocalization().getSettingsValues(settings, key, languageId);
306            }
307    
308            public static String[] getSettingsValues(
309                    Settings settings, String key, String languageId, boolean useDefault) {
310    
311                    return getLocalization().getSettingsValues(
312                            settings, key, languageId, useDefault);
313            }
314    
315            public static String removeLocalization(
316                    String xml, String key, String requestedLanguageId) {
317    
318                    return getLocalization().removeLocalization(
319                            xml, key, requestedLanguageId);
320            }
321    
322            public static String removeLocalization(
323                    String xml, String key, String requestedLanguageId, boolean cdata) {
324    
325                    return getLocalization().removeLocalization(
326                            xml, key, requestedLanguageId, cdata);
327            }
328    
329            public static String removeLocalization(
330                    String xml, String key, String requestedLanguageId, boolean cdata,
331                    boolean localized) {
332    
333                    return getLocalization().removeLocalization(
334                            xml, key, requestedLanguageId, cdata, localized);
335            }
336    
337            public static void setLocalizedPreferencesValues(
338                            PortletRequest portletRequest, PortletPreferences preferences,
339                            String parameter)
340                    throws Exception {
341    
342                    getLocalization().setLocalizedPreferencesValues(
343                            portletRequest, preferences, parameter);
344            }
345    
346            public static void setPreferencesValue(
347                            PortletPreferences preferences, String key, String languageId,
348                            String value)
349                    throws Exception {
350    
351                    getLocalization().setPreferencesValue(
352                            preferences, key, languageId, value);
353            }
354    
355            public static void setPreferencesValues(
356                            PortletPreferences preferences, String key, String languageId,
357                            String[] values)
358                    throws Exception {
359    
360                    getLocalization().setPreferencesValues(
361                            preferences, key, languageId, values);
362            }
363    
364            public static String updateLocalization(
365                    Map<Locale, String> localizationMap, String xml, String key,
366                    String defaultLanguageId) {
367    
368                    return getLocalization().updateLocalization(
369                            localizationMap, xml, key, defaultLanguageId);
370            }
371    
372            public static String updateLocalization(
373                    String xml, String key, String value) {
374    
375                    return getLocalization().updateLocalization(xml, key, value);
376            }
377    
378            public static String updateLocalization(
379                    String xml, String key, String value, String requestedLanguageId) {
380    
381                    return getLocalization().updateLocalization(
382                            xml, key, value, requestedLanguageId);
383            }
384    
385            public static String updateLocalization(
386                    String xml, String key, String value, String requestedLanguageId,
387                    String defaultLanguageId) {
388    
389                    return getLocalization().updateLocalization(
390                            xml, key, value, requestedLanguageId, defaultLanguageId);
391            }
392    
393            public static String updateLocalization(
394                    String xml, String key, String value, String requestedLanguageId,
395                    String defaultLanguageId, boolean cdata) {
396    
397                    return getLocalization().updateLocalization(
398                            xml, key, value, requestedLanguageId, defaultLanguageId, cdata);
399            }
400    
401            public static String updateLocalization(
402                    String xml, String key, String value, String requestedLanguageId,
403                    String defaultLanguageId, boolean cdata, boolean localized) {
404    
405                    return getLocalization().updateLocalization(
406                            xml, key, value, requestedLanguageId, defaultLanguageId, cdata,
407                            localized);
408            }
409    
410            public void setLocalization(Localization localization) {
411                    PortalRuntimePermission.checkSetBeanProperty(getClass());
412    
413                    _localization = localization;
414            }
415    
416            private static Localization _localization;
417    
418    }