/[schmitzm]/trunk/src/skrueger/i8n/I8NUtil.java
ViewVC logotype

Diff of /trunk/src/skrueger/i8n/I8NUtil.java

Parent Directory Parent Directory | Revision Log Revision Log | View Patch Patch

revision 41 by alfonx, Mon Apr 6 19:54:46 2009 UTC revision 1124 by alfonx, Thu Oct 14 20:58:06 2010 UTC
# Line 1  Line 1 
1    /*******************************************************************************
2     * Copyright (c) 2009 Martin O. J. Schmitz.
3     *
4     * This file is part of the SCHMITZM library - a collection of utility
5     * classes based on Java 1.6, focusing (not only) on Java Swing
6     * and the Geotools library.
7     *
8     * The SCHMITZM project is hosted at:
9     * http://wald.intevation.org/projects/schmitzm/
10     *
11     * This program is free software; you can redistribute it and/or
12     * modify it under the terms of the GNU Lesser General Public License
13     * as published by the Free Software Foundation; either version 3
14     * of the License, or (at your option) any later version.
15     *
16     * This program is distributed in the hope that it will be useful,
17     * but WITHOUT ANY WARRANTY; without even the implied warranty of
18     * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
19     * GNU General Public License for more details.
20     *
21     * You should have received a copy of the GNU Lesser General Public License (license.txt)
22     * along with this program; if not, write to the Free Software
23     * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
24     * or try this link: http://www.gnu.org/licenses/lgpl.html
25     *
26     * Contributors:
27     *     Martin O. J. Schmitz - initial API and implementation
28     *     Stefan A. Tzeggai - additional utility classes
29     ******************************************************************************/
30  package skrueger.i8n;  package skrueger.i8n;
31    
32    import java.util.ArrayList;
33    import java.util.List;
34  import java.util.Locale;  import java.util.Locale;
35  import java.util.Set;  import java.util.Set;
36  import java.util.TreeSet;  import java.util.TreeSet;
37    
38    import org.apache.log4j.Logger;
39    
40  public class I8NUtil {  public class I8NUtil {
41            static final Logger LOGGER = Logger.getLogger(I8NUtil.class);
42    
43          private static Set<String> languageCodes = new TreeSet<String>();          private static Set<String> languageCodes = new TreeSet<String>();
44          static {          static {
45                  for (Locale locale : java.util.Locale.getAvailableLocales()) {                  for (final Locale locale : java.util.Locale.getAvailableLocales()) {
46                          getLanguageCodes().add(locale.getLanguage());                          getLanguageCodes().add(locale.getLanguage());
47                  }                  }
48                  // for (String code : java.util.Locale.getISOLanguages()) {                  // for (String code : java.util.Locale.getISOLanguages()) {
# Line 16  public class I8NUtil { Line 50  public class I8NUtil {
50                  // }                  // }
51          }          }
52    
53          //                /**
54          // /**           * Creates a {@link Translation}<br/>
55          // * @Returns an ImageIcon for a given ISO code or null.           * <p>
56          // * @param code ISO Country Code           * <li>If <code>oneLineCoded</code> is empty or null, NO TRANSLATION is set.
57          // */           * <li>If format can't be recognized, the {@link String} is interpreted as
58          // public static ImageIcon getFlagIcon(String code) {           * the translation in the <code>{@value #DEFAULT_KEY}</code> language
59          //                         *
60          // String ressourcename = "resource/flags/" + code.toUpperCase() + ".gif";           * @author Stefan Alfons Tzeggai
61          //                         */
62          // URL resourceURL = TranslationEditJPanel.class.getResource(ressourcename);          public static Translation createFromOneLine(final String oneLineCoded) {
63          //                                final Translation result = new Translation();
64          // if (resourceURL != null)                  result.fromOneLine(oneLineCoded);
65          // return new ImageIcon( resourceURL);                  return result;
66          //                        }
67          // return new ImageIcon();  
68          // }          /**
69          //                 * Returns the Translation to a String of the Format: "de{Baum}en{tree}" <br/>
70          // /**           *
71          // * @Returns an {@link ImageIcon} flag for the language setup as           *
72          // Translation language           * @author Stefan Alfons Tzeggai
73          // */           */
74          // public static ImageIcon getFlagIcon() {          public static String toOneLine(final Translation source) {
75          // return getFlagIcon( Translation.getActiveLang() );                  final StringBuffer oneLine = new StringBuffer();
76          // }                  for (final String key : source.keySet()) {
77                            oneLine.append(key + "{" + source.get(key) + "}");
78                    }
79                    return oneLine.toString();
80            }
81    
82          /**          /**
83           * @author Stefan Alfons Krüger           * @author Stefan Alfons Tzeggai
84           * @param code           * @param code
85           * @return true if the code paramter is a valid ISO Language code           * @return true if the code paramter is a valid ISO Language code
86           */           */
87          public static boolean isValidISOLangCode(String code) {          public static boolean isValidISOLangCode(final String code) {
88                  return getLanguageCodes().contains(code);                  return getLanguageCodes().contains(code);
89          }          }
90    
# Line 59  public class I8NUtil { Line 97  public class I8NUtil {
97          }          }
98    
99          /**          /**
100             * Lookup {@link Locale} where they speak the 2 letter code language.
101             *
102           * @param code           * @param code
103           *            A two-letter language code.           *            A two-letter language code.
104           * @return <code>null</code> or one (of many possible) {@link Locale} that           * @return <code>null</code> or one (of many possible) {@link Locale} that
105           *         uses this language.           *         uses this language.
106           */           */
107          public static Locale getLocaleFor(String code) {          public static List<Locale> getLocalesForLang(final String code) {
108                  for (Locale l : Locale.getAvailableLocales()) {  
109  //                      System.out.println(l.getLanguage()+" not = "+code);                  final ArrayList<Locale> locales = new ArrayList<Locale>();
110                          if (l.getLanguage().toLowerCase().equals(code.toLowerCase())) {  
111                                  return l;                  for (final Locale l : Locale.getAvailableLocales()) {
112                            if (l.getLanguage().equals(code.toLowerCase())) {
113                                    locales.add(l);
114                          }                          }
115                  }                  }
116                  return null;  
117                    return locales;
118            }
119    
120            /**
121             * Lookup first country where they speak the 2 letter code language.
122             *
123             * @param code
124             *            A two-letter language code.
125             * @return <code>null</code> or one (of many possible) {@link Locale} that
126             *         uses this language.
127             */
128            public static Locale getFirstLocaleForLang(final String code) {
129    
130                    List<Locale> locales = getLocalesForLang(code);
131    
132                    if (locales.size() > 0)
133                            return locales.get(0);
134    
135                    LOGGER.error("Can't create a Locale for code " + code
136                                    + "! Returning the system default locale to avoid NPEs.");
137    
138                    return Locale.getDefault();
139            }
140    
141            /**
142             * A convenience method that checks if the {@link Translation} object
143             * contains a translation for the active language. A {@link String}
144             * containing only spaces will return <code>false</code>.
145             *
146             * @param trans
147             *            {@link Translation} to check.
148             */
149            public static boolean isEmpty(final Translation trans) {
150                    if (trans == null)
151                            return true;
152                    return isEmpty(trans.toString());
153            }
154    
155            /**
156             * A convenience method that checks if the {@link String} returned by from a
157             * {@link Translation} object contains a "valid" translation for the active
158             * language. A {@link String} containing only spaces or equals
159             * {@link Translation}.NO_TRANSLATION will return <code>false</code>.
160             *
161             * @param transString
162             *            {@link String} to check.
163             */
164            public static boolean isEmpty(final String transString) {
165                    if (transString == null)
166                            return true;
167                    if (transString.trim().isEmpty())
168                            return true;
169                    if (transString.equals(Translation.NO_TRANSLATION))
170                            return true;
171                    return false;
172            }
173    
174            /**
175             * @return a {@link Double} between 0 and 1 representing the part of the
176             *         given {@link Translation} that has been filled.
177             * @param ac
178             *            {@link AtlasConfig} to determine the languages to expect.
179             * @param trans
180             *            The {@link Translation} to check.
181             */
182            public static double qmTranslation(final List<String> languages,
183                            final Translation trans) {
184    
185                    if (trans == null)
186                            return 0.;
187    
188                    Integer cunt = 0;
189                    for (final String l : languages) {
190                            final String t = trans.get(l);
191                            if (!isEmpty(t))
192                                    cunt++;
193                    }
194                    return cunt.doubleValue() / (double) languages.size();
195            }
196    
197            /**
198             * The German Umlaute have standard ASCII alternatives that are sometimes
199             * use. This method will replace any possible ASCII-Umlaut Representation
200             * into real Umlaute. E.g. "ae" to "ä" and "ue" to "ü". Umlaute are returned
201             * as inline-UTF8.
202             */
203            public static String mitUmlaute(final String withoutUmlaute) {
204                    String replaced = withoutUmlaute;
205    
206                    replaced = replaced.replaceAll("ue", "\u00FC");
207                    replaced = replaced.replaceAll("Ue", "\u00DC");
208    
209                    replaced = replaced.replaceAll("oe", "\u00F6");
210                    replaced = replaced.replaceAll("Oe", "\u00D6");
211    
212                    replaced = replaced.replaceAll("ae", "\u00E4");
213                    replaced = replaced.replaceAll("Ae", "\u00C4");
214                    return replaced;
215          }          }
216  }  }

Legend:
Removed from v.41  
changed lines
  Added in v.1124

[email protected]
ViewVC Help
Powered by ViewVC 1.1.26