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.language;
016    
017    import com.liferay.portal.kernel.language.LanguageUtil;
018    import com.liferay.portal.kernel.language.LanguageWrapper;
019    import com.liferay.portal.kernel.language.UnicodeLanguage;
020    import com.liferay.portal.kernel.security.pacl.DoPrivileged;
021    import com.liferay.portal.kernel.util.UnicodeFormatter;
022    
023    import java.util.Locale;
024    import java.util.ResourceBundle;
025    
026    import javax.servlet.http.HttpServletRequest;
027    
028    /**
029     * Provides various translation related functionalities in unicode for language
030     * keys specified in portlet configurations and portal resource bundles.
031     *
032     * @author Brian Wing Shun Chan
033     * @see    LanguageImpl
034     */
035    @DoPrivileged
036    public class UnicodeLanguageImpl implements UnicodeLanguage {
037    
038            /**
039             * Returns the translated pattern in unicode using the current request's
040             * locale or, if the current request locale is not available, the server's
041             * default locale. If a translation for a given key does not exist, this
042             * method returns the requested key as the translation.
043             *
044             * <p>
045             * The substitute placeholder (e.g. <code>{0}</code>) is replaced with the
046             * argument, following the standard Java {@link ResourceBundle} notion of
047             * index based substitution.
048             * </p>
049             *
050             * @param  request the request used to determine the current locale
051             * @param  pattern the key to look up in the current locale's resource file.
052             *         The key follows the standard Java resource specification.
053             * @param  argument the single argument to be substituted into the pattern
054             *         and translated, if possible
055             * @return the translated pattern in unicode, with the argument substituted
056             *         in for the pattern's placeholder
057             */
058            @Override
059            public String format(
060                    HttpServletRequest request, String pattern, LanguageWrapper argument) {
061    
062                    return UnicodeFormatter.toString(
063                            LanguageUtil.format(request, pattern, argument));
064            }
065    
066            /**
067             * Returns the translated pattern in unicode using the current request's
068             * locale or, if the current request locale is not available, the server's
069             * default locale. If a translation for a given key does not exist, this
070             * method returns the requested key as the translation.
071             *
072             * <p>
073             * The substitute placeholder (e.g. <code>{0}</code>) is replaced with the
074             * argument, following the standard Java {@link ResourceBundle} notion of
075             * index based substitution.
076             * </p>
077             *
078             * @param  request the request used to determine the current locale
079             * @param  pattern the key to look up in the current locale's resource file.
080             *         The key follows the standard Java resource specification.
081             * @param  argument the single argument to be substituted into the pattern
082             *         and translated, if possible
083             * @param  translateArguments whether the argument is translated
084             * @return the translated pattern in unicode, with the argument substituted
085             *         in for the pattern's placeholder
086             */
087            @Override
088            public String format(
089                    HttpServletRequest request, String pattern, LanguageWrapper argument,
090                    boolean translateArguments) {
091    
092                    return UnicodeFormatter.toString(
093                            LanguageUtil.format(
094                                    request, pattern, argument, translateArguments));
095            }
096    
097            /**
098             * Returns the translated pattern in unicode using the current request's
099             * locale or, if the current request locale is not available, the server's
100             * default locale. If a translation for a given key does not exist, this
101             * method returns the requested key as the translation.
102             *
103             * <p>
104             * The substitute placeholders (e.g. <code>{0}</code>, <code>{1}</code>,
105             * <code>{2}</code>, etc.) are replaced with the arguments, following the
106             * standard Java {@link ResourceBundle} notion of index based substitution.
107             * </p>
108             *
109             * @param  request the request used to determine the current locale
110             * @param  pattern the key to look up in the current locale's resource file.
111             *         The key follows the standard Java resource specification.
112             * @param  arguments the arguments to be substituted into the pattern and
113             *         translated, if possible
114             * @return the translated pattern in unicode, with the arguments substituted
115             *         in for the pattern's placeholders
116             */
117            @Override
118            public String format(
119                    HttpServletRequest request, String pattern,
120                    LanguageWrapper[] arguments) {
121    
122                    return UnicodeFormatter.toString(
123                            LanguageUtil.format(request, pattern, arguments));
124            }
125    
126            /**
127             * Returns the translated pattern in unicode using the current request's
128             * locale or, if the current request locale is not available, the server's
129             * default locale. If a translation for a given key does not exist, this
130             * method returns the requested key as the translation.
131             *
132             * <p>
133             * The substitute placeholders (e.g. <code>{0}</code>, <code>{1}</code>,
134             * <code>{2}</code>, etc.) are replaced with the arguments, following the
135             * standard Java {@link ResourceBundle} notion of index based substitution.
136             * </p>
137             *
138             * @param  request the request used to determine the current locale
139             * @param  pattern the key to look up in the current locale's resource file.
140             *         The key follows the standard Java resource specification.
141             * @param  arguments the arguments to be substituted into the pattern
142             * @param  translateArguments whether the arguments are translated
143             * @return the translated pattern in unicode, with the arguments substituted
144             *         in for the pattern's placeholders
145             */
146            @Override
147            public String format(
148                    HttpServletRequest request, String pattern, LanguageWrapper[] arguments,
149                    boolean translateArguments) {
150    
151                    return UnicodeFormatter.toString(
152                            LanguageUtil.format(
153                                    request, pattern, arguments, translateArguments));
154            }
155    
156            /**
157             * Returns the translated pattern in unicode using the current request's
158             * locale or, if the current request locale is not available, the server's
159             * default locale. If a translation for a given key does not exist, this
160             * method returns the requested key as the translation.
161             *
162             * <p>
163             * The substitute placeholder (e.g. <code>{0}</code>) is replaced with the
164             * argument, following the standard Java {@link ResourceBundle} notion of
165             * index based substitution.
166             * </p>
167             *
168             * @param  request the request used to determine the current locale
169             * @param  pattern the key to look up in the current locale's resource file.
170             *         The key follows the standard Java resource specification.
171             * @param  argument the single argument to be substituted into the pattern
172             *         and translated, if possible
173             * @return the translated pattern in unicode, with the argument substituted
174             *         in for the pattern's placeholder
175             */
176            @Override
177            public String format(
178                    HttpServletRequest request, String pattern, Object argument) {
179    
180                    return UnicodeFormatter.toString(
181                            LanguageUtil.format(request, pattern, argument));
182            }
183    
184            /**
185             * Returns the translated pattern in unicode using the current request's
186             * locale or, if the current request locale is not available, the server's
187             * default locale. If a translation for a given key does not exist, this
188             * method returns the requested key as the translation.
189             *
190             * <p>
191             * The substitute placeholder (e.g. <code>{0}</code>) is replaced with the
192             * argument, following the standard Java {@link ResourceBundle} notion of
193             * index based substitution.
194             * </p>
195             *
196             * @param  request the request used to determine the current locale
197             * @param  pattern the key to look up in the current locale's resource file.
198             *         The key follows the standard Java resource specification.
199             * @param  argument the single argument to be substituted into the pattern
200             *         and translated, if possible
201             * @param  translateArguments whether the argument is translated
202             * @return the translated pattern in unicode, with the argument substituted
203             *         in for the pattern's placeholder
204             */
205            @Override
206            public String format(
207                    HttpServletRequest request, String pattern, Object argument,
208                    boolean translateArguments) {
209    
210                    return UnicodeFormatter.toString(
211                            LanguageUtil.format(
212                                    request, pattern, argument, translateArguments));
213            }
214    
215            /**
216             * Returns the translated pattern in unicode using the current request's
217             * locale or, if the current request locale is not available, the server's
218             * default locale. If a translation for a given key does not exist, this
219             * method returns the requested key as the translation.
220             *
221             * <p>
222             * The substitute placeholders (e.g. <code>{0}</code>, <code>{1}</code>,
223             * <code>{2}</code>, etc.) are replaced with the arguments, following the
224             * standard Java {@link ResourceBundle} notion of index based substitution.
225             * </p>
226             *
227             * @param  request the request used to determine the current locale
228             * @param  pattern the key to look up in the current locale's resource file.
229             *         The key follows the standard Java resource specification.
230             * @param  arguments the arguments to be substituted into the pattern and
231             *         translated, if possible
232             * @return the translated pattern in unicode, with the arguments substituted
233             *         in for the pattern's placeholders
234             */
235            @Override
236            public String format(
237                    HttpServletRequest request, String pattern, Object[] arguments) {
238    
239                    return UnicodeFormatter.toString(
240                            LanguageUtil.format(request, pattern, arguments));
241            }
242    
243            /**
244             * Returns the translated pattern in unicode using the current request's
245             * locale or, if the current request locale is not available, the server's
246             * default locale. If a translation for a given key does not exist, this
247             * method returns the requested key as the translation.
248             *
249             * <p>
250             * The substitute placeholders (e.g. <code>{0}</code>, <code>{1}</code>,
251             * <code>{2}</code>, etc.) are replaced with the arguments, following the
252             * standard Java {@link ResourceBundle} notion of index based substitution.
253             * </p>
254             *
255             * @param  request the request used to determine the current locale
256             * @param  pattern the key to look up in the current locale's resource file.
257             *         The key follows the standard Java resource specification.
258             * @param  arguments the arguments to be substituted into the pattern
259             * @param  translateArguments whether the arguments are translated
260             * @return the translated pattern in unicode, with the arguments substituted
261             *         in for the pattern's placeholders
262             */
263            @Override
264            public String format(
265                    HttpServletRequest request, String pattern, Object[] arguments,
266                    boolean translateArguments) {
267    
268                    return UnicodeFormatter.toString(
269                            LanguageUtil.format(
270                                    request, pattern, arguments, translateArguments));
271            }
272    
273            /**
274             * Returns the translated pattern in unicode using the locale or, if the
275             * locale is not available, the server's default locale. If a translation
276             * for a given key does not exist, this method returns the requested key as
277             * the translation.
278             *
279             * <p>
280             * The substitute placeholder (e.g. <code>{0}</code>) is replaced with the
281             * argument, following the standard Java {@link ResourceBundle} notion of
282             * index based substitution.
283             * </p>
284             *
285             * @param  locale the locale to translate to
286             * @param  pattern the key to look up in the current locale's resource file.
287             *         The key follows the standard Java resource specification.
288             * @param  argument the argument to be substituted into the pattern
289             * @return the translated pattern in unicode, with the argument substituted
290             *         in for the pattern's placeholder
291             */
292            @Override
293            public String format(Locale locale, String pattern, Object argument) {
294                    return UnicodeFormatter.toString(
295                            LanguageUtil.format(locale, pattern, argument));
296            }
297    
298            /**
299             * Returns the translated pattern in unicode using the locale or, if the
300             * locale is not available, the server's default locale. If a translation
301             * for a given key does not exist, this method returns the requested key as
302             * the translation.
303             *
304             * <p>
305             * The substitute placeholder (e.g. <code>{0}</code>) is replaced with the
306             * argument, following the standard Java {@link ResourceBundle} notion of
307             * index based substitution.
308             * </p>
309             *
310             * @param  locale the locale to translate to
311             * @param  pattern the key to look up in the current locale's resource file.
312             *         The key follows the standard Java resource specification.
313             * @param  argument the argument to be substituted into the pattern
314             * @param  translateArguments whether the argument is translated
315             * @return the translated pattern in unicode, with the argument substituted
316             *         in for the pattern's placeholder
317             */
318            @Override
319            public String format(
320                    Locale locale, String pattern, Object argument,
321                    boolean translateArguments) {
322    
323                    return UnicodeFormatter.toString(
324                            LanguageUtil.format(locale, pattern, argument, translateArguments));
325            }
326    
327            /**
328             * Returns the translated pattern in unicode using the locale or, if the
329             * locale is not available, the server's default locale. If a translation
330             * for a given key does not exist, this method returns the requested key as
331             * the translation.
332             *
333             * <p>
334             * The substitute placeholders (e.g. <code>{0}</code>, <code>{1}</code>,
335             * <code>{2}</code>, etc.) are replaced with the arguments, following the
336             * standard Java {@link ResourceBundle} notion of index based substitution.
337             * </p>
338             *
339             * @param  locale the locale to translate to
340             * @param  pattern the key to look up in the current locale's resource file.
341             *         The key follows the standard Java resource specification.
342             * @param  arguments the arguments to be substituted into the pattern
343             * @return the translated pattern in unicode, with the arguments substituted
344             *         in for the pattern's placeholders
345             */
346            @Override
347            public String format(Locale locale, String pattern, Object[] arguments) {
348                    return UnicodeFormatter.toString(
349                            LanguageUtil.format(locale, pattern, arguments));
350            }
351    
352            /**
353             * Returns the translated pattern in unicode using the locale or, if the
354             * locale is not available, the server's default locale. If a translation
355             * for a given key does not exist, this method returns the requested key as
356             * the translation.
357             *
358             * <p>
359             * The substitute placeholders (e.g. <code>{0}</code>, <code>{1}</code>,
360             * <code>{2}</code>, etc.) are replaced with the arguments, following the
361             * standard Java {@link ResourceBundle} notion of index based substitution.
362             * </p>
363             *
364             * @param  locale the locale to translate to
365             * @param  pattern the key to look up in the current locale's resource file.
366             *         The key follows the standard Java resource specification.
367             * @param  arguments the arguments to be substituted into the pattern
368             * @param  translateArguments whether the arguments are translated
369             * @return the translated pattern in unicode, with the arguments substituted
370             *         in for the pattern's placeholders
371             */
372            @Override
373            public String format(
374                    Locale locale, String pattern, Object[] arguments,
375                    boolean translateArguments) {
376    
377                    return UnicodeFormatter.toString(
378                            LanguageUtil.format(
379                                    locale, pattern, arguments, translateArguments));
380            }
381    
382            /**
383             * Returns the translated pattern in the resource bundle in unicode or, if
384             * the resource bundle is not available, the untranslated key in unicode. If
385             * a translation for a given key does not exist, this method returns the
386             * requested key in unicode as the translation.
387             *
388             * <p>
389             * The substitute placeholder (e.g. <code>{0}</code>) is replaced with the
390             * argument, following the standard Java {@link ResourceBundle} notion of
391             * index based substitution.
392             * </p>
393             *
394             * @param  resourceBundle the requested key's resource bundle
395             * @param  pattern the key to look up in the resource bundle. The key
396             *         follows the standard Java resource specification.
397             * @param  argument the argument to be substituted into the pattern
398             * @return the translated pattern in unicode, with the argument substituted
399             *         in for the pattern's placeholder
400             */
401            @Override
402            public String format(
403                    ResourceBundle resourceBundle, String pattern, Object argument) {
404    
405                    return UnicodeFormatter.toString(
406                            LanguageUtil.format(resourceBundle, pattern, argument));
407            }
408    
409            /**
410             * Returns the translated pattern in the resource bundle in unicode or, if
411             * the resource bundle is not available, the untranslated key in unicode. If
412             * a translation for a given key does not exist, this method returns the
413             * requested key in unicode as the translation.
414             *
415             * <p>
416             * The substitute placeholder (e.g. <code>{0}</code>) is replaced with the
417             * argument, following the standard Java {@link ResourceBundle} notion of
418             * index based substitution.
419             * </p>
420             *
421             * @param  resourceBundle the requested key's resource bundle
422             * @param  pattern the key to look up in the resource bundle. The key
423             *         follows the standard Java resource specification.
424             * @param  argument the argument to be substituted into the pattern
425             * @param  translateArguments whether the argument is translated
426             * @return the translated pattern in unicode, with the argument substituted
427             *         in for the pattern's placeholder
428             */
429            @Override
430            public String format(
431                    ResourceBundle resourceBundle, String pattern, Object argument,
432                    boolean translateArguments) {
433    
434                    return UnicodeFormatter.toString(
435                            LanguageUtil.format(
436                                    resourceBundle, pattern, argument, translateArguments));
437            }
438    
439            /**
440             * Returns the translated pattern in the resource bundle in unicode or, if
441             * the resource bundle is not available, the untranslated key in unicode. If
442             * a translation for a given key does not exist, this method returns the
443             * requested key in unicode as the translation.
444             *
445             * <p>
446             * The substitute placeholders (e.g. <code>{0}</code>, <code>{1}</code>,
447             * <code>{2}</code>, etc.) are replaced with the arguments, following the
448             * standard Java {@link ResourceBundle} notion of index based substitution.
449             * </p>
450             *
451             * @param  resourceBundle the requested key's resource bundle
452             * @param  pattern the key to look up in the resource bundle. The key
453             *         follows the standard Java resource specification.
454             * @param  arguments the arguments to be substituted into the pattern
455             * @return the translated pattern in unicode, with the arguments substituted
456             *         in for the pattern's placeholder
457             */
458            @Override
459            public String format(
460                    ResourceBundle resourceBundle, String pattern, Object[] arguments) {
461    
462                    return UnicodeFormatter.toString(
463                            LanguageUtil.format(resourceBundle, pattern, arguments));
464            }
465    
466            /**
467             * Returns the translated pattern in the resource bundle in unicode or, if
468             * the resource bundle is not available, the untranslated key in unicode. If
469             * a translation for a given key does not exist, this method returns the
470             * requested key in unicode as the translation.
471             *
472             * <p>
473             * The substitute placeholders (e.g. <code>{0}</code>, <code>{1}</code>,
474             * <code>{2}</code>, etc.) are replaced with the arguments, following the
475             * standard Java {@link ResourceBundle} notion of index based substitution.
476             * </p>
477             *
478             * @param  resourceBundle the requested key's resource bundle
479             * @param  pattern the key to look up in the resource bundle. The key
480             *         follows the standard Java resource specification.
481             * @param  arguments the arguments to be substituted into the pattern
482             * @param  translateArguments whether the arguments are translated
483             * @return the translated pattern in unicode, with the arguments substituted
484             *         in for the pattern's placeholder
485             */
486            @Override
487            public String format(
488                    ResourceBundle resourceBundle, String pattern, Object[] arguments,
489                    boolean translateArguments) {
490    
491                    return UnicodeFormatter.toString(
492                            LanguageUtil.format(
493                                    resourceBundle, pattern, arguments, translateArguments));
494            }
495    
496            /**
497             * Returns the key's translation from the portlet configuration in unicode,
498             * or from the portal's resource bundle if the portlet configuration is
499             * unavailable.
500             *
501             * @param  request the request used to determine the key's context and
502             *         locale
503             * @param  key the translation key
504             * @return the key's translation in unicode, or the unicode key if the
505             *         translation is unavailable
506             */
507            @Override
508            public String get(HttpServletRequest request, String key) {
509                    return UnicodeFormatter.toString(LanguageUtil.get(request, key));
510            }
511    
512            /**
513             * Returns the key's translation from the portlet configuration in unicode,
514             * or from the portal's resource bundle if the portlet configuration is
515             * unavailable.
516             *
517             * @param  request the request used to determine the key's context and
518             *         locale
519             * @param  key the translation key
520             * @param  defaultValue the value to return if there is no matching
521             *         translation
522             * @return the key's translation in unicode, or the default value in unicode
523             *         if the translation is unavailable
524             */
525            @Override
526            public String get(
527                    HttpServletRequest request, String key, String defaultValue) {
528    
529                    return UnicodeFormatter.toString(
530                            LanguageUtil.get(request, key, defaultValue));
531            }
532    
533            /**
534             * Returns the key's translation from the portal's resource bundle in
535             * unicode.
536             *
537             * @param  locale the key's locale
538             * @param  key the translation key
539             * @return the key's translation in unicode
540             */
541            @Override
542            public String get(Locale locale, String key) {
543                    return UnicodeFormatter.toString(LanguageUtil.get(locale, key));
544            }
545    
546            /**
547             * Returns the key's translation from the portal's resource bundle in
548             * unicode.
549             *
550             * @param  locale the key's locale
551             * @param  key the translation key
552             * @param  defaultValue the value to return if there is no matching
553             *         translation
554             * @return the key's translation in unicode, or the default value in unicode
555             *         if the translation is unavailable
556             */
557            @Override
558            public String get(Locale locale, String key, String defaultValue) {
559                    return UnicodeFormatter.toString(
560                            LanguageUtil.get(locale, key, defaultValue));
561            }
562    
563            /**
564             * Returns the key's translation from the resource bundle in unicode.
565             *
566             * @param  resourceBundle the requested key's resource bundle
567             * @param  key the translation key
568             * @return the key's translation in unicode
569             */
570            @Override
571            public String get(ResourceBundle resourceBundle, String key) {
572                    return UnicodeFormatter.toString(LanguageUtil.get(resourceBundle, key));
573            }
574    
575            /**
576             * Returns the key's translation from the resource bundle in unicode.
577             *
578             * @param  resourceBundle the requested key's resource bundle
579             * @param  key the translation key
580             * @param  defaultValue the value to return if there is no matching
581             *         translation
582             * @return the key's translation in unicode, or the default value in unicode
583             *         if the translation is unavailable
584             */
585            @Override
586            public String get(
587                    ResourceBundle resourceBundle, String key, String defaultValue) {
588    
589                    return UnicodeFormatter.toString(
590                            LanguageUtil.get(resourceBundle, key, defaultValue));
591            }
592    
593            /**
594             * Returns an exact localized description in unicode of the time interval
595             * (in milliseconds) in the largest unit possible.
596             *
597             * <p>
598             * For example, the following time intervals would be converted to the
599             * following time descriptions, using the English locale:
600             * </p>
601             *
602             * <ul>
603             * <li>
604             * 1000 = 1 Second
605             * </li>
606             * <li>
607             * 1001 = 1001 Milliseconds
608             * </li>
609             * <li>
610             * 86400000 = 1 Day
611             * </li>
612             * <li>
613             * 86401000 = 86401 Seconds
614             * </li>
615             * </ul>
616             *
617             * @param  request the request used to determine the current locale
618             * @param  milliseconds the time interval in milliseconds to describe
619             * @return an exact localized description in unicode of the time interval in
620             *         the largest unit possible
621             */
622            @Override
623            public String getTimeDescription(
624                    HttpServletRequest request, long milliseconds) {
625    
626                    return UnicodeFormatter.toString(
627                            LanguageUtil.getTimeDescription(request, milliseconds));
628            }
629    
630            /**
631             * Returns an exact localized description in unicode of the time interval
632             * (in milliseconds) in the largest unit possible.
633             *
634             * <p>
635             * For example, the following time intervals would be converted to the
636             * following time descriptions, using the English locale:
637             * </p>
638             *
639             * <ul>
640             * <li>
641             * 1000 = 1 Second
642             * </li>
643             * <li>
644             * 1001 = 1001 Milliseconds
645             * </li>
646             * <li>
647             * 86400000 = 1 Day
648             * </li>
649             * <li>
650             * 86401000 = 86401 Seconds
651             * </li>
652             * </ul>
653             *
654             * @param  request the request used to determine the current locale
655             * @param  milliseconds the time interval in milliseconds to describe
656             * @return an exact localized description in unicode of the time interval in
657             *         the largest unit possible
658             */
659            @Override
660            public String getTimeDescription(
661                    HttpServletRequest request, Long milliseconds) {
662    
663                    return UnicodeFormatter.toString(
664                            LanguageUtil.getTimeDescription(request, milliseconds));
665            }
666    
667    }