3

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?!

Roman C
  • 49,761
  • 33
  • 66
  • 176
Alireza Fattahi
  • 42,517
  • 14
  • 123
  • 173
  • The better way is apply a builder to the model and replace keys with the actual names. You have to put a lot of effort writing a builder used to traverse a model translate variables for the supplied message formats. – Roman C May 29 '16 at 07:48
  • Did you try setting the `label` instead of the `name`? – Dave Newton May 29 '16 at 11:20
  • @DaveNewton, I have the same problem. name is mandatory for gridColumn,and setting label is not helping me. – Sima May 29 '16 at 11:42
  • Set the name to something *different*, e.g., `bankName` or whatever. – Dave Newton May 29 '16 at 11:47
  • @DaveNewton I tried that too , it's not helping. – Sima May 29 '16 at 12:07
  • Sorry I mean `title`, although I don't know if that will help either. E.g., `title` to the I18N bit, `name` to something that's a reasonable name. – Dave Newton May 29 '16 at 12:47
  • @DaveNewton I did set title but it's for general title of column not for each cell value – Sima May 29 '16 at 13:01
  • @RomanC could you please explain more about the builder – Sima May 30 '16 at 03:45
  • @RomanC I appreciate if you could send it as complete answer so I can accept it. – Alireza Fattahi May 30 '16 at 19:35
  • @AlirezaFattahi I didn't write an example for this issue but it seems you have a redundancy in the generated code. What if you use an argument without `getText()`? – Roman C May 30 '16 at 20:22

1 Answers1

1

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.

Roman C
  • 49,761
  • 33
  • 66
  • 176