1

In a Vaadin 21 application I've got a grid with a column that represents an enum. Translation is done like this:

Column<MyDomainClass> myColumn = grid.addColumn(new TextRenderer<MyDomainClass>(new ItemLabelGenerator<MyDomainClass>() {
    @Override
    public String apply(MyDomainClass gridItem) {
        String i18nKey = gridItem.getMyEnumAttribute().name();
        return getTranslation(i18nKey);
    }
}

This translates the enum values in the column when the view loads.

I just added a combobox to change the locale and register all components with labels, ... by implementing the interface LocaleChangeObserver like this:

@Override
public void localeChange(LocaleChangeEvent localeChangeEvent) {
    // These two work fine
    myTextfield.setLabel(getTranslation(textfieldI18nKey));
    myCombobox.setLabel(getTranslation(comboboxI18nKey));
    [...]
    // This one feels a little strange but works
    myCombobox.setItemLabelGenerator(myCombobox.getItemLabelGenerator());
    // And for this I don't know how to do it
    myColumn.reTranslateAllItems();
}

Smell Re-Setting the ItemLabelGenerator at the combobox again feels a little strange, but works.

Question How can I retranslate all items in the column? So what's the right substitution for myColumn.reTranslateAllItems() (see above)?

Anna Koskinen
  • 1,362
  • 3
  • 22
S. Doe
  • 685
  • 1
  • 6
  • 25
  • 2
    Do you refresh the grid when the local changes? – Simon Martinelli Nov 17 '21 at 07:07
  • 4
    I'd try invoking `grid.getDataProvider().refreshAll()` at the end of localeChange. I'm not sure if it does anything if there aren't any changes, but it's worth a try. – ollitietavainen Nov 17 '21 at 07:27
  • 2
    Yes, `grid.getDataProvider().refreshAll()` does the trick. Thank you. A downside is that this reloads the data: FetchCallback and CountCallback are called again instead of just re-translating the item labels (what would be nice). – S. Doe Nov 17 '21 at 13:06

0 Answers0