Context based NLS

Sometimes translation are needed in a specific context.

Often these leads to duplicated parts of the translation key in the code like:

    NLS nls = NLS_FACTORY.nls();

    lblCustomerName.setText(nls.get("PageUI.name"));
    lblStreet.setText(nls.get("PageUI.street"));
    lblPageOf.setText(nls.get("PageUI.page_counter", 3, 5));
    btnClose.setText(nls.get("PageUI.btn.close"));

Here could say the the class or its translations are in the context 'page.options'.

NILS provides a way to get only translations in a context. From an existing NLS object or from the NilsFactory. This could shorten the code as followed:

    NLS nls = NLS_FACTORY.nls().context("PageUI");

    lblCustomerName.setText(nls.get("name"));
    lblStreet.setText(nls.get("street"));
    lblPageOf.setText(nls.get("page_counter", 3, 5));
    btnClose.setText(nls.get("btn.close"));

This also works for classes:

    NLS nls = NLS_FACTORY.nlsWithContext(PageUI.class);

    lblCustomerName.setText(nls.get("name"));
    lblStreet.setText(nls.get("street"));
    lblPageOf.setText(nls.get("page_counter", 3, 5));
    btnClose.setText(nls.get("btn.close"));

See: the NLS.context-methods and the NilsFactory.context-methods for more information.