Callback Function Only Called Once

More
7 years 8 months ago #1584 by BryanJ-GreyBorn
Hey there,

I'm currently having a problem where the first time a UI Canvas is set to active, the localization callback is called, but once it's disabled and set back to active, it isn't called again. I looked into the documentation and dug through similar questions on this forum and found this:

"The Callback is called whenever the label is localized. Which only happens when the object is enabled, language changes or if you are calling OnLocalize directly."

So is the callback supposed to only be called the first time or every time the UI is enabled? I'm currently doing a workaround, where I just call LocalizationManager.LocalizeAll(true); every time I open the UI after its initial opening. Obviously this isn't the best way to do this, so here's hoping this is a simple fix!

Thanks,
Bryan

Please Log in or Create an account to join the conversation.

More
7 years 8 months ago #1585 by Frank
Hi,
I think the behavior you are seeing is the correct one.

The plugin calls the callback when it has to localize the label. This only happens when the label is enabled, the current language is changed or as in your workaround, when the forcing the localizationManager to localize all which in turns call OnLocalize in the label.

However, if the label has already been localized, it won't be localized again unless the language is different. This is an optimization for objects that are enabled/disabled continuously. If the text was localized to the correct language, then there shouldn't be a need to localize it again to the same language. That's why even if the object is enabled, it will not trigger the localization and will not call the callback.

Said that, I see that in your case you are using the callback to add parameters, and so you need it to run again even if the text is already localized to your current language.

You can do it in several ways:

1- You can create an script like the following:

ForceLocalizeOnEnable.cs
using UnityEngine;

namespace I2.Loc
{
	public class ForceLocalizeOnEnable : MonoBehaviour
	{
		public void OnEnable()
		{
			var localize = GetComponent<Localize> ();
			if (localize)
				localize.OnLocalize (true);
		}
	}
}

If you attach that behavior to your label, it will force localizing it (and call the callback) every time you enable it. That will be faster than calling the LocalizationManager.LocalizeAll(true) because that last function forces ALL labels to be localized again.


2- The other way is to always force the localization when the object is enabled

To do this, just open the Localize.cs file and comment the lines 122 and 123 as follows:
		public void OnLocalize( bool Force = false )
		{
			if (!Force && (!enabled || gameObject==null || !gameObject.activeInHierarchy))
				return;

			//if (string.IsNullOrEmpty(LocalizationManager.CurrentLanguage))                            --- this line
			//	return;                                                                                                                               --- and this line

			if (!Force && LastLocalizedLanguage==LocalizationManager.CurrentLanguage)
				return;
			LastLocalizedLanguage = LocalizationManager.CurrentLanguage;

That will do the trick.


However, I see the need to make a proper fix for this (more now that you can do callback and parameters ), so I went ahead and modified the plugin to add a "Force Localize" toggle that makes the plugin skips the optimizations for that label and always call the Callback and the Parameters.



I already uploaded a new beta (2.6.7a3) with this change to the beta folder.
If you download it, it will automatically enable that toggle when there is a callback assigned.

Hope that helps,
Frank

Are you :-) Give I2L 5 stars!
Are you :-( Please lets us know how to improve it!
To get the betas as soon as they are ready, check this out
Attachments:

Please Log in or Create an account to join the conversation.

More
7 years 8 months ago #1594 by BryanJ-GreyBorn
Thanks for the wonderfully detailed response. I used the force localization script and it works perfectly. Thanks again for the quick response!

-Bryan

Please Log in or Create an account to join the conversation.

Time to create page: 0.267 seconds
Template by JoomlaShine