Versions Compared

Key

  • This line was added.
  • This line was removed.
  • Formatting was changed.

...

We know now (right?) that to add a Label to a page, we write:

Code Block
html
html

  <span 
Panel
<span
wicket:id="page.label">Label contents</span>

and

Panelcode

  add( new Label( "page.label", new PropertyModel( this, "propertyName" ) );

No problem.

So, what do we do if we want to i18nise this label?

Warning

Don't do this!

The instinctive approach for some people is to write:

Panelcode

  if( getSession().getLocale h1.  Locale.ENGLISH )


    add( new Label( "page.label", new PropertyModel( this, "enPropertyName" ) );


  else if( getSession().getLocale  Locale.FRENCH )


    add( new Label( "page.label", new PropertyModel( this, "frPropertyName" ) );


  ...

...


Rather, use the support provided by Wicket. By setting up properties files for your pages, wicket will take care of the details for you. So, you would set up files like so:

...

For instance, a model for a link could be something like:

unmigrated-wiki-markup
Panel
Code Block

  public interface Link
  {
      String getId();
      URL getUrl();
      LinkType getType();
      String getDefaultDisplayText();
      String getLocale();
  }

and to retrieve this type of link (from the DB or whatever), you could use something like this:


  public interface LinkService
  {
      Link getLink( final String key, final String locale );
  }
Code Block
Panel
Wiki Markup
Panel

Note: normally, java.util.Locale should be used in place of a String for the locale,
but I was having a few headaches and just wanted to get this done, so I used a
shortcut. (wink)

...

Panel

<a wicket:id="links.acme">ã?“ã?¡ã‚‰</a>ã?«ã?�覧ã??ã? ã?•ã?„.

and in our Page.java:

>Ã?£?ââ?¬Å?Ã?£?Ã?¡Ã?£ââ?¬Å¡Ã¢â?¬Â°</a>Ã?£?Ã?«Ã?£?ââ?¬?Ã?¨Ã?¦Ã?§Ã?£??Ã?£?Ã? Ã?£?ââ?¬Â¢Ã?£?ââ?¬Å¾.

and in our Page.java:

Code Block

  add( new i18nLink( 
Panel
add( new i18nLink(
"links.acme" );

Our i18nLink component has all the information it needs, since the id is provided and it can get the locale from the session with getSession().getLocale().

...

Code Block
public final class LinkHandler
{panel}
        extends AbstractMarkupFilter
{panel}
{
{panel}
    public LinkHandler()
    {
        super( null );
    }
{panel}

{panel}
    public MarkupElement nextTag()
        throws ParseException
    {
        // Get the next tag. If null, no more tags are available
        final ComponentTag tag = (ComponentTag)getParent().nextTag();
        if ( null == tag || null != tag.getId() )
            return tag;
{panel}

{panel}
        // Process <a> tags with "link" attribute
        if( null != tag.getName() && tag.getName().equals( "a" ) )
        {
            final String linkAttr = tag.getAttributes().getString( "link" );
            if ( ( null != linkAttr ) )
            {
                final String localeAttr = tag.getAttributes().getString( "locale" );
{panel}

{panel}
                final Link link = getLinkService().getLink( linkAttr, localeAttr );
                if( null != link && null != link.getUrl() )
                    tag.getAttributes().put( "href", link.getUrl().toString() );
                else
                    tag.getAttributes().put( "href", "linkError" );
                tag.setModified( true );
            }
        }
{panel}

{panel}
        return tag;
    }
{panel}

{panel}
    private LinkService getLinkService()
    {
        // Return the service that provides the link
    }
{panel}
}

With this approach, the content author is free to include (or not) any link by simply writing:

...

Wicket will rewrite the tag, looking up the link target in the persistence layer.

Voila!=h4.

As a Side Note

...

Rather than "overloading" the <a> tag, I would have liked to have used a different namespace, and upon parsing the markup the tag would be translated to a simple <a> tag in the default namespace. However, due to the way the parser was implemented, Wicket would not allow this.

...

Panel

<bioscene:link href="someLinkRef">Link Text</bioscene:link>

with this:

 ) )
        {
      
final
 
String
 
attrValue
 
=
 
tag
  tag.
getAttributes
setNamespace(
).getString( "href"
 null );
            
if ( ( null != attrValue ) )
tag.setName( "a" );

            final String attrValue = tag.getAttributes().getString( "href" 
{
);
            if ( ( null != attrValue ) )
            {
                tag.getAttributes().put( "href", processTag( attrValue ) );
                tag.setModified( true );
            }
        }
Code Block

        
Panel
// Process all tags in the "bioscene"
namespace
 namespace
        if( null != tag.getNamespace() && tag.getNamespace().equals( "bioscene"
) )
{
tag.setNamespace( null );
tag.setName( "a" );
Panel
Wiki Markup

But Wicket threw this:

Panel

java.text.ParseException: Tag '<:a href="/.temp/www/main/index.html">' (line 21, column 21) has a mismatched close tag at '</bioscene:link>' (line 21, column 58)

Ah, well. I've caused enough damage already.

Misc Notes

Resource search path

(From Erik van Oosten's post to the Wicket-User list)

Wicket will search all resource files with the names equal to the
components in your component hierarchy, with your application as a last
resort.

So for a MyApplication, with a MyPage containing a MyPanel Wicket will
look in:

  1. MyPanel.properties (and _locale variants)
  2. MyPage.properties (..)
  3. MyApplication.properties (..)

Actually, it even goes two steps further. Wicket will also look at
property files for the base classes of MyPanel, MyPage and
MyApplication. Secondly you can have different styles and variants.