1 package org.apache.turbine.services.localization; 2 3 18 19 import java.util.Locale ; 20 import java.util.StringTokenizer ; 21 22 import org.apache.commons.configuration.Configuration; 23 24 import org.apache.turbine.Turbine; 25 import org.apache.turbine.util.RunData; 26 27 40 public class LocaleDetector 41 { 42 50 public static Locale getLocale(RunData data) 51 { 52 String header = data.getRequest().getHeader("Accept-Language"); 53 if (header == null || header.length() == 0) 54 return null; 55 return getLocale(header); 56 } 57 58 65 public static Locale getLocale(String languageHeader) 66 { 67 Configuration conf = Turbine.getConfiguration(); 68 69 70 if (languageHeader == null || 72 languageHeader.trim().equals("")) 73 { 74 return new Locale ( 75 conf.getString("locale.default.language", "en"), 76 conf.getString("locale.default.country", "US")); 77 } 78 79 StringTokenizer tokenizer = new StringTokenizer (languageHeader, ","); 83 84 String language = tokenizer.nextToken(); 87 return getLocaleForLanguage(language.trim()); 90 } 92 93 99 private static Locale getLocaleForLanguage(String language) 100 { 101 Locale locale; 102 int semi, dash; 103 104 if ((semi = language.indexOf(';')) != -1) 106 { 107 language = language.substring(0, semi); 108 } 109 110 language = language.trim(); 111 112 if ((dash = language.indexOf('-')) == -1) 115 { 116 locale = new Locale (language, ""); 118 } 119 else 120 { 121 locale = new Locale (language.substring(0, dash), 122 language.substring(dash + 1)); 123 } 124 125 return locale; 126 } 127 } 128 | Popular Tags |