struts-dev mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From "Hou Yunfeng" <>
Subject locale support improvement
Date Sat, 07 Oct 2000 08:34:38 GMT
I found Struts is very useful for me, but, still I found some inconvenience related to the
locale management. I must create several resouce message files for all the locales I would
like to serve, it is OK for me, but when it comes that I must seperate each small pieces of
all the pages into key/value pairs, it's a huge work. I decided to use another solution: for
each locale of each forwarding, there will be a seperate jsp file.

So I made some modification to the code:
1. ActionForward: I add a field named locales, which will hold all the supported locales of
the forwading, as well as its setter and getter. And another getPath method which will have
Locale as it's parameter.

 protected String locales = null;
 private Hashtable pathes = null;
 public String getPath() {

 return (this.path);


  * Return the path accoring to the request locale.
 public String getPath(Locale locale) {
  String tmpPath =  (String) pathes.get(locale.toString());
  if (tmpPath == null)
   tmpPath = this.path;
  return (tmpPath);

  * Set the locales.
  * @param locales The new path
 public void setLocales(String locales) {

 this.locales = locales;


  * Return the locales.
 public String getLocales() {

 return (this.locales);


  * Set the path.
  * @param path The new path
 public void setPath(String path) {

 this.path = path;

  * Set the pathes for all the locales supported.
 private void setPathes() {
 if ((this.path == null) || (this.locales == null))

 int pointPos = this.path.lastIndexOf(".");
 String tmpPath = this.path.substring(0, pointPos);
 String tmpName = this.path.substring(pointPos);
 String localeName;
 StringTokenizer st = new StringTokenizer(locales, ",;");
 while (st.hasMoreTokens()) {
  localeName = st.nextToken();
  if (localeName.equals(Locale.getDefault().toString()))
   pathes.put(localeName, this.path);
   pathes.put(localeName, tmpPath + "_" + localeName + tmpName);

2. ActionServlet: only need to replace the getPath() to getPath(Locale) as follows in the
method ProcessActionInstance.
     String path = forward.getPath(request.getLocale());

3. action.xml to add locales property to it. for example,
  <action    path="/logon"
    <forward name="success"    path="/mainMenu.jsp" locales="zh_CN;en_US"/>

 4. Now, you can create mainMenu_en_US.jsp for English, mainMenu_zh_CN.jsp for Chinese, well
mainMenu.jsp for the default locale.

Pls let me know if you need complete source file.

BTW, I am new to the open source area and would like to know how to get deeper involved. Thanks.

Hou Yunfeng
View raw message