1 28 29 30 package com.caucho.widget; 31 32 import com.caucho.util.L10N; 33 34 import java.util.HashMap ; 35 import java.util.Locale ; 36 import java.util.Map ; 37 import java.util.logging.Logger ; 38 39 42 public class LocaleString 43 { 44 private static L10N L = new L10N( LocaleString.class ); 45 46 static protected final Logger log = 47 Logger.getLogger( LocaleString.class.getName() ); 48 49 private static ThreadLocal _threadLocale = new ThreadLocal <Locale >(); 50 51 private Locale _locale; 52 53 private String _text = null; 55 private String _anyText = null; 57 private HashMap <Locale ,String > _textMap; 59 60 64 public static void setThreadLocale( Locale locale ) 65 { 66 _threadLocale.set( locale ); 67 } 68 69 public static LocaleString add( LocaleString localeString, 70 LocaleString value ) 71 { 72 if ( localeString == null ) 73 localeString = new LocaleString(); 74 75 localeString.add( value ); 76 77 return localeString; 78 } 79 80 public static LocaleString add( LocaleString localeString, 81 String value ) 82 { 83 if ( localeString == null ) 84 localeString = new LocaleString(); 85 86 localeString.addText( value ); 87 88 return localeString; 89 } 90 91 public static LocaleString add( LocaleString localeString, 92 Locale locale, String value ) 93 { 94 if ( localeString == null ) 95 localeString = new LocaleString(); 96 97 localeString.put( locale, value ); 98 99 return localeString; 100 } 101 102 public static String get( LocaleString localeString, Locale locale ) 103 { 104 return localeString == null ? null : localeString.get( locale ); 105 } 106 107 public static String get( LocaleString localeString ) 108 { 109 return localeString == null ? null : localeString.getValue(); 110 } 111 112 public LocaleString() 113 { 114 setLocale( null ); 115 } 116 117 120 public LocaleString( Locale locale ) 121 { 122 setLocale( locale ); 123 } 124 125 128 public LocaleString( String text ) 129 { 130 setLocale( null ); 131 addText( text ); 132 } 133 134 137 public LocaleString( Locale locale, String text ) 138 { 139 setLocale( locale ); 140 addText( text ); 141 } 142 143 147 public void setLocale( Locale locale ) 148 { 149 if ( locale == null ) { 150 String lang = System.getProperty( "user.language" ); 151 String country = System.getProperty( "user.region" ); 152 153 if ( lang != null && country != null ) 154 locale = new Locale ( lang, country ); 155 else if ( lang != null ) 156 locale = new Locale ( lang ); 157 else 158 locale = Locale.ENGLISH; 159 } 160 161 _locale = locale; 162 } 163 164 public Locale getLocale() 165 { 166 return _locale; 167 } 168 169 173 public void setLang( String lang ) 174 { 175 int index = lang.indexOf('-'); 176 177 if (index > -1 ) { 178 String language = lang.substring( 0, index ); 179 String country = lang.substring( index + 1 ); 180 setLocale( new Locale ( language, country ) ); 181 } 182 else 183 setLocale( new Locale ( lang ) ); 184 } 185 186 public void addText( String text ) 187 { 188 _text = text; 189 } 190 191 public void put( Locale locale, String text ) 192 { 193 if ( locale == null ) 194 _text = text; 195 else { 196 if ( locale.equals( _locale ) ) 197 _text = text; 198 else { 199 if ( _textMap == null ) { 200 _textMap = new HashMap <Locale ,String >(); 201 _anyText = text; 202 } 203 204 _textMap.put( locale, text ); 205 } 206 } 207 } 208 209 219 public String getValue() 220 { 221 Locale locale = (Locale ) _threadLocale.get(); 222 223 return get( locale ); 224 } 225 226 236 public String get( Locale locale ) 237 { 238 if ( locale == null ) { 239 if ( _text != null ) 240 return _text; 241 else if ( _anyText != null ) 242 return _anyText; 243 else 244 return null; 245 } 246 247 String text = null; 248 249 if ( _text != null && locale.equals( _locale ) ) 250 text = _text; 251 else if ( _textMap != null ) 252 text = _textMap.get( locale ); 253 254 if ( text != null ) 255 return text; 256 257 Locale reducedLocale = reduceLocale( locale ); 258 259 return get( reducedLocale ); 260 } 261 262 267 public void add( LocaleString localeString ) 268 { 269 if ( localeString._text != null ) 270 put( localeString._locale, localeString._text ); 271 272 if ( localeString._textMap != null ) { 273 for ( Map.Entry <Locale ,String > entry : localeString._textMap.entrySet() ) 274 { 275 put( entry.getKey(), entry.getValue() ); 276 } 277 } 278 } 279 280 private static Locale reduceLocale( Locale locale ) 281 { 282 String language = locale.getLanguage(); 283 String country = locale.getCountry(); 284 String variant = locale.getVariant(); 285 286 if ( variant.length() > 0 ) 287 return new Locale ( language, country ); 288 else if ( country.length() > 0 ) 289 return new Locale ( language ); 290 else 291 return null; 292 } 293 294 public String toString() 295 { 296 return getValue(); 297 } 298 } 299 | Popular Tags |