struts2jqgridstruts2-jquerystruts2-jquery-pluginstruts2-jquery-grid

How to use getText in grid name of Struts 2 jquery grid pluing


Consider that I have a List of AccountVO object which must be displayed in grid

AccountVO{
   bankCode; //which could be HSB,CITY
   amout;
   ...
}

In resource bundle I have

bank.name.HSB = The HSB Bank
bank.name.CITY = The CITY Bank
......
bank.name.HSB = بانک اچ اس بی
bank.name.CITY = بانک شهر

I tried to dynamically change grid name in gridColumn tag. so I used getText in gridColumn

<sjg:gridColumn name="%{getText('bank.name.'+bankCode)}" .... />

It did not work.

When I see the generated code I find below:

options_gridtable_colmodels_بانک شهر = {};
options_gridtable_colmodels_بانک شهر.name = "بانک شهر";
options_gridtable_colmodels_بانک شهر.jsonmap = "بانک شهر";

As you can see the javascript variables now have the i18n names in them, which is not correct.

To solve this, I use getText in action. For example:

for(List<Account>: account ){
  account.setI18nBankName(  getText('bank.name.'+ account.getBankCode() ) );
}

Now I can use:

<sjg:gridColumn name="i18nBankName" .... />

As you can see I need extra loop and some dummy property.

Is there better way?!


Solution

  • You would start by adding a property bankName to the AccountVO class.

    class AccountVO{
       String bankCode; //which could be HSB,CITY
       String bankName;
       Float amount;
       ...
    }
    

    This property should have translated value for the data used in the grid. You don't need to translate this data in JSP or in JS code because this logic solely belongs to a controller. Initially it can contain a key such as bank.name and then translated it to the actual value taken from the resource bundle.

    The redundant messages could be simplified if you use a message format

    bank.name = The {1} Bank
    

    Then you can use a parameter to getText().

    If you can't use parametrized messages then keep it as is and substitute a message key in the property bankName with the value from resource bundle.

    Once you have a data model translated you can use it in the grid.