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