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            public static Locale getDefaultImportLocale(
059                    String className, long classPK, Locale contentDefaultLocale,
060                    Locale[] contentAvailableLocales) {
061    
062                    return getLocalization().getDefaultImportLocale(
063                            className, classPK, contentDefaultLocale, contentAvailableLocales);
064            }
065    
066            public static String getDefaultLanguageId(Document document) {
067                    return getLocalization().getDefaultLanguageId(document);
068            }
069    
070            public static String getDefaultLanguageId(
071                    Document document, Locale defaultLocale) {
072    
073                    return getLocalization().getDefaultLanguageId(document, defaultLocale);
074            }
075    
076            public static String getDefaultLanguageId(String xml) {
077                    return getLocalization().getDefaultLanguageId(xml);
078            }
079    
080            public static String getDefaultLanguageId(
081                    String xml, Locale defaultLocale) {
082    
083                    return getLocalization().getDefaultLanguageId(xml, defaultLocale);
084            }
085    
086            public static Localization getLocalization() {
087                    PortalRuntimePermission.checkGetBeanProperty(LocalizationUtil.class);
088    
089                    return _localization;
090            }
091    
092            public static String getLocalization(
093                    String xml, String requestedLanguageId) {
094    
095                    return getLocalization().getLocalization(xml, requestedLanguageId);
096            }
097    
098            public static String getLocalization(
099                    String xml, String requestedLanguageId, boolean useDefault) {
100    
101                    return getLocalization().getLocalization(
102                            xml, requestedLanguageId, useDefault);
103            }
104    
105            public static String getLocalization(
106                    String xml, String requestedLanguageId, boolean useDefault,
107                    String defaultValue) {
108    
109                    return getLocalization().getLocalization(
110                            xml, requestedLanguageId, useDefault, defaultValue);
111            }
112    
113            public static Map<Locale, String> getLocalizationMap(
114                    Collection<Locale> locales, Locale defaultLocale, String key) {
115    
116                    return getLocalization().getLocalizationMap(
117                            locales, defaultLocale, key);
118            }
119    
120            public static Map<Locale, String> getLocalizationMap(
121                    HttpServletRequest request, String parameter) {
122    
123                    return getLocalization().getLocalizationMap(request, parameter);
124            }
125    
126            public static Map<Locale, String> getLocalizationMap(
127                    PortletPreferences preferences, String preferenceName) {
128    
129                    return getLocalization().getLocalizationMap(
130                            preferences, preferenceName);
131            }
132    
133            public static Map<Locale, String> getLocalizationMap(
134                    PortletPreferences preferences, String preferenceName,
135                    String propertyName) {
136    
137                    return getLocalization().getLocalizationMap(
138                            preferences, preferenceName, propertyName);
139            }
140    
141            public static Map<Locale, String> getLocalizationMap(
142                    PortletPreferences preferences, String preferenceName,
143                    String propertyName, String defaultPropertyValue,
144                    ClassLoader classLoader) {
145    
146                    return getLocalization().getLocalizationMap(
147                            preferences, preferenceName, propertyName, defaultPropertyValue,
148                            classLoader);
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            public static Map<Locale, String> getMap(
218                    LocalizedValuesMap localizedValuesMap) {
219    
220                    return getLocalization().getMap(localizedValuesMap);
221            }
222    
223            public static List<Locale> getModifiedLocales(
224                    Map<Locale, String> oldMap, Map<Locale, String> newMap) {
225    
226                    if ((newMap == null) || newMap.isEmpty()) {
227                            return Collections.emptyList();
228                    }
229    
230                    List<Locale> modifiedLocales = new ArrayList<>();
231    
232                    for (Locale locale : LanguageUtil.getAvailableLocales()) {
233                            String oldValue = oldMap.get(locale);
234                            String newValue = newMap.get(locale);
235    
236                            if (!oldValue.equals(newValue)) {
237                                    modifiedLocales.add(locale);
238                            }
239                    }
240    
241                    return modifiedLocales;
242            }
243    
244            /**
245             * @deprecated As of 7.0.0 replaced by {@link #getLocalizedName(String,
246             *             String)}
247             */
248            @Deprecated
249            public static String getPreferencesKey(String key, String languageId) {
250                    return getLocalization().getPreferencesKey(key, languageId);
251            }
252    
253            public static String getPreferencesValue(
254                    PortletPreferences preferences, String key, String languageId) {
255    
256                    return getLocalization().getPreferencesValue(
257                            preferences, key, languageId);
258            }
259    
260            public static String getPreferencesValue(
261                    PortletPreferences preferences, String key, String languageId,
262                    boolean useDefault) {
263    
264                    return getLocalization().getPreferencesValue(
265                            preferences, key, languageId, useDefault);
266            }
267    
268            public static String[] getPreferencesValues(
269                    PortletPreferences preferences, String key, String languageId) {
270    
271                    return getLocalization().getPreferencesValues(
272                            preferences, key, languageId);
273            }
274    
275            public static String[] getPreferencesValues(
276                    PortletPreferences preferences, String key, String languageId,
277                    boolean useDefault) {
278    
279                    return getLocalization().getPreferencesValues(
280                            preferences, key, languageId, useDefault);
281            }
282    
283            public static String getSettingsValue(
284                    Settings settings, String key, String languageId) {
285    
286                    return getLocalization().getSettingsValue(settings, key, languageId);
287            }
288    
289            public static String getSettingsValue(
290                    Settings settings, String key, String languageId, boolean useDefault) {
291    
292                    return getLocalization().getSettingsValue(
293                            settings, key, languageId, useDefault);
294            }
295    
296            public static String[] getSettingsValues(
297                    Settings settings, String key, String languageId) {
298    
299                    return getLocalization().getSettingsValues(settings, key, languageId);
300            }
301    
302            public static String[] getSettingsValues(
303                    Settings settings, String key, String languageId, boolean useDefault) {
304    
305                    return getLocalization().getSettingsValues(
306                            settings, key, languageId, useDefault);
307            }
308    
309            public static String getXml(
310                    LocalizedValuesMap localizedValuesMap, String key) {
311    
312                    return getLocalization().getXml(localizedValuesMap, key);
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    }