Ignore:
Timestamp:
06/30/09 18:05:35 (15 years ago)
Author:
alexandrecorreia
Message:

Ticket #474 - Arquivos modificados para a tradução do Applet.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/jabberit_messenger/java_source/src/nu/fw/jeti/util/I18N.java

    r1014 r1064  
    11/* 
    2  * Creado el 30/06/2004 por Rodolfo Gonzalez Gonzalez <rodolfo@equinoxe.g-networks.net> 
     2 * @Author(s) - Alexndre Correia 
    33 * 
    4  * This class is a wrapper for easier i18n of Jeti. It uses the standard i18n  
    5  * mechanisms given by Java.  
     4 * This class is a wrapper for easier i18n of Jeti.  
     5 * It uses the standard i18n mechanisms given by Java.  
    66 *  
     7 * Created on - 30/06/2009 
    78 */ 
     9 
    810package nu.fw.jeti.util; 
    911 
    10 import java.io.*; 
    11 import java.io.BufferedReader; 
    12 import java.io.File; 
    13 import java.io.IOException; 
    14 import java.io.InputStreamReader; 
    15 import java.net.MalformedURLException; 
    16 import java.net.URL; 
    1712import java.util.*; 
    1813 
    1914import javax.swing.AbstractButton; 
    20 import javax.swing.JComponent; 
    2115import javax.swing.JLabel; 
    22 import javax.swing.UIManager; 
     16import nu.fw.jeti.languages.*; 
    2317 
    24 import nu.fw.jeti.backend.Start; 
    25 import nu.fw.jeti.backend.URLClassloader; 
    26  
    27 /** 
    28  * @author Rodolfo Gonzalez <rodolfo@equinoxe.g-networks.net> 
    29  */ 
    30 public class I18N 
     18public class I18N implements languages, languagesPlugins 
    3119{ 
    32         private static ResourceBundle jetiBundle; 
    33         private static Map pluginsBundle; 
    34         private Language[] languages; 
    35         private Map countries; 
    36  
    37         public I18N() 
     20        private static HashMap messagesLang; 
     21        private static HashMap messagesLangPlugins; 
     22         
     23        public static String getText(String type, String messageID) 
    3824        { 
    39                 countries = new HashMap(); 
    40                 if ( Start.applet || Start.webstart ) 
    41                 { 
    42                         try 
    43                         { 
    44                                 BufferedReader data = null; 
    45                                 InputStream is  = getClass().getResourceAsStream("/languages/list.txt"); 
    46                                 if(is==null)return;//no languages 
    47                                 data =new BufferedReader(new InputStreamReader(is)); 
    48                                 //data =new BufferedReader(new InputStreamReader(new URL(Start.programURL + "languages/list.txt").openStream())); 
    49                                 List locales = new LinkedList(); 
    50                                 while (true) 
    51                                 { 
    52                                         String file = data.readLine(); 
    53                                         if (file ==null) break;//end of stream 
    54                                         locales.add(new Locale(file)); 
    55                                 } 
    56                                 data.close(); 
    57                                 extractLanguages(locales); 
    58                                 extractCountries(); 
    59                         } 
    60                         catch (IOException ex){ex.printStackTrace();} 
    61                 } 
    62                 else 
    63                 { 
    64                         extractLanguages(searchTranslations()); 
    65                         extractCountries(); 
    66                 } 
    67         } 
    68  
    69         private List searchTranslations() 
    70         { 
    71                 String urlString = Start.path + "languages" + File.separator; 
    72                 File path = new File(urlString); 
    73                 File file[] = path.listFiles(); 
    74                 List locales = new LinkedList(); 
    75                 //locales.add(new Locale("en"));//english is available, but java always selects default locale before base locale 
    76                 if (file == null) return locales; 
    77                 for (int i = 0; i < file.length; i++) 
    78                 { 
    79                         Locale locale = getLocale(file[i].getName()); 
    80                         if (locale != null) locales.add(locale); 
    81                 } 
    82                 return locales; 
    83         } 
    84  
    85         private Locale getLocale(String filename) 
    86         { 
    87                 if (!filename.startsWith("jeti") || !filename.endsWith(".properties")) return null; 
    88                 int nlen = "jeti".length(); 
    89                 String language, country = ""; 
    90                 switch (filename.length() - nlen - 11) 
    91                 { 
    92                         case 6: 
    93                                 // E.g. name+"_en_US"+".properties" 
    94                                 if (filename.charAt(3 + nlen) != '_') return null; 
    95                                 country = filename.substring(nlen + 4, nlen + 6); 
    96                         // no break; here! 
    97                         case 3: 
    98                                 if (filename.charAt(nlen) != '_') return null; 
    99                                 language = filename.substring(nlen + 1, nlen + 3); 
    100                                 Locale locale = new Locale(language, country); 
    101                                 return locale; 
    102                 } 
    103                 return null; 
    104         } 
    105  
    106         private void extractLanguages(List availableLocales) 
    107         { 
    108                 Set lang = new HashSet(); 
    109                 for (Iterator i = availableLocales.iterator(); i.hasNext();) 
    110                 { 
    111                         Locale l = (Locale) i.next(); 
    112                         String language = l.getLanguage(); 
    113                         List langLocs = (List) countries.get(language); 
    114                         if (langLocs == null) 
    115                         { 
    116                                 langLocs = new ArrayList(); 
    117                                 countries.put(language, langLocs); 
    118                         } 
    119                         langLocs.add(new Country(l)); 
    120                         lang.add(new Language(l)); 
    121                 } 
    122                 //add default locale: 
    123                 Locale l = Locale.getDefault(); 
    124                 String language = l.getLanguage(); 
    125                 List langLocs = (List) countries.get(language); 
    126                 if (langLocs == null) 
    127                 { 
    128                         langLocs = new ArrayList(); 
    129                         countries.put(language, langLocs); 
    130                 } 
    131                 lang.add(new Language(l)); 
    132                 languages = (Language[]) lang.toArray(new Language[0]); 
    133         } 
    134  
    135         private void extractCountries() 
    136         { 
    137                 Locale[] allLocales = Locale.getAvailableLocales(); 
    138                 for (int i = 0; i < allLocales.length; i++) 
    139                 { 
    140                         String language = allLocales[i].getLanguage(); 
    141                         for (int j = 0; j < languages.length; j++) 
    142                         { 
    143                                 if (language.equals(languages[j].getLanguageCode())) 
    144                                 { 
    145                                         List langLocs = (List) countries.get(language); 
    146                                         Country country = new Country(allLocales[i]); 
    147                                         if (!langLocs.contains(country)) langLocs.add(country); 
    148                                         //System.out.println(allLocales[i].getDisplayCountry()); 
    149                                 } 
    150                         } 
    151                 } 
    152         } 
    153  
    154         /** 
    155          * @return The available languages 
    156          */ 
    157         public Language[] getLanguages() 
    158         { 
    159                 return languages; 
    160         } 
    161  
    162         /** 
    163          * @return The countries that are possible with the available languages 
    164          */ 
    165         public Map getCountries() 
    166         { 
    167                 return countries; 
    168         } 
    169  
    170          
    171         public static String getText(String type, String messageID) { 
    172                 ResourceBundle bundle = (ResourceBundle)pluginsBundle.get(type); 
    173                 if(bundle == null) bundle = jetiBundle; 
    174                 return getText(bundle,messageID); 
     25                return getText(messageID); 
    17526        } 
    17627         
    177         /** 
    178          * Gives back a translation of the input key without '&' (which is used for mnemonics) 
    179          *  
    180          * @param messageID is the English phrase used as key, following the GNU gettext style 
    181          * @param bundle is the name of the base resource bundle file 
    182          * @return result (or messageID in case of error) 
    183          */ 
    18428        public static String gettext(String messageID) 
    18529        { 
    186                 return getText(jetiBundle, messageID); 
     30                return getText(messageID); 
    18731        } 
    18832         
    189         private static String getText(ResourceBundle bundle, String messageID) 
     33        private static String getText( String messageID ) 
    19034        { 
    191                 if (messageID==null)return null; 
    192                 String translation = getTextWithAmp(bundle,messageID); 
     35                if ( messageID == null ) 
     36                        return null; 
     37                 
     38                String translation = getTextWithAmp(messageID); 
    19339                //remove & 
    19440                int i = translation.indexOf("&"); 
     
    19642                { 
    19743                        translation = translation.substring(0, i) + translation.substring(i + 1, translation.length()); 
    198                         //i = translation.indexOf("&",i); 
    19944                } 
     45 
    20046                return translation; 
    20147        } 
    20248 
    203         /** 
    204          * Gives back a translation of the input key 
    205          *  
    206          * @param messageID is the English phrase used as key, following the GNU gettext style 
    207          * @param bundle is the name of the base resource bundle file 
    208          * @return result (or messageID in case of error) 
    209          */ 
    21049        public static String getTextWithAmp(String messageID) 
    211         {//TODO change back to private when string prep is implemented 
    212                 return getTextWithAmp(jetiBundle, messageID); 
     50        {  
     51                return I18N.getText_WithAmp(messageID); 
    21352        } 
    21453         
    215         private static String getTextWithAmp(ResourceBundle bundle, String messageID) 
     54        private static String getText_WithAmp( String messageID ) 
    21655        { 
    217                 if (messageID==null)return null; 
    218                 if(bundle!=null) 
     56                String translation = (String)messagesLang.get(messageID); 
     57                 
     58                if( translation == null ) 
    21959                { 
    220                         try 
    221                         { 
    222                                 return (String) bundle.getObject(messageID); 
    223                         } catch (MissingResourceException e) 
    224                         { 
    225                                 System.out.println(messageID + " is not translated"); 
    226                         } 
     60                        int dotPos = messageID.lastIndexOf('.'); 
     61                         
     62                        if ( dotPos > 0 ) 
     63                                messageID = messageID.substring(dotPos+1); 
     64                         
     65                        return messageID.replace('_',' ');                       
    22766                } 
    228                 int dotPos = messageID.lastIndexOf('.'); 
    229                 if (dotPos>0) messageID = messageID.substring(dotPos+1); 
    230                 //return messageID.replaceAll("_"," ");  
    231                 return messageID.replace('_',' ');  
     67 
     68                return translation;  
    23269        } 
    23370 
    234         public static void setTextAndMnemonic(String messageID,JLabel label) { 
     71        public static void setTextAndMnemonic(String messageID,JLabel label) 
     72        { 
    23573        setTextAndMnemonic(messageID, label, false); 
    23674    } 
    23775 
    238         public static void setTextAndMnemonic(String messageID,JLabel label, boolean addDots) 
     76        public static void setTextAndMnemonic(String messageID, JLabel label, boolean addDots ) 
    23977        { 
    240                 String mnemonicText = getTextWithAmp(messageID); 
     78                String mnemonicText = I18N.getTextWithAmp(messageID); 
    24179                String translation=mnemonicText; 
    24280                int pos = mnemonicText.indexOf("&"); 
     81                 
    24382                if (pos > -1) 
    244                 { 
    24583                        translation = mnemonicText.substring(0, pos) + mnemonicText.substring(pos + 1, mnemonicText.length()); 
    246                 } 
    247         if (addDots) { 
     84         
     85                if (addDots) 
    24886            translation += "..."; 
    249         } 
     87                 
    25088                label.setText(translation); 
    251                 pos = getMnemonicPosition(mnemonicText); 
    252                 if(pos>-1) label.setDisplayedMnemonic(mnemonicText.charAt(pos)); 
    253                 if(pos>0) label.setDisplayedMnemonicIndex(pos-1); 
     89                pos = ( (int)mnemonicText.indexOf("&") >= 0 ) ? ((int)mnemonicText.indexOf("&")) + 1 : -1; 
     90                 
     91                if ( pos > -1) 
     92                        label.setDisplayedMnemonic(mnemonicText.charAt(pos)); 
     93                 
     94                if ( pos > 0 ) 
     95                        label.setDisplayedMnemonicIndex(pos-1); 
    25496        } 
    25597         
    25698        public static void setMnemonic(String messageID, JLabel label) 
    25799        { 
    258                 String translation = getTextWithAmp(messageID); 
    259                 int pos = getMnemonicPosition(translation); 
    260                 if(pos>-1)label.setDisplayedMnemonic(translation.charAt(pos)); 
    261                 if(pos>1)label.setDisplayedMnemonicIndex(pos-1); 
     100                String translation = I18N.getTextWithAmp(messageID); 
     101                int pos = ( (int)translation.indexOf("&") >= 0 ) ? ((int)translation.indexOf("&")) + 1 : -1; 
     102                 
     103                if ( pos > -1) 
     104                        label.setDisplayedMnemonic(translation.charAt(pos)); 
     105                 
     106                if ( pos > 1) 
     107                        label.setDisplayedMnemonicIndex(pos-1); 
    262108        } 
    263109         
    264110        public static void setMnemonic(String messageID, AbstractButton button) 
    265111        { 
    266                 String translation = getTextWithAmp(messageID); 
    267                 int pos = getMnemonicPosition(translation); 
    268                 if(pos>-1)button.setMnemonic(translation.charAt(pos)); 
    269                 if(pos>1)button.setDisplayedMnemonicIndex(pos-1); 
     112                String translation = I18N.getTextWithAmp(messageID); 
     113                int pos = ( (int)translation.indexOf("&") >= 0 ) ? ((int)translation.indexOf("&")) + 1 : -1; 
     114                 
     115                if ( pos >- 1)  
     116                        button.setMnemonic(translation.charAt(pos)); 
     117                if ( pos > 1) 
     118                        button.setDisplayedMnemonicIndex(pos-1); 
    270119        } 
    271120 
    272         /** 
    273          * @param messageID is the English phrase used as key, following the 
    274      *              GNU gettext style 
    275          * @param bundle is the name of the base resource bundle file 
    276          * @param button The button to set the mnemonic on 
    277          */ 
    278121        public static void setTextAndMnemonic(String messageID,AbstractButton button) 
    279122        { 
     
    281124    } 
    282125 
    283         /** 
    284          * @param messageID is the English phrase used as key, following the 
    285      *              GNU gettext style 
    286          * @param bundle is the name of the base resource bundle file 
    287          * @param button The button to set the mnemonic on 
    288          * @param addDots If true add '...' to the translated text 
    289          */ 
    290         public static void setTextAndMnemonic(String messageID,AbstractButton button, 
    291                                           boolean addDots) 
     126        public static void setTextAndMnemonic(String messageID,AbstractButton button, boolean addDots) 
    292127        { 
    293128                String mnemonicText = getTextWithAmp(messageID); 
     129                String translation=mnemonicText; 
    294130                 
    295                 String translation=mnemonicText; 
    296131                int pos = mnemonicText.indexOf("&"); 
     132                 
    297133                if (pos > -1) 
    298                 { 
    299134                        translation = mnemonicText.substring(0, pos) + mnemonicText.substring(pos + 1, mnemonicText.length()); 
    300                 } 
    301         if (addDots) { 
     135         
     136                if (addDots) 
    302137            translation += "..."; 
    303         } 
    304                 button.setText(translation); 
    305                 pos = getMnemonicPosition(mnemonicText); 
    306                 if(pos>-1)button.setMnemonic(mnemonicText.charAt(pos)); 
    307                 if(pos>0) button.setDisplayedMnemonicIndex(pos-1); 
     138                 
     139        button.setText(translation); 
     140         
     141                pos = ( (int)mnemonicText.indexOf("&") >= 0 ) ? ((int)mnemonicText.indexOf("&")) + 1 : -1; 
     142 
     143                if( pos > -1) 
     144                        button.setMnemonic(mnemonicText.charAt(pos)); 
     145 
     146                if( pos > 0 ) 
     147                        button.setDisplayedMnemonicIndex(pos-1); 
    308148                 
    309149        } 
    310150 
    311         private static int getMnemonicPosition(String translation) 
     151        private static void loadLanguages(Locale myLocale) 
    312152        { 
    313                 int index = translation.indexOf("&"); 
    314                 if (index >= 0) return index+1; 
    315                 return -1; 
     153                if ( myLocale.toString().equals("pt_BR")) 
     154                { 
     155                        for( int i = 0; i < language_pt_BR.length; i++ ) 
     156                                messagesLang.put(language_pt_BR[i][0].toString(), language_pt_BR[i][1].toString()); 
     157                         
     158                        for( int i = 0 ; i < languagePlugins_pt_BR.length; i++) 
     159                                messagesLangPlugins.put(languagePlugins_pt_BR[i][0].toString(), languagePlugins_pt_BR[i][1].toString()); 
     160                } 
    316161        } 
     162         
     163        public static void init(String myLanguage, String myCountry) 
     164        { 
     165                messagesLang = new HashMap(); 
     166                messagesLangPlugins = new HashMap(); 
    317167 
    318         static public void init() 
    319         { 
    320                 ClassLoader classLoader=null; 
    321         if (!Start.applet && !Start.webstart) { 
    322             try { 
    323                                 URL url = new URL(Start.programURL, "languages/"); 
    324                 classLoader = new URLClassloader( 
    325                     new URL[] { url}, I18N.class.getClassLoader()); 
    326             } catch (MalformedURLException e) { 
    327                                 e.printStackTrace(); 
    328                         } 
    329                 } else classLoader = I18N.class.getClassLoader(); 
    330                 //               see if the user has some preferences 
    331                 String myLanguage = Preferences.getString("jeti", "language", ""); 
    332                 String myCountry = Preferences.getString("jeti", "country", ""); 
    333                 Locale myLocale=null; 
    334         if (myLanguage != "") { 
    335             myLocale = new Locale(myLanguage, myCountry); 
    336         } else { 
    337             myLocale = Locale.getDefault(); 
    338         } 
    339         jetiBundle = loadLanguage("jeti",classLoader,myLocale); 
    340         if(jetiBundle==null || jetiBundle.getLocale().getLanguage().equals("")) 
    341         { 
    342                 // Make it fall back to English, this way we always get at least 
    343                 // the english texts (which contains the menu mnemonic markings) 
    344                 myLocale= new Locale(Locale.ENGLISH.getLanguage(),myCountry); 
    345                 System.out.println("lang not found, default: " + myLocale); 
    346                 jetiBundle = loadLanguage("jeti",classLoader,myLocale); 
    347         } 
    348         pluginsBundle = new HashMap(); 
    349         pluginsBundle.put("drawing",loadLanguage("drawing",classLoader,myLocale)); 
    350         JComponent.setDefaultLocale(myLocale); 
    351                     
    352         //loadLanguage(classLoader, myLocale); 
    353                 UIManager.put("OptionPane.okButtonText", gettext("OK")); 
    354                 UIManager.put("OptionPane.cancelButtonText", gettext("Cancel")); 
     168                // Preferences 
     169                Locale myLocale   = null; 
     170 
     171                if( myLanguage != "" ) 
     172                        myLocale = new Locale(myLanguage, myCountry); 
     173                else 
     174                        myLocale = new Locale("en", "US"); 
     175 
     176                System.out.println("Valor de myLocale : " + myLocale); 
    355177                 
    356         } 
    357  
    358         private static ResourceBundle loadLanguage(String type, ClassLoader classLoader, Locale myLocale) 
    359         { 
    360                 try { 
    361             return ResourceBundle.getBundle("languages/" +type, myLocale); 
    362         } catch (MissingResourceException e) { 
    363             try { 
    364                 return ResourceBundle.getBundle(type, 
    365                                                       myLocale, classLoader); 
    366             } catch (MissingResourceException e2) { 
    367                 System.out.println(e2.getMessage()); 
    368                 return null; 
    369             } 
    370         } 
    371         } 
    372  
    373         public static class Country 
    374         { 
    375                 private String country; 
    376                 private String countryCode; 
    377  
    378                 public Country(Locale locale) 
    379                 { 
    380                         country = locale.getDisplayCountry(); 
    381                         if (country.equals("")) country = gettext("main.options.standard.Other"); 
    382                         countryCode = locale.getCountry(); 
    383                 } 
    384  
    385                 public String getCountryCode() 
    386                 { 
    387                         return countryCode; 
    388                 } 
    389  
    390                 public String toString() 
    391                 { 
    392                         return country; 
    393                 } 
    394  
    395         } 
    396  
    397         public static class Language 
    398         { 
    399                 private String language; 
    400                 private String languageCode; 
    401  
    402                 public Language(Locale locale) 
    403                 { 
    404                         language = locale.getDisplayLanguage(); 
    405                         languageCode = locale.getLanguage(); 
    406                 } 
    407  
    408                 public String getLanguageCode() 
    409                 { 
    410                         return languageCode; 
    411                 } 
    412  
    413                 public String toString() 
    414                 { 
    415                         return language; 
    416                 } 
    417                  
    418                 public boolean equals(Object obj) 
    419                 { 
    420                         return language.equals(((Language)obj).language); 
    421                 } 
    422                  
    423                 public int hashCode() 
    424                 { 
    425                         return language.hashCode(); 
    426                 } 
    427                  
    428                  
     178                I18N.loadLanguages(myLocale); 
    429179        } 
    430180} 
    431  
    432181 
    433182/* 
Note: See TracChangeset for help on using the changeset viewer.