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