/** * @Author(s) - Alexndre Correia * Created on - 30/06/2009 */ package nu.fw.jeti.util; import java.nio.charset.Charset; import java.nio.charset.CharsetDecoder; import java.util.*; import javax.swing.AbstractButton; import javax.swing.JLabel; import nu.fw.jeti.languages.*; public class I18N implements languages, languagesPlugins { private static HashMap messagesLang; private static HashMap messagesLangPlugins; private static String getEncoding() { CharsetDecoder decoder = Charset.defaultCharset().newDecoder(); String charsetType = decoder.charset().toString(); return charsetType; } public static String getText(String type, String messageID) { return getText(messageID); } public static String gettext(String messageID) { return getText(messageID); } private static String getText( String messageID ) { if ( messageID == null ) return null; String translation = getTextWithAmp(messageID); //remove & int i = translation.indexOf("&"); if ( i > -1 ) translation = translation.substring(0, i) + translation.substring(i + 1, translation.length()); return translation; } public static String getTextWithAmp(String messageID) { return I18N.getText_WithAmp(messageID); } private static String getText_WithAmp( String messageID ) { String translation = (String)messagesLang.get(messageID); if( translation == null ) { translation = (String)messagesLangPlugins.get(messageID); if( translation == null ) { int dotPos = messageID.lastIndexOf('.'); if ( dotPos > 0 ) messageID = messageID.substring(dotPos+1); return messageID.replace('_',' '); } } // Encoding UTF-8/ISO-8859-1 try { translation = new String(translation.getBytes("UTF-8"), "UTF-8"); }catch(Exception e){} return translation; } public static void setTextAndMnemonic(String messageID,JLabel label) { setTextAndMnemonic(messageID, label, false); } public static void setTextAndMnemonic(String messageID, JLabel label, boolean addDots ) { String mnemonicText = I18N.getTextWithAmp(messageID); String translation=mnemonicText; int pos = mnemonicText.indexOf("&"); if (pos > -1) translation = mnemonicText.substring(0, pos) + mnemonicText.substring(pos + 1, mnemonicText.length()); if (addDots) translation += "..."; label.setText(translation); pos = ( (int)mnemonicText.indexOf("&") >= 0 ) ? ((int)mnemonicText.indexOf("&")) + 1 : -1; if ( pos > -1) label.setDisplayedMnemonic(mnemonicText.charAt(pos)); if ( pos > 0 ) label.setDisplayedMnemonicIndex(pos-1); } public static void setMnemonic(String messageID, JLabel label) { String translation = I18N.getTextWithAmp(messageID); int pos = ( (int)translation.indexOf("&") >= 0 ) ? ((int)translation.indexOf("&")) + 1 : -1; if ( pos > -1) label.setDisplayedMnemonic(translation.charAt(pos)); if ( pos > 1) label.setDisplayedMnemonicIndex(pos-1); } public static void setMnemonic(String messageID, AbstractButton button) { String translation = I18N.getTextWithAmp(messageID); int pos = ( (int)translation.indexOf("&") >= 0 ) ? ((int)translation.indexOf("&")) + 1 : -1; if ( pos >- 1) button.setMnemonic(translation.charAt(pos)); if ( pos > 1) button.setDisplayedMnemonicIndex(pos-1); } public static void setTextAndMnemonic(String messageID,AbstractButton button) { setTextAndMnemonic(messageID, button, false); } public static void setTextAndMnemonic(String messageID,AbstractButton button, boolean addDots) { String mnemonicText = getTextWithAmp(messageID); String translation=mnemonicText; int pos = mnemonicText.indexOf("&"); if (pos > -1) translation = mnemonicText.substring(0, pos) + mnemonicText.substring(pos + 1, mnemonicText.length()); if (addDots) translation += "..."; button.setText(translation); pos = ( (int)mnemonicText.indexOf("&") >= 0 ) ? ((int)mnemonicText.indexOf("&")) + 1 : -1; if( pos > -1) button.setMnemonic(mnemonicText.charAt(pos)); if( pos > 0 ) button.setDisplayedMnemonicIndex(pos-1); } private static void loadLanguages(Locale myLocale) { if ( myLocale.toString().equals("pt_BR")) { for( int i = 0; i < language_pt_BR.length; i++ ) messagesLang.put(language_pt_BR[i][0].toString(), language_pt_BR[i][1].toString()); for( int i = 0 ; i < languagePlugins_pt_BR.length; i++) messagesLangPlugins.put(languagePlugins_pt_BR[i][0].toString(), languagePlugins_pt_BR[i][1].toString()); } } public static void init(String myLanguage, String myCountry) { messagesLang = new HashMap(); messagesLangPlugins = new HashMap(); // Preferences Locale myLocale = null; if( !myLanguage.equals("") && !myCountry.equals("") ) myLocale = new Locale(myLanguage, myCountry); else myLocale = new Locale("en", "US"); I18N.loadLanguages(myLocale); } } /* * Overrides for emacs * Local variables: * tab-width: 4 * End: */