Localization issue
Localization issue
- Subject: Localization issue
- From: Gilles MATHURIN <email@hidden>
- Date: Fri, 4 Aug 2006 12:10:22 -0400
Hi all,
Hope you've got a great summer, as for me it's always summer in the
caribbeans.
I 've got an issue with localizating an testApp. I am using the code
in the Chuck Hill and Sacha Book, and i get an IllegalState Exception
as a result.
I tried to implement their code in my Session.java file (i thought it
was going there, but maybe i completly misunderstood th chapter).
Hope Someone could help me …
Regards.
GM
Here is my Session.java code :
//
// Session.java
// Project LocalizationApp
//
// Created by gillesmathurin on 03/08/06
//
import com.webobjects.foundation.*;
import com.webobjects.appserver.*;
import com.webobjects.eocontrol.*;
public class Session extends WOSession {
public NSDictionary countryCodeToCountryName;
public NSDictionary languageCodeToLanguageName;
public Session() {
super();
/* ** Put your per-session initialization code here ** */
String codes[] = new String[] {"FR","US","DE"};
String countries[] = new String[] {"FRANCE", "USA", "ALLEMAGNE"};
NSDictionary countryCodeToCountryName = new NSDictionary
(countries,codes);
String lcodes[] = new String[] {"fr","en","de"};
String lnames[] = new String[] {"French", "English", "German"};
NSDictionary languageCodeToLanguageName = new NSDictionary
(lnames,lcodes);
this.setLanguages(browserLanguages());
}
protected String removeTail(String aString) {
String aCleanedString = aString;
int index = aString.indexOf(";");
if (index> 0) {
aCleanedString = aString.substring(0,index);
}
return aCleanedString.trim();
}
/** Return the browser prefs in a array */
protected NSArray browserLanguagePreferences(String aBrowserString) {
NSMutableArray aList = new NSMutableArray();
if (aBrowserString != null) {
int index = 0;
String aBrowserCode = "";
String remainderString = aBrowserString;
while (remainderString.length() > 0) {
index = remainderString.indexOf(",",index);
if (index>0) {
aBrowserCode = remainderString.substring(0,index);
aList.addObject(removeTail(aBrowserCode));
remainderString = remainderString.substring(index + 1);
}
else {
aBrowserCode = remainderString;
aList.addObject(removeTail(aBrowserCode));
remainderString ="";
}
}
} else { // Use the default
aList.addObject("fr-FR");
}
return aList;
}
public static String countryCode(String aName) {
// the string is in the form xx-YY
String aCode ="";
if (aName.indexOf("-") > 0) {
aCode = aName.substring(aName.indexOf("-") + 1).toUpperCase();
} else {
aCode = "ZZ"; // Unable to determinde the country code
}
// The standard say that there can be another part after the
country so just trim it
if (aCode.indexOf("-") > 0) {
aCode = aCode.substring(0, aCode.indexOf("-")).toUpperCase();
}
return aCode;
}
public static String languageCode(String aName) {
// the string is in the form xx-YY
String aCode = "";
if (aName.length() > 2) {
aCode = aName.substring(0,2).toLowerCase();
} else if (aName.length() == 2) {
aCode = aName.toLowerCase();
} else {
aCode = "ZZ"; // Unable to determinde the country code
}
return aCode;
}
protected NSArray browserLanguages() {
NSMutableArray browserLanguages = new NSMutableArray();
if (context().request() != null) {
// So many different way to specify the accepted languages !
// Each adaptor has their own…
String aHeader = context().request().headerForKey("accept-language");
if ((aHeader == null) || aHeader.equals(""))
aHeader = context().request().headerForKey("Accept-Language");
if ((aHeader == null) || aHeader.equals(""))
aHeader = context().request().headerForKey("HTTP_ACCEPT_LANGUAGE");
String aBrowserCode = "";
java.util.Enumeration anEnumaration = browserLanguagePreferences
(aHeader).objectEnumerator();
while (anEnumaration.hasMoreElements()) {
aBrowserCode = (String)anEnumaration.nextElement();
String countryName = (String)countryCodeToCountryName.objectForKey
(countryCode(aBrowserCode));
String languageName = (String)
languageCodeToLanguageName.objectForKey(languageCode(aBrowserCode));
browserLanguages.addObject(languageName + " (" +countryName + ")");
}
}
return browserLanguages;
}
}
_______________________________________________
Do not post admin requests to the list. They will be ignored.
Webobjects-dev mailing list (email@hidden)
Help/Unsubscribe/Update your Subscription:
This email sent to email@hidden