[IGN-4876]System.util.translate takes too long

I have been implementing additional languages into the program and as I do, I’m noticing major slowdowns in loading times, even when it’s in English. This is especially true when it comes to
the use of system.util.translate. Has anyone else been having this issue? Is there a way to fix this or a workaround that anyone could suggest?

“Loading times” is very vague.
First, what visualization system are you using (Vision, or Perspective)?
Related to that, what are “loading times” - what action is ‘slow’ that you aren’t expecting?
Second, can you quantify “major slowdowns” a bit? Five seconds? Thirty seconds? Ten minutes?
Finally, how are you seeing system.util.translate impact loading times? What exactly are you using system.util.translate for?

I’m using perspective. Before adding the system.util.translate the page loaded really fast. I then went through and added system.util.translate all over the place and now it takes much longer to load; from about 1/10 of a second to like 3 or 4 seconds. And the parts components that don’t have the system.util.translate load in much faster, but the parts that have it take noticeably longer. I’m using it to translate text from one language to another.

I just tested it with a timer. Without system.util.translate, the page took a little more than 3 seconds to fully load. When I add the system.util.translate back in, it took 58 seconds to fully load. The elements that didn’t have it loaded pretty much right away, but then the ones with it popped in one by one.

Can you expand a little bit more on exactly how you’re using system.util.translate? Most Perspective components should automatically translate displayed text for you, and I’d expect that mechanism to be a whole lot faster (no roundtrip to the gateway is required).

We have things like this image that are several string concatenated and the different parts need to be translated. WO gets changed to 工作指示 and Boxes is changed to 盒子 for example.

Here is the code being used in an expression structure binding.

	try:
		locale = value.locale.split('-')[0]
		mode = value.mode
		wo = system.util.translate(str(value.wo), locale)
		product = system.util.translate(str(value.product), locale)
		qty = value.qty or 0.0
		units = system.util.translate(str(value.units), locale)
		
		if mode != 'Production':
			return ''
		
		if wo != '' and wo is not None:
			return 'WO# ' + wo + ' - ' + product + ' - {:,g} '.format(qty) + units
		
		if product is not None:
			return 'PC# ' + product + ' - {:,g} '.format(qty) + units
	except:
		pass
		
	return ''

EDIT: I forgot to mention this, but before running both tests, I cleared the cookies and did a hard refresh.

On a side note; if you try to use a string formatter ('{:s}'.format(units)) to format Chinese characters, it changes it from 盒子 to 盒子. This is not an issue on other python interpreters, but it is here. I don’t know if it’s a jython bug or an Ignition bug, but that took me WAY too long to figure out. It doesn’t happen with Spanish so I am thinking it is just an issue with non-alphabet languages.

You will just have to use the right encoding.

idk why its slowing down your screen so much tho.
Does this happen if you use the exact same script but comment out the translate and just use the value.wo ect? instead of system.util.translate(str(value.wo), locale)

not sure what the try except is trying to do
also i dont thing the if’s are needed as a translate should never really return None (unless you set strict to true which you dont)

Also i see no loop in here… are you translating all the data at once(you should) or on every label?(thats gonne be a lot of communication with the gateway)
Maybe its worth generating the data in a dataset tag and then create a translated dataset tag for each translation you have and dynamically grab those(and default to english if you find no translation). that should basicly eliminate any slowdown in the client, just a bit of more work for the gateway at first (but less the more clients there are) and a bit more memory used

Consider pre-translating all necessary static words into private session custom properties, where the translation is triggered only by changes in the locale. Then the heavily used expressions and transforms can simply reference {session.props.custom.translated.wo} or {session.props.custom.translated.pc} in a simple stringFormat() expression. (Bonus is that will avoid jython’s string interpolation in favor of java’s.)

(Possibly use an expression structure to generate session.props.custom.translated.* in one go.)

Yeah, unfortunately there’s a weakness in our implementation of system.util.translate here - the function can’t simply ask the gateway for a specific term, it has to load an entire ‘translation package’ (containing all your defined translations, across all locales) and then retrieve your single element. Every single call to system.util.translate is doing that.
You can use a script like the one I posted here to ‘bulk’ translate calls into a single roundtrip (and a single fetch of the translation package), or you can look into precalculating/caching translations in session properties, or you could use a modified version of the script I linked to cache the TranslationPackage in a top-level script constant.

1 Like

I think we can make these functions a lot smarter so that workarounds like my other post aren’t necessary, so I’ve made a ticket to investigate this further.

2 Likes

So I’m working through the code in the example you linked and it’s not returning the translation.

In the Translation Manager, I added:

I set the language to zh-US:
image

I ran the code:

But it just returned the key that was passed in instead of the translation.

Changing strict = True didn’t return the translation either. It just returned null.

Changing locale = None had no effect at all.

Also, is there a way for it to just pass back the entire list of terms instead of me having to pass them all in manually?

https://files.inductiveautomation.com/sdk/javadoc/ignition81/8.1.10/com/inductiveautomation/ignition/common/i18n/translation/TranslationPackage.html#getAllTranslations()
TranslationPackage has a variety of methods, you could use getAllTranslations to build up a dict of {key: {locale: translation} or {(key, locale): translation}, theoretically.
I would recommend doing that in a project script, not directly inside the tranform.

As for the script not working, you’re splitting on underscores but supplying a locale separated with hyphens.

You could try using the following class to transform a string into a locale, regardless of format:

from com.inductiveautomation.ignition.common.i18n import LocaleUtils
locale = LocaleUtils.parseLocale("zh-US").getOrThrow()
1 Like