/[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 38 by alfonx, Sun Apr 5 15:06:56 2009 UTC revision 185 by alfonx, Fri Jul 3 14:36:08 2009 UTC
# Line 1  Line 1 
1  package skrueger.i8n;  package skrueger.i8n;
2    
3    import java.util.Locale;
4  import java.util.Set;  import java.util.Set;
5  import java.util.TreeSet;  import java.util.TreeSet;
6    
7    import org.apache.log4j.Logger;
8    
9  public class I8NUtil {  public class I8NUtil {
10                    static final Logger LOGGER = Logger.getLogger(I8NUtil.class);
11    
12          private static Set<String> languageCodes = new TreeSet<String>();          private static Set<String> languageCodes = new TreeSet<String>();
13          static {          static {
14          for (String code : java.util.Locale.getISOLanguages()) {                  for (Locale locale : java.util.Locale.getAvailableLocales()) {
15                  getLanguageCodes().add(code);                          getLanguageCodes().add(locale.getLanguage());
16          }                  }
17      }                  // for (String code : java.util.Locale.getISOLanguages()) {
18  //                        // getLanguageCodes().add(code);
19  //      /**                  // }
20  //       * @Returns an ImageIcon for a given ISO code or null.          }
21  //       * @param code ISO Country Code  
22  //       */          /**
23  //      public static ImageIcon getFlagIcon(String code) {           * Creates a {@link Translation}<br/>
24  //                         * <p>
25  //              String ressourcename = "resource/flags/" + code.toUpperCase() + ".gif";           * <li>If <code>oneLineCoded</code> is empty or null, NO TRANSLATION is set.
26  //                         * <li>If format can't be recognized, the {@link String} is interpreted as
27  //              URL resourceURL = TranslationEditJPanel.class.getResource(ressourcename);           * the translation in the <code>{@value #DEFAULT_KEY}</code> language
28  //                         *
29  //              if (resourceURL != null)           * @author Stefan Alfons Krüger
30  //                      return new ImageIcon( resourceURL);           */
31  //                        public static Translation createFromOneLIne(final String oneLineCoded) {
32  //              return new ImageIcon();                  Translation result = new Translation();
33  //      }                  result.fromOneLine(oneLineCoded);
34  //                        return result;
35  //      /**          }
36  //       * @Returns an {@link ImageIcon} flag for the language setup as Translation language  
37  //       */          /**
38  //      public static ImageIcon getFlagIcon() {           * Returns the Translation to a String of the Format: "de{Baum}en{tree}" <br/>
39  //              return getFlagIcon( Translation.getActiveLang() );           *
40  //      }           *
41                     * @author Stefan Alfons Krüger
42                     */
43            public static String toOneLine(Translation source) {
44                    StringBuffer oneLine = new StringBuffer();
45                    for (String key : source.keySet()) {
46                            oneLine.append(key + "{" + source.get(key) + "}");
47                    }
48                    return oneLine.toString();
49            }
50    
51          /**          /**
52           * @author Stefan Alfons Krüger           * @author Stefan Alfons Krüger
53           * @param code           * @param code
# Line 45  public class I8NUtil { Line 57  public class I8NUtil {
57                  return getLanguageCodes().contains(code);                  return getLanguageCodes().contains(code);
58          }          }
59    
   
60          /**          /**
61           * @return All language codes available in java.util.Locale.getISOLanguages() without duplicates.           * @return All language codes available in
62             *         java.util.Locale.getISOLanguages() without duplicates.
63           */           */
64          public static Set<String> getLanguageCodes() {          public static Set<String> getLanguageCodes() {
65                  return languageCodes;                  return languageCodes;
66          }          }
 }  
67    
68            /**
69             * @param code
70             *            A two-letter language code.
71             * @return <code>null</code> or one (of many possible) {@link Locale} that
72             *         uses this language.
73             */
74            public static Locale getLocaleFor(String code) {
75                    for (Locale l : Locale.getAvailableLocales()) {
76                            if (l.getLanguage().equals(code.toLowerCase())) {
77                                    return l;
78                            }
79                            // LOGGER.debug(l.getLanguage() + " not = " + code);
80                    }
81    
82                    LOGGER.error("Can't create a Locale for code " + code
83                                    + "! Returning the system default locale to avoid NPEs.");
84    
85                    return Locale.getDefault();
86            }
87    
88            /**
89             * A convenience method that checks if the {@link Translation} object
90             * contains a translation for the active language. A {@link String}
91             * containing only spaces will return <code>false</code>.
92             *
93             * @param trans
94             *            {@link Translation} to check.
95             */
96            public static boolean isEmpty(final Translation trans) {
97                    if (trans == null)
98                            return true;
99                    return isEmpty(trans.toString());
100            }
101    
102            /**
103             * A convenience method that checks if the {@link String} returned by from a
104             * {@link Translation} object contains a "valid" translation for the active
105             * language. A {@link String} containing only spaces or equals
106             * {@link Translation}.NO_TRANSLATION will return <code>false</code>.
107             *
108             * @param transString
109             *            {@link String} to check.
110             */
111            public static boolean isEmpty(final String transString) {
112                    if (transString == null)
113                            return true;
114                    if (transString.trim().isEmpty())
115                            return true;
116                    if (transString.equals(Translation.NO_TRANSLATION))
117                            return true;
118                    return false;
119            }
120    }

Legend:
Removed from v.38  
changed lines
  Added in v.185

[email protected]
ViewVC Help
Powered by ViewVC 1.1.26