/[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 2 by mojays, Tue Feb 24 22:43:52 2009 UTC revision 44 by alfonx, Tue Apr 14 20:23:41 2009 UTC
# Line 1  Line 1 
1  package skrueger.i8n;  package skrueger.i8n;
2    
3  import java.util.LinkedList;  import java.util.Locale;
4  import java.util.List;  import java.util.Set;
5    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          public static List<String> languageCodes = new LinkedList<String>();  
12            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                  languageCodes.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  
 //       */  
 //      public static ImageIcon getFlagIcon(String code) {  
 //                
 //              String ressourcename = "resource/flags/" + code.toUpperCase() + ".gif";  
 //                
 //              URL resourceURL = TranslationEditJPanel.class.getResource(ressourcename);  
 //                
 //              if (resourceURL != null)  
 //                      return new ImageIcon( resourceURL);  
 //                
 //              return new ImageIcon();  
 //      }  
 //        
 //      /**  
 //       * @Returns an {@link ImageIcon} flag for the language setup as Translation language  
 //       */  
 //      public static ImageIcon getFlagIcon() {  
 //              return getFlagIcon( Translation.getActiveLang() );  
 //      }  
           
           
22          /**          /**
23           * @author Stefan Alfons Krüger           * @author Stefan Alfons Krüger
24           * @param code           * @param code
25           * @return true if the code paramter is a valid ISO Language code           * @return true if the code paramter is a valid ISO Language code
26           */           */
27          public static boolean isValidISOLangCode(String code) {          public static boolean isValidISOLangCode(String code) {
28                  return languageCodes.contains(code);                  return getLanguageCodes().contains(code);
29            }
30    
31            /**
32             * @return All language codes available in
33             *         java.util.Locale.getISOLanguages() without duplicates.
34             */
35            public static Set<String> getLanguageCodes() {
36                    return languageCodes;
37            }
38    
39            /**
40             * @param code
41             *            A two-letter language code.
42             * @return <code>null</code> or one (of many possible) {@link Locale} that
43             *         uses this language.
44             */
45            public static Locale getLocaleFor(String code) {
46                    for (Locale l : Locale.getAvailableLocales()) {
47                            // System.out.println(l.getLanguage()+" not = "+code);
48                            if (l.getLanguage().toLowerCase().equals(code.toLowerCase())) {
49                                    return l;
50                            }
51                    }
52                    LOGGER.error("Can't create a Locale for code " + code
53                                    + "! Returning the system default locale to avoid NPEs.");
54                    return Locale.getDefault();
55          }          }
 }  
56    
57            /**
58             * A convenience method that checks if the {@link Translation} object
59             * contains a translation for the active language. A {@link String}
60             * containing only spaces will return <code>false</code>.
61             *
62             * @param trans
63             *            {@link Translation} to check.
64             */
65            public static boolean isEmpty(final Translation trans) {
66                    if (trans == null)
67                            return true;
68                    return isEmpty(trans.toString());
69            }
70    
71            /**
72             * A convenience method that checks if the {@link String} returned by from a
73             * {@link Translation} object contains a "valid" translation for the active
74             * language. A {@link String} containing only spaces or equals
75             * {@link Translation}.NO_TRANSLATION will return <code>false</code>.
76             *
77             * @param transString
78             *            {@link String} to check.
79             */
80            public static boolean isEmpty(final String transString) {
81                    if (transString == null)
82                            return true;
83                    if (transString.trim().isEmpty())
84                            return true;
85                    if (transString.equals(Translation.NO_TRANSLATION))
86                            return true;
87                    return false;
88            }
89    }

Legend:
Removed from v.2  
changed lines
  Added in v.44

[email protected]
ViewVC Help
Powered by ViewVC 1.1.26