1 50 package org.apache.avalon.fortress.examples.components; 51 52 import java.util.HashMap ; 53 import java.util.Map ; 54 import java.util.Set ; 55 import org.apache.avalon.framework.configuration.Configuration; 56 import org.apache.avalon.framework.configuration.ConfigurationException; 57 import org.apache.avalon.framework.configuration.Configurable; 58 import org.apache.excalibur.instrument.AbstractLogEnabledInstrumentable; 59 import org.apache.excalibur.instrument.CounterInstrument; 60 61 87 public class TranslatorImpl extends AbstractLogEnabledInstrumentable 88 implements Translator, Configurable 89 { 90 private CounterInstrument m_translationsInstrument; 92 93 private Map m_keys = new java.util.HashMap (); 95 96 99 public TranslatorImpl() 100 { 101 addInstrument( m_translationsInstrument = new CounterInstrument( "translations" ) ); 102 } 103 104 112 public void configure( Configuration config ) 113 throws ConfigurationException 114 { 115 if( config != null ) 116 { 117 Configuration[] entries = 118 config.getChild( "dictionary" ).getChildren( "translation" ); 119 120 for( int i = 0; i < entries.length; ++i ) 121 { 122 String key = entries[ i ].getAttribute( "key" ); 123 Configuration[] values = entries[ i ].getChildren( "value" ); 124 125 Map translations = new HashMap (); 126 127 for( int j = 0; j < values.length; ++j ) 128 { 129 translations.put( 130 values[ j ].getAttribute( "language" ), 131 values[ j ].getValue() 132 ); 133 } 134 135 m_keys.put( key, translations ); 136 } 137 138 if( getLogger().isDebugEnabled() ) 139 { 140 getLogger().debug( 141 "Translator configured with " + m_keys.size() + " translations" 142 ); 143 } 144 } 145 146 else 147 { 148 if( getLogger().isWarnEnabled() ) 149 { 150 getLogger().warn( "No configuration specified" ); 151 } 152 } 153 } 154 155 164 public String [] getSupportedLanguages( String key ) 165 { 166 Map translations = (Map )m_keys.get( key ); 167 Set keys = translations.keySet(); 168 return (String [])keys.toArray( new String []{} ); 169 } 170 171 180 public String getTranslation( String key, String language ) 181 { 182 m_translationsInstrument.increment(); 184 185 Map translationMap = (Map )m_keys.get( key ); 186 return (String )translationMap.get( language ); 187 } 188 } 189 190 | Popular Tags |