[3952] | 1 | /** |
---|
| 2 | * @Author(s) - Alexndre Correia |
---|
| 3 | * Created on - 30/06/2009 |
---|
| 4 | */ |
---|
| 5 | |
---|
| 6 | package nu.fw.jeti.util; |
---|
| 7 | |
---|
| 8 | import java.nio.charset.Charset; |
---|
| 9 | import java.nio.charset.CharsetDecoder; |
---|
| 10 | import java.util.*; |
---|
| 11 | |
---|
| 12 | import javax.swing.AbstractButton; |
---|
| 13 | import javax.swing.JLabel; |
---|
| 14 | import nu.fw.jeti.languages.*; |
---|
| 15 | |
---|
| 16 | public class I18N implements languages, languagesPlugins |
---|
| 17 | { |
---|
| 18 | private static HashMap messagesLang; |
---|
| 19 | private static HashMap messagesLangPlugins; |
---|
| 20 | |
---|
| 21 | private static String getEncoding() |
---|
| 22 | { |
---|
| 23 | CharsetDecoder decoder = Charset.defaultCharset().newDecoder(); |
---|
| 24 | String charsetType = decoder.charset().toString(); |
---|
| 25 | |
---|
| 26 | return charsetType; |
---|
| 27 | } |
---|
| 28 | |
---|
| 29 | public static String getText(String type, String messageID) |
---|
| 30 | { |
---|
| 31 | return getText(messageID); |
---|
| 32 | } |
---|
| 33 | |
---|
| 34 | public static String gettext(String messageID) |
---|
| 35 | { |
---|
| 36 | return getText(messageID); |
---|
| 37 | } |
---|
| 38 | |
---|
| 39 | private static String getText( String messageID ) |
---|
| 40 | { |
---|
| 41 | if ( messageID == null ) |
---|
| 42 | return null; |
---|
| 43 | |
---|
| 44 | String translation = getTextWithAmp(messageID); |
---|
| 45 | //remove & |
---|
| 46 | int i = translation.indexOf("&"); |
---|
| 47 | |
---|
| 48 | if ( i > -1 ) |
---|
| 49 | translation = translation.substring(0, i) + translation.substring(i + 1, translation.length()); |
---|
| 50 | |
---|
| 51 | return translation; |
---|
| 52 | } |
---|
| 53 | |
---|
| 54 | public static String getTextWithAmp(String messageID) |
---|
| 55 | { |
---|
| 56 | return I18N.getText_WithAmp(messageID); |
---|
| 57 | } |
---|
| 58 | |
---|
| 59 | private static String getText_WithAmp( String messageID ) |
---|
| 60 | { |
---|
| 61 | String translation = (String)messagesLang.get(messageID); |
---|
| 62 | |
---|
| 63 | if( translation == null ) |
---|
| 64 | { |
---|
| 65 | translation = (String)messagesLangPlugins.get(messageID); |
---|
| 66 | |
---|
| 67 | if( translation == null ) |
---|
| 68 | { |
---|
| 69 | int dotPos = messageID.lastIndexOf('.'); |
---|
| 70 | |
---|
| 71 | if ( dotPos > 0 ) |
---|
| 72 | messageID = messageID.substring(dotPos+1); |
---|
| 73 | |
---|
| 74 | return messageID.replace('_',' '); |
---|
| 75 | } |
---|
| 76 | } |
---|
| 77 | |
---|
| 78 | // Encoding UTF-8/ISO-8859-1 |
---|
| 79 | try |
---|
| 80 | { |
---|
| 81 | |
---|
| 82 | translation = new String(translation.getBytes("UTF-8"), "UTF-8"); |
---|
| 83 | |
---|
| 84 | }catch(Exception e){} |
---|
| 85 | |
---|
| 86 | return translation; |
---|
| 87 | } |
---|
| 88 | |
---|
| 89 | public static void setTextAndMnemonic(String messageID,JLabel label) |
---|
| 90 | { |
---|
| 91 | setTextAndMnemonic(messageID, label, false); |
---|
| 92 | } |
---|
| 93 | |
---|
| 94 | public static void setTextAndMnemonic(String messageID, JLabel label, boolean addDots ) |
---|
| 95 | { |
---|
| 96 | String mnemonicText = I18N.getTextWithAmp(messageID); |
---|
| 97 | String translation=mnemonicText; |
---|
| 98 | int pos = mnemonicText.indexOf("&"); |
---|
| 99 | |
---|
| 100 | if (pos > -1) |
---|
| 101 | translation = mnemonicText.substring(0, pos) + mnemonicText.substring(pos + 1, mnemonicText.length()); |
---|
| 102 | |
---|
| 103 | if (addDots) |
---|
| 104 | translation += "..."; |
---|
| 105 | |
---|
| 106 | label.setText(translation); |
---|
| 107 | pos = ( (int)mnemonicText.indexOf("&") >= 0 ) ? ((int)mnemonicText.indexOf("&")) + 1 : -1; |
---|
| 108 | |
---|
| 109 | if ( pos > -1) |
---|
| 110 | label.setDisplayedMnemonic(mnemonicText.charAt(pos)); |
---|
| 111 | |
---|
| 112 | if ( pos > 0 ) |
---|
| 113 | label.setDisplayedMnemonicIndex(pos-1); |
---|
| 114 | } |
---|
| 115 | |
---|
| 116 | public static void setMnemonic(String messageID, JLabel label) |
---|
| 117 | { |
---|
| 118 | String translation = I18N.getTextWithAmp(messageID); |
---|
| 119 | int pos = ( (int)translation.indexOf("&") >= 0 ) ? ((int)translation.indexOf("&")) + 1 : -1; |
---|
| 120 | |
---|
| 121 | if ( pos > -1) |
---|
| 122 | label.setDisplayedMnemonic(translation.charAt(pos)); |
---|
| 123 | |
---|
| 124 | if ( pos > 1) |
---|
| 125 | label.setDisplayedMnemonicIndex(pos-1); |
---|
| 126 | } |
---|
| 127 | |
---|
| 128 | public static void setMnemonic(String messageID, AbstractButton button) |
---|
| 129 | { |
---|
| 130 | String translation = I18N.getTextWithAmp(messageID); |
---|
| 131 | int pos = ( (int)translation.indexOf("&") >= 0 ) ? ((int)translation.indexOf("&")) + 1 : -1; |
---|
| 132 | |
---|
| 133 | if ( pos >- 1) |
---|
| 134 | button.setMnemonic(translation.charAt(pos)); |
---|
| 135 | if ( pos > 1) |
---|
| 136 | button.setDisplayedMnemonicIndex(pos-1); |
---|
| 137 | } |
---|
| 138 | |
---|
| 139 | public static void setTextAndMnemonic(String messageID,AbstractButton button) |
---|
| 140 | { |
---|
| 141 | setTextAndMnemonic(messageID, button, false); |
---|
| 142 | } |
---|
| 143 | |
---|
| 144 | public static void setTextAndMnemonic(String messageID,AbstractButton button, boolean addDots) |
---|
| 145 | { |
---|
| 146 | String mnemonicText = getTextWithAmp(messageID); |
---|
| 147 | String translation=mnemonicText; |
---|
| 148 | |
---|
| 149 | int pos = mnemonicText.indexOf("&"); |
---|
| 150 | |
---|
| 151 | if (pos > -1) |
---|
| 152 | translation = mnemonicText.substring(0, pos) + mnemonicText.substring(pos + 1, mnemonicText.length()); |
---|
| 153 | |
---|
| 154 | if (addDots) |
---|
| 155 | translation += "..."; |
---|
| 156 | |
---|
| 157 | button.setText(translation); |
---|
| 158 | |
---|
| 159 | pos = ( (int)mnemonicText.indexOf("&") >= 0 ) ? ((int)mnemonicText.indexOf("&")) + 1 : -1; |
---|
| 160 | |
---|
| 161 | if( pos > -1) |
---|
| 162 | button.setMnemonic(mnemonicText.charAt(pos)); |
---|
| 163 | |
---|
| 164 | if( pos > 0 ) |
---|
| 165 | button.setDisplayedMnemonicIndex(pos-1); |
---|
| 166 | |
---|
| 167 | } |
---|
| 168 | |
---|
| 169 | private static void loadLanguages(Locale myLocale) |
---|
| 170 | { |
---|
| 171 | if ( myLocale.toString().equals("pt_BR")) |
---|
| 172 | { |
---|
| 173 | for( int i = 0; i < language_pt_BR.length; i++ ) |
---|
| 174 | messagesLang.put(language_pt_BR[i][0].toString(), language_pt_BR[i][1].toString()); |
---|
| 175 | |
---|
| 176 | for( int i = 0 ; i < languagePlugins_pt_BR.length; i++) |
---|
| 177 | messagesLangPlugins.put(languagePlugins_pt_BR[i][0].toString(), languagePlugins_pt_BR[i][1].toString()); |
---|
| 178 | } |
---|
| 179 | } |
---|
| 180 | |
---|
| 181 | public static void init(String myLanguage, String myCountry) |
---|
| 182 | { |
---|
| 183 | messagesLang = new HashMap(); |
---|
| 184 | messagesLangPlugins = new HashMap(); |
---|
| 185 | |
---|
| 186 | // Preferences |
---|
| 187 | Locale myLocale = null; |
---|
| 188 | |
---|
| 189 | if( !myLanguage.equals("") && !myCountry.equals("") ) |
---|
| 190 | myLocale = new Locale(myLanguage, myCountry); |
---|
| 191 | else |
---|
| 192 | myLocale = new Locale("en", "US"); |
---|
| 193 | |
---|
| 194 | I18N.loadLanguages(myLocale); |
---|
| 195 | } |
---|
| 196 | } |
---|
| 197 | |
---|
| 198 | /* |
---|
| 199 | * Overrides for emacs |
---|
| 200 | * Local variables: |
---|
| 201 | * tab-width: 4 |
---|
| 202 | * End: |
---|
| 203 | */ |
---|