You are viewing a plain text version of this content. The canonical link for it is here.
Posted to users@tapestry.apache.org by Programozás <pr...@hotmail.com> on 2004/03/11 20:00:20 UTC

global resource bundle of localized messages

Hi,

I tried to implement a very simple approach: each library has its own global resource bundle with the same name as the library next to the library specification file. If a message key is not found in the component's own .properties files then the framework looks for it in the library's global resource bundle. In case of the library specification:

    /com/company/MyLibrary.library

a file named

    /com/company/MyLibrary.properties

should exist.

I think that it is difficult to subclass the following framework classes because of private declarations:

    org.apache.tapestry.engine.ComponentMessages: I needed _locale and _properties in my subclass, too
    org.apache.tapestry.engine.DefaultComponentMessagesSource: although my recent code overrides only getMessages()

Or I have misused something...

Norbi

Re: global resource bundle of localized messages

Posted by "F. Da Costa" <da...@xs4all.nl>.
Hi Karel,

I'm a absolute beginner on localization issues so ...

Does your code imply the following?
Per language one has to create a resource (.properties) file for *all* the 
pages in the project.
  - Example: Three supported languages would imply 3 .properties files for 
the whole app.

Judging from the fact that you have extended the Engine and 
ComponentMessages may i deduct that the basic Tapestry 'calling' structure 
remains intact?

TIA,

Fermin DCG

Karel Miarka wrote:
> Attachments discarded, so here it is:
> 
> public class Global {
>   private Map bundles = new Hashtable();
>   public ResourceBundle getBundle(Locale locale) {
>     ResourceBundle bundle = (ResourceBundle)bundles.get(locale);
>     if (bundle == null) {
>       bundles.put(locale, bundle =
> ResourceBundle.getBundle("resources.Common", locale));
>     }
>     return bundle;
>   }
>   public void resetBundles() {
>     bundles = new Hashtable();
>   }
> }
> 
> public class Engine extends BaseEngine {
>   private transient IComponentMessagesSource messagesSource;
>   public IComponentMessagesSource getComponentMessagesSource() {
>     if (messagesSource == null) {
>       messagesSource = new IComponentMessagesSource() {
>         public IMessages getMessages(IComponent component) {
>           return new MyComponentMessages(component.getPage().getLocale(),
> ((Global)getGlobal()).getBundle(component.getPage().getLocale()));
>         }
>         public void reset() {
>           ((Global)getGlobal()).resetBundles();
>         }
>       };
>     }
>     return messagesSource;
>   }
> }
> 
> class MyComponentMessages extends ComponentMessages {
>   private ResourceBundle _bundle;
>   public MyComponentMessages(Locale locale, ResourceBundle bundle) {
>     super(locale, null);
>     _bundle = bundle;
>   }
>   public String getMessage(String key, String defaultValue)
>   {
>     String result = null;
>     try {
>       result = _bundle.getString(key);
>     } catch (Exception e) {}
>     if (result == null) {
>       if (defaultValue == null) {
>         result = "[" + key + "]";
>       } else {
>         result = defaultValue;
>       }
>     }
>     return result;
>   }
>   public String getMessage(String key)
>   {
>     return getMessage(key, null);
>   }
> }
> 
> 
> ----- Original Message ----- 
> From: "Karel Miarka" <ka...@issa.cz>
> To: "Tapestry users" <ta...@jakarta.apache.org>
> Sent: Friday, March 12, 2004 8:36 AM
> Subject: Re: global resource bundle of localized messages
> 
> 
> 
>>Hi,
>>
>>I have attached my version of taking localized messages from global
> 
> resource
> 
>>bundle. I find this solution quite simple. I hope that:
>>1) it can be usefull for someone
>>2) somebody will tell me that it can be improved (I'm quite new to Java,
> 
> so
> 
>>my code is not very good).
>>
>>In the Global class I'm caching resource bundles for each locale (because
> 
> I
> 
>>was not sure if such caching is provided by
>>ResourceBundle.getBundle()) and in the Engine I'm creating a
>>ComponentMessageSource from the bundle returned by Global.
>>I had to create my subclass of ComponentMessages which accepts
>>ResourceBundle in constructor, which is missing in the framework version.
>>
>>Karel
>>
>>----- Original Message ----- 
>>From: "sebong oh" <do...@yahoo.co.kr>
>>To: "Tapestry users" <ta...@jakarta.apache.org>
>>Sent: Friday, March 12, 2004 12:23 AM
>>Subject: Re: global resource bundle of localized messages
>>
>>
>>
>>>I'm using xml global resource file.
>>>I made my own Engine class  from BaseEngine class. And overrided
>>
>>createComponentStringsSource method.
>>
>>>Maybe you'll have to define your own ComponentStringsSource class like
>>
>>XMLComponentStringsSource implementing IComponentStringsSource. It'll be
>>helpful to analyze DefaultComponentStringsSource class source.
>>
>>>
>>>
>>>
>>>----- Original Message ----- 
>>>From: "Programozás" <pr...@hotmail.com>
>>>To: "tapestry-user" <ta...@jakarta.apache.org>
>>>Cc: "tapestry-dev" <ta...@jakarta.apache.org>
>>>Sent: Friday, March 12, 2004 4:00 AM
>>>Subject: global resource bundle of localized messages
>>>
>>>
>>>Hi,
>>>
>>>I tried to implement a very simple approach: each library has its own
>>
>>global resource bundle with the same name as the library next to the
> 
> library
> 
>>specification file. If a message key is not found in the component's own
>>.properties files then the framework looks for it in the library's global
>>resource bundle. In case of the library specification:
>>
>>>    /com/company/MyLibrary.library
>>>
>>>a file named
>>>
>>>    /com/company/MyLibrary.properties
>>>
>>>should exist.
>>>
>>>I think that it is difficult to subclass the following framework classes
>>
>>because of private declarations:
>>
>>>    org.apache.tapestry.engine.ComponentMessages: I needed _locale and
>>
>>_properties in my subclass, too
>>
>>>    org.apache.tapestry.engine.DefaultComponentMessagesSource: although
> 
> my
> 
>>recent code overrides only getMessages()
>>
>>>Or I have misused something...
>>>
>>>Norbi
>>>
>>
>>
> 
> 
> 
>>---------------------------------------------------------------------
>>To unsubscribe, e-mail: tapestry-user-unsubscribe@jakarta.apache.org
>>For additional commands, e-mail: tapestry-user-help@jakarta.apache.org
> 
> 
> 
> 
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: tapestry-user-unsubscribe@jakarta.apache.org
> For additional commands, e-mail: tapestry-user-help@jakarta.apache.org
> 
> 


---------------------------------------------------------------------
To unsubscribe, e-mail: tapestry-user-unsubscribe@jakarta.apache.org
For additional commands, e-mail: tapestry-user-help@jakarta.apache.org


Re: global resource bundle of localized messages

Posted by Karel Miarka <ka...@issa.cz>.
Attachments discarded, so here it is:

public class Global {
  private Map bundles = new Hashtable();
  public ResourceBundle getBundle(Locale locale) {
    ResourceBundle bundle = (ResourceBundle)bundles.get(locale);
    if (bundle == null) {
      bundles.put(locale, bundle =
ResourceBundle.getBundle("resources.Common", locale));
    }
    return bundle;
  }
  public void resetBundles() {
    bundles = new Hashtable();
  }
}

public class Engine extends BaseEngine {
  private transient IComponentMessagesSource messagesSource;
  public IComponentMessagesSource getComponentMessagesSource() {
    if (messagesSource == null) {
      messagesSource = new IComponentMessagesSource() {
        public IMessages getMessages(IComponent component) {
          return new MyComponentMessages(component.getPage().getLocale(),
((Global)getGlobal()).getBundle(component.getPage().getLocale()));
        }
        public void reset() {
          ((Global)getGlobal()).resetBundles();
        }
      };
    }
    return messagesSource;
  }
}

class MyComponentMessages extends ComponentMessages {
  private ResourceBundle _bundle;
  public MyComponentMessages(Locale locale, ResourceBundle bundle) {
    super(locale, null);
    _bundle = bundle;
  }
  public String getMessage(String key, String defaultValue)
  {
    String result = null;
    try {
      result = _bundle.getString(key);
    } catch (Exception e) {}
    if (result == null) {
      if (defaultValue == null) {
        result = "[" + key + "]";
      } else {
        result = defaultValue;
      }
    }
    return result;
  }
  public String getMessage(String key)
  {
    return getMessage(key, null);
  }
}


----- Original Message ----- 
From: "Karel Miarka" <ka...@issa.cz>
To: "Tapestry users" <ta...@jakarta.apache.org>
Sent: Friday, March 12, 2004 8:36 AM
Subject: Re: global resource bundle of localized messages


> Hi,
>
> I have attached my version of taking localized messages from global
resource
> bundle. I find this solution quite simple. I hope that:
> 1) it can be usefull for someone
> 2) somebody will tell me that it can be improved (I'm quite new to Java,
so
> my code is not very good).
>
> In the Global class I'm caching resource bundles for each locale (because
I
> was not sure if such caching is provided by
> ResourceBundle.getBundle()) and in the Engine I'm creating a
> ComponentMessageSource from the bundle returned by Global.
> I had to create my subclass of ComponentMessages which accepts
> ResourceBundle in constructor, which is missing in the framework version.
>
> Karel
>
> ----- Original Message ----- 
> From: "sebong oh" <do...@yahoo.co.kr>
> To: "Tapestry users" <ta...@jakarta.apache.org>
> Sent: Friday, March 12, 2004 12:23 AM
> Subject: Re: global resource bundle of localized messages
>
>
> > I'm using xml global resource file.
> > I made my own Engine class  from BaseEngine class. And overrided
> createComponentStringsSource method.
> > Maybe you'll have to define your own ComponentStringsSource class like
> XMLComponentStringsSource implementing IComponentStringsSource. It'll be
> helpful to analyze DefaultComponentStringsSource class source.
> >
> >
> >
> >
> > ----- Original Message ----- 
> > From: "Programozás" <pr...@hotmail.com>
> > To: "tapestry-user" <ta...@jakarta.apache.org>
> > Cc: "tapestry-dev" <ta...@jakarta.apache.org>
> > Sent: Friday, March 12, 2004 4:00 AM
> > Subject: global resource bundle of localized messages
> >
> >
> > Hi,
> >
> > I tried to implement a very simple approach: each library has its own
> global resource bundle with the same name as the library next to the
library
> specification file. If a message key is not found in the component's own
> .properties files then the framework looks for it in the library's global
> resource bundle. In case of the library specification:
> >
> >     /com/company/MyLibrary.library
> >
> > a file named
> >
> >     /com/company/MyLibrary.properties
> >
> > should exist.
> >
> > I think that it is difficult to subclass the following framework classes
> because of private declarations:
> >
> >     org.apache.tapestry.engine.ComponentMessages: I needed _locale and
> _properties in my subclass, too
> >     org.apache.tapestry.engine.DefaultComponentMessagesSource: although
my
> recent code overrides only getMessages()
> >
> > Or I have misused something...
> >
> > Norbi
> >
>
>



> ---------------------------------------------------------------------
> To unsubscribe, e-mail: tapestry-user-unsubscribe@jakarta.apache.org
> For additional commands, e-mail: tapestry-user-help@jakarta.apache.org



---------------------------------------------------------------------
To unsubscribe, e-mail: tapestry-user-unsubscribe@jakarta.apache.org
For additional commands, e-mail: tapestry-user-help@jakarta.apache.org


Re: global resource bundle of localized messages

Posted by Karel Miarka <ka...@issa.cz>.
Hi,

I have attached my version of taking localized messages from global resource
bundle. I find this solution quite simple. I hope that:
1) it can be usefull for someone
2) somebody will tell me that it can be improved (I'm quite new to Java, so
my code is not very good).

In the Global class I'm caching resource bundles for each locale (because I
was not sure if such caching is provided by
ResourceBundle.getBundle()) and in the Engine I'm creating a
ComponentMessageSource from the bundle returned by Global.
I had to create my subclass of ComponentMessages which accepts
ResourceBundle in constructor, which is missing in the framework version.

Karel

----- Original Message ----- 
From: "sebong oh" <do...@yahoo.co.kr>
To: "Tapestry users" <ta...@jakarta.apache.org>
Sent: Friday, March 12, 2004 12:23 AM
Subject: Re: global resource bundle of localized messages


> I'm using xml global resource file.
> I made my own Engine class  from BaseEngine class. And overrided
createComponentStringsSource method.
> Maybe you'll have to define your own ComponentStringsSource class like
XMLComponentStringsSource implementing IComponentStringsSource. It'll be
helpful to analyze DefaultComponentStringsSource class source.
>
>
>
>
> ----- Original Message ----- 
> From: "Programoz�s" <pr...@hotmail.com>
> To: "tapestry-user" <ta...@jakarta.apache.org>
> Cc: "tapestry-dev" <ta...@jakarta.apache.org>
> Sent: Friday, March 12, 2004 4:00 AM
> Subject: global resource bundle of localized messages
>
>
> Hi,
>
> I tried to implement a very simple approach: each library has its own
global resource bundle with the same name as the library next to the library
specification file. If a message key is not found in the component's own
.properties files then the framework looks for it in the library's global
resource bundle. In case of the library specification:
>
>     /com/company/MyLibrary.library
>
> a file named
>
>     /com/company/MyLibrary.properties
>
> should exist.
>
> I think that it is difficult to subclass the following framework classes
because of private declarations:
>
>     org.apache.tapestry.engine.ComponentMessages: I needed _locale and
_properties in my subclass, too
>     org.apache.tapestry.engine.DefaultComponentMessagesSource: although my
recent code overrides only getMessages()
>
> Or I have misused something...
>
> Norbi
>