Perspective System.util.translate Poor/Inconsistent Performance

The 'round trip' back and forth is probably not as much of a problem as that post makes it out to be, but...

Yes, absolutely. Something like this in a script library should allow you to translate many terms at once, reasonably efficiently:

def translate(terms, strict=False, locale=None):
	from com.inductiveautomation.ignition.gateway import IgnitionGateway
	from com.inductiveautomation.ignition.gateway.script import GatewaySystemUtilities
	package = IgnitionGateway.get().getLocalizationManager().loadFullPackage()
	if locale is None:
		locale = GatewaySystemUtilities.LOCAL_LOCALE.get()
	fn = package.getStrict if strict else package.get
	return [fn(locale, term) for term in terms]

EDIT 10/13/21:
Potentially more end-user useful, a function that returns a dictionary:

def translate(terms, strict=False, locale=None):
	"""
	Translate a collection of terms in bulk, rather than all at once.

	Args:
		terms: The terms to translate.
		strict (bool): If True, a provided key without a translation will return
			None. If False, the original key will be returned.
		locale (java.util.Locale): The locale to translate values to. 
			If not provided, will attempt to use the default from the current 
			context (e.g. Perspective session or Gateway default).

	Returns:
		dict: A mapping of original keys to translated terms.
	"""
	from com.inductiveautomation.ignition.gateway import IgnitionGateway
	from com.inductiveautomation.ignition.gateway.script import GatewaySystemUtilities
	package = IgnitionGateway.get().getLocalizationManager().loadFullPackage()
	if locale is None:
		locale = GatewaySystemUtilities.LOCAL_LOCALE.get()
	fn = package.getStrict if strict else package.get
	return {term: fn(locale, term) for term in terms}
5 Likes