You are viewing a plain text version of this content. The canonical link for it is here.
Posted to dev@struts.apache.org by Hou Yunfeng <ho...@hotmail.com> on 2000/10/07 10:34:38 UTC

locale support improvement

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;
 this.setPathes();

 }

 /**
  * 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;
 this.setPathes();
 }

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

 pathes.clear();
 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);
  else 
   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"
      actionClass="org.apache.struts.example.LogonAction"
        formAttribute="logonForm"
        formClass="org.apache.struts.example.LogonForm"
        inputForm="/logon.jsp">
    <forward name="success"    path="/mainMenu.jsp" locales="zh_CN;en_US"/>
  </action>

 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        houyunf@hotmail.com

Re: locale support improvement

Posted by Pierre Métras <ge...@sympatico.ca>.
I think the two solutions are complementary and I support too the inclusion
of Hou's code in Struts. The dictionnary/resource management don't fit all
needs, and sometimes you want to present different layouts for different
locales. Keeping the various locale pages up to date is another problem, and
neither solution is the panacea (what's the best: have an English
application with half-translated locale pages, or different localized
applications with different features?). But companies who need international
sites have the power to manage it. And a framework should let the
application designer the choice to select the best solution, eventually on a
per-page case, as proposed by Hou.

Pierre Métras

----- Original Message -----
From: "Eduardo Pelegri--Llopart" <Ed...@eng.sun.com>
To: <st...@jakarta.apache.org>
Sent: Monday, October 09, 2000 4:53 PM
Subject: Re: locale support improvement


> I am sympathetic to both the problem and the proposed solution.  But,
> the disadvatage of this type of approach is how to keep the multiple
> languages synchronized through changes, specially when the localization
> process is done by handing over to some localization team that is barely
> knowleagable of what is going on.
>
> Perhaps one approach would be to automatically generate the resource
> files from "the" main JSP file, and then to have the authoring tool
> prompt, in-situ, for the localization data.
>
> I.e. an scenario of the form...
>
> Main team authors JSP page that uses msgs tags.
> Automated process (in authoring tool, ideally) maintains automatically
> the resouce map.
> WHen localization team goes to create localized content, it is presented
> the original JSP page + a place where to enter the translation.
>
> ////
>
> BTW one may need multiple main JSP pages in some cases, since the layout
> for non-western languages may be different enough for that.
>
> - eduard/o
>
>
> > Hou Yunfeng wrote:
> >
> > 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;
> >  this.setPathes();
> >
> >  }
> >
> >  /**
> >   * 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;
> >  this.setPathes();
> >  }
> >
> >  /**
> >   * Set the pathes for all the locales supported.
> >   *
> >   */
> >  private void setPathes() {
> >  if ((this.path == null) || (this.locales == null))
> >   return;
> >
> >  pathes.clear();
> >  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);
> >   else
> >    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"
> >       actionClass="org.apache.struts.example.LogonAction"
> >         formAttribute="logonForm"
> >         formClass="org.apache.struts.example.LogonForm"
> >         inputForm="/logon.jsp">
> >     <forward name="success"    path="/mainMenu.jsp"
> > locales="zh_CN;en_US"/>
> >   </action>
> >
> >  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        houyunf@hotmail.com
>


Re: locale support improvement

Posted by Eduardo Pelegri--Llopart <Ed...@eng.sun.com>.
I am sympathetic to both the problem and the proposed solution.  But,
the disadvatage of this type of approach is how to keep the multiple
languages synchronized through changes, specially when the localization
process is done by handing over to some localization team that is barely
knowleagable of what is going on.

Perhaps one approach would be to automatically generate the resource
files from "the" main JSP file, and then to have the authoring tool
prompt, in-situ, for the localization data.

I.e. an scenario of the form...

Main team authors JSP page that uses msgs tags.
Automated process (in authoring tool, ideally) maintains automatically
the resouce map.
WHen localization team goes to create localized content, it is presented
the original JSP page + a place where to enter the translation.

////

BTW one may need multiple main JSP pages in some cases, since the layout
for non-western languages may be different enough for that.

	- eduard/o


> Hou Yunfeng wrote:
> 
> 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;
>  this.setPathes();
> 
>  }
> 
>  /**
>   * 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;
>  this.setPathes();
>  }
> 
>  /**
>   * Set the pathes for all the locales supported.
>   *
>   */
>  private void setPathes() {
>  if ((this.path == null) || (this.locales == null))
>   return;
> 
>  pathes.clear();
>  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);
>   else
>    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"
>       actionClass="org.apache.struts.example.LogonAction"
>         formAttribute="logonForm"
>         formClass="org.apache.struts.example.LogonForm"
>         inputForm="/logon.jsp">
>     <forward name="success"    path="/mainMenu.jsp"
> locales="zh_CN;en_US"/>
>   </action>
> 
>  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        houyunf@hotmail.com