Hi WarnerJan,

I agree, creating a composite control and "pushing" the localized messages to child controls, its best to move the getMessage method outside the constructor to one of the event handlers (onInit, onRender) or even the #render method.

Also note that fields have the convention where it automatically picks up a localized message based on its name and '.label' postfix so it might not be necessary to push the messages:

  username.label=First Name


Keep in mind that stateful pages could create problems with adding controls in the onInit event because onInit is invoked each request. So either create the controls in the constructor and set the label during onInit, or do a check in onInit if the control is already present:

  public class LoginForm extends Form {
    private TextField username;

    public LoginForm() {
      add(username = new TextField("username", true));
    }

    public onInit() {
      username.setLabel(getMessage("label_username"));
    }
  }

kind regards

bob

PS: At some stage we need to add a "How to write controls" section to the docs to address these ins and outs.


WarnerJan Veldhuis wrote:
It might be worth mentioning in the docs that when subclassing forms and using i18n, you should place control creation in onInit(), instead of the constructor.

This example will not use i18n, since getMessage() does not have a parent context:

public class LoginForm extends Form {
    public *LoginForm()* {
TextField username = new TextField("username", getMessage("label_username"), true);
        add(username);
    }
}

This example works with i18n, at least, using onInit() is how I solved this issue:

public class LoginForm extends Form {
    public LoginForm() {
    }

    public *onInit()* {
TextField username = new TextField("username", getMessage("label_username"), true);
        add(username);
    }
}

Is this assumption correct?

Cheers,

WarnerJan

Reply via email to