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.portlet.translator.util;
016    
017    import com.liferay.portal.kernel.language.LanguageUtil;
018    import com.liferay.portal.kernel.util.PrefsPropsUtil;
019    import com.liferay.portal.kernel.util.PropsKeys;
020    import com.liferay.portal.kernel.util.StringPool;
021    import com.liferay.portal.kernel.webcache.WebCacheException;
022    import com.liferay.portal.kernel.webcache.WebCacheItem;
023    import com.liferay.portlet.translator.model.Translation;
024    
025    import java.util.Comparator;
026    import java.util.HashMap;
027    import java.util.Locale;
028    import java.util.Map;
029    import java.util.TreeMap;
030    
031    /**
032     * @author Brian Wing Shun Chan
033     * @author Hugo Huijser
034     */
035    public class TranslatorUtil {
036    
037            public static String[] getFromAndToLanguageIds(
038                    String translationId, Map<String, String> languageIdsMap) {
039    
040                    try {
041                            int pos = translationId.indexOf(StringPool.UNDERLINE);
042    
043                            String fromLanguageId = translationId.substring(0, pos);
044    
045                            if (!languageIdsMap.containsKey(fromLanguageId)) {
046                                    pos = translationId.indexOf(StringPool.UNDERLINE, pos + 1);
047    
048                                    fromLanguageId = translationId.substring(0, pos);
049    
050                                    if (!languageIdsMap.containsKey(fromLanguageId)) {
051                                            return null;
052                                    }
053                            }
054    
055                            String toLanguageId = translationId.substring(pos + 1);
056    
057                            if (!languageIdsMap.containsKey(fromLanguageId)) {
058                                    return null;
059                            }
060    
061                            return new String[] {fromLanguageId, toLanguageId};
062                    }
063                    catch (Exception e) {
064                    }
065    
066                    return null;
067            }
068    
069            public static Map<String, String> getLanguageIdsMap(Locale locale) {
070                    Map<String, String> languageIdsMap = new HashMap<String, String>();
071    
072                    String[] languageIds = PrefsPropsUtil.getStringArray(
073                            PropsKeys.TRANSLATOR_LANGUAGES, StringPool.COMMA);
074    
075                    for (String languageId : languageIds) {
076                            languageIdsMap.put(
077                                    languageId, LanguageUtil.get(locale, "language." + languageId));
078                    }
079    
080                    Map<String, String> sortedLanguageIdsMap = new TreeMap<String, String>(
081                            new ValueComparator(languageIdsMap));
082    
083                    sortedLanguageIdsMap.putAll(languageIdsMap);
084    
085                    return sortedLanguageIdsMap;
086            }
087    
088            public static Translation getTranslation(
089                            String fromLanguageId, String toLanguageId, String fromText)
090                    throws WebCacheException {
091    
092                    WebCacheItem wci = new TranslationWebCacheItem(
093                            fromLanguageId, toLanguageId, fromText);
094    
095                    return (Translation)wci.convert("");
096            }
097    
098            private static class ValueComparator implements Comparator<Object> {
099    
100                    public ValueComparator(Map<String, String> map) {
101                            _map = map;
102                    }
103    
104                    @Override
105                    public int compare(Object obj1, Object obj2) {
106                            String value1 = _map.get(obj1);
107                            String value2 = _map.get(obj2);
108    
109                            return value1.compareTo(value2);
110                    }
111    
112                    private final Map<String, String> _map;
113    
114            }
115    
116    }