You are viewing a plain text version of this content. The canonical link for it is here.
Posted to users@wicket.apache.org by Michał Letyński <ml...@consol.pl> on 2009/04/14 11:07:05 UTC

Getting localized string in constructor.

Hi.
When im getting localized string in constructor via getString method the 
warning message is dispalyed: "Tried to retrieve a localized string for 
a component that has not yet been added to the page. "

I know that one possible way to "fix" this is to use 
ResourceModel("key'). But what if i cant use ResourceModel ? for e.g 
some component want String directly. Is there a better solution then 
using: 
"Application.get().getResourceSettings().getLocalizer().getString(resourceKey,
            (Component)null, defaultValue);


---------------------------------------------------------------------
To unsubscribe, e-mail: users-unsubscribe@wicket.apache.org
For additional commands, e-mail: users-help@wicket.apache.org


Re: Getting localized string in constructor.

Posted by "Srikanth.NT" <nt...@gmail.com>.
We use a static method to make this work without warning.

public static String getResourceString(final Class clazz, final String
propertyName) {
    List<IStringResourceLoader> loaderList =
Application.get().getResourceSettings().getStringResourceLoaders();
    for (IStringResourceLoader loader : loaderList) {
        String value = loader.loadStringResource(clazz, propertyName, null,
MyOwnSession.get().getStyle());
        if (StringUtils.isNotBlank(value)) {
            return value;
        }
    }
    return null;
}




Michał Letyński wrote:
> 
> Hi.
> When im getting localized string in constructor via getString method the 
> warning message is dispalyed: "Tried to retrieve a localized string for 
> a component that has not yet been added to the page. "
> 
> I know that one possible way to "fix" this is to use 
> ResourceModel("key'). But what if i cant use ResourceModel ? for e.g 
> some component want String directly. Is there a better solution then 
> using: 
> "Application.get().getResourceSettings().getLocalizer().getString(resourceKey,
>             (Component)null, defaultValue);
> 
> 
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: users-unsubscribe@wicket.apache.org
> For additional commands, e-mail: users-help@wicket.apache.org
> 
> 
> 


-----
http://ntsrikanth.blogspot.com/
-- 
View this message in context: http://www.nabble.com/Getting-localized-string-in-constructor.-tp23035847p23038671.html
Sent from the Wicket - User mailing list archive at Nabble.com.


---------------------------------------------------------------------
To unsubscribe, e-mail: users-unsubscribe@wicket.apache.org
For additional commands, e-mail: users-help@wicket.apache.org


Re: Getting localized string in constructor.

Posted by Jonas <ba...@gmail.com>.
Just checked the code again, the message seems to be shown always (not
just in development mode). To work around this, you can either call
the localizer
without component (as you originally proposed), or you can configure your
logging not to log warnings from the localizer. Just remember that
those warnings
might be useful if you decide later to have per page resource bundles
after all...


2009/4/14 Jonas <ba...@gmail.com>:
> Hi,
>
> I don't think createing a ResourceModel instance makes sense, because
> all ResourceModel
> does is using the localizer...
>
> Regarding the original question, if I remember correctly the warning
> message only shows
> up in development mode to give you a heads up that resource lookup
> will not work as
> expected if the component is not yet added to a page.
> See: <http://day-to-day-stuff.blogspot.com/2008/05/wicket-internationalization.html>
> section 'Finding the message'
> If the resource string isn't located in the page's resource file, it's
> probably safe to ignore the warning.
>
> Besides, if you're in a Component, you can use Component#getString as
> shortcut (instead
> of Application.get().getResourceSettings().getLocalizer().getString(...))
>
> cheers,
> Jonas
>
> 2009/4/14 Witold Czaplewski <wi...@cts-media.eu>:
>> Hi Michał,
>>
>> if you need the string of a ResourceModel you may want to call: new
>> ResourceModel("key").getObject().toString().
>>
>> cheers,
>> Witold
>>
>> Am Tue, 14 Apr 2009 11:07:05 +0200
>> schrieb Michał Letyński <ml...@consol.pl>:
>>
>>> Hi.
>>> When im getting localized string in constructor via getString method the
>>> warning message is dispalyed: "Tried to retrieve a localized string for
>>> a component that has not yet been added to the page. "
>>>
>>> I know that one possible way to "fix" this is to use
>>> ResourceModel("key'). But what if i cant use ResourceModel ? for e.g
>>> some component want String directly. Is there a better solution then
>>> using:
>>> "Application.get().getResourceSettings().getLocalizer().getString(resourceKey,
>>>             (Component)null, defaultValue);
>>>
>>>
>>> ---------------------------------------------------------------------
>>> To unsubscribe, e-mail: users-unsubscribe@wicket.apache.org
>>> For additional commands, e-mail: users-help@wicket.apache.org
>>>
>>
>> ---------------------------------------------------------------------
>> To unsubscribe, e-mail: users-unsubscribe@wicket.apache.org
>> For additional commands, e-mail: users-help@wicket.apache.org
>>
>>
>

---------------------------------------------------------------------
To unsubscribe, e-mail: users-unsubscribe@wicket.apache.org
For additional commands, e-mail: users-help@wicket.apache.org


Re: Getting localized string in constructor.

Posted by Jonas <ba...@gmail.com>.
Hi,

I don't think createing a ResourceModel instance makes sense, because
all ResourceModel
does is using the localizer...

Regarding the original question, if I remember correctly the warning
message only shows
up in development mode to give you a heads up that resource lookup
will not work as
expected if the component is not yet added to a page.
See: <http://day-to-day-stuff.blogspot.com/2008/05/wicket-internationalization.html>
section 'Finding the message'
If the resource string isn't located in the page's resource file, it's
probably safe to ignore the warning.

Besides, if you're in a Component, you can use Component#getString as
shortcut (instead
of Application.get().getResourceSettings().getLocalizer().getString(...))

cheers,
Jonas

2009/4/14 Witold Czaplewski <wi...@cts-media.eu>:
> Hi Michał,
>
> if you need the string of a ResourceModel you may want to call: new
> ResourceModel("key").getObject().toString().
>
> cheers,
> Witold
>
> Am Tue, 14 Apr 2009 11:07:05 +0200
> schrieb Michał Letyński <ml...@consol.pl>:
>
>> Hi.
>> When im getting localized string in constructor via getString method the
>> warning message is dispalyed: "Tried to retrieve a localized string for
>> a component that has not yet been added to the page. "
>>
>> I know that one possible way to "fix" this is to use
>> ResourceModel("key'). But what if i cant use ResourceModel ? for e.g
>> some component want String directly. Is there a better solution then
>> using:
>> "Application.get().getResourceSettings().getLocalizer().getString(resourceKey,
>>             (Component)null, defaultValue);
>>
>>
>> ---------------------------------------------------------------------
>> To unsubscribe, e-mail: users-unsubscribe@wicket.apache.org
>> For additional commands, e-mail: users-help@wicket.apache.org
>>
>
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: users-unsubscribe@wicket.apache.org
> For additional commands, e-mail: users-help@wicket.apache.org
>
>

---------------------------------------------------------------------
To unsubscribe, e-mail: users-unsubscribe@wicket.apache.org
For additional commands, e-mail: users-help@wicket.apache.org


Re: Getting localized string in constructor.

Posted by Witold Czaplewski <wi...@cts-media.eu>.
Hi Michał,

if you need the string of a ResourceModel you may want to call: new
ResourceModel("key").getObject().toString().

cheers,
Witold

Am Tue, 14 Apr 2009 11:07:05 +0200
schrieb Michał Letyński <ml...@consol.pl>:

> Hi.
> When im getting localized string in constructor via getString method the 
> warning message is dispalyed: "Tried to retrieve a localized string for 
> a component that has not yet been added to the page. "
> 
> I know that one possible way to "fix" this is to use 
> ResourceModel("key'). But what if i cant use ResourceModel ? for e.g 
> some component want String directly. Is there a better solution then 
> using: 
> "Application.get().getResourceSettings().getLocalizer().getString(resourceKey,
>             (Component)null, defaultValue);
> 
> 
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: users-unsubscribe@wicket.apache.org
> For additional commands, e-mail: users-help@wicket.apache.org
> 

---------------------------------------------------------------------
To unsubscribe, e-mail: users-unsubscribe@wicket.apache.org
For additional commands, e-mail: users-help@wicket.apache.org