Translate Dataset

Hello!

I am trying to do translation on the fields in a row selector object. The display text does not update automatically if you add the terms into the translation manager.

The Data In property for the row selector is hard-coded. One solution is to create two hard-coded dataset properties (one for English and one for Portuguese) and use an expression binding that switches between those two datasets. This would work fine but I would prefer to not hard-code any translations. I wanted to have all of the translations in the translation manager so they would be in one place.

Currently, I have a script that runs upon opening the vision window that loops through and translates every cell in an English dataset and writes it to the Portuguese dataset. This works but I feel that it is not ideal. It also seems to not work great when previewing the screen in designer. I am including the script below. Any suggestions? Thanks!

def translateDataset(enData):
  headers = ["Line", "Equipment", "FilterString"]
  data = []
  for row in range(enData.getRowCount()):
    data.append([system.util.translate(enData.getValueAt(row, 0)), system.util.translate(enData.getValueAt(row, 1)), system.util.translate(enData.getValueAt(row, 2))])
  
  ptData = system.dataset.toDataSet(headers, data)
  return ptData

enData = system.gui.getParentWindow(event).getComponentForPath('Root Container.Group 2').EnglishAlarmGroups

system.gui.getParentWindow(event).getComponentForPath('Root Container.Group 2').PortugueseAlarmGroups = translateDataset(enData)

There’s no fundamentally better way do it than looping through each cell, but you can use a general-purpose function (and save the repetitive lookup of the translation); I modified your script slightly to work on any input dataset:

def translateDataset(dataset):
	cache = {}
	data = []
	for row in range(dataset.rowCount):
		rowData = []
		for col in range(dataset.columnCount):
			value = dataset.getValueAt(row, col)
			if isinstance(value, basestring):
				translation = cache.setdefault(value, system.util.translate(value))
				rowData.append(translation)
			else:
				rowData.append(value)
		data.append(rowData)
  
	return system.dataset.toDataSet(list(dataset.columnNames), data)

Thanks PGriffith!