How to localize unknown text at runtime?

More
8 years 4 months ago #1153 by MooseMouse
I'm using NGUI 3.9.4b and I2 Localization v.2.6.2b2


I've been looking over the "UnityStandard" example. Specifically the "GUI TextCallback" gameObject. To localize the text in this gameObject at runtime, you use the callback, "CallbackNotification.cs" to specify the player color:
using UnityEngine;
using System.Collections;

namespace I2.Loc
{
	public class CallbackNotification:MonoBehaviour
	{
		public void OnModifyLocalization()
		{
			if (string.IsNullOrEmpty(Localize.MainTranslation))
				return;
			
			string PlayerColor = LocalizationManager.GetTermTranslation( "Color/Red" );
			
			Localize.MainTranslation = Localize.MainTranslation.Replace("{PLAYER_COLOR}", PlayerColor);
		}
	}
}

However, what if I sometimes the blue player wins? The I2 asset's current callback system won't accept a parameter as follows:
using UnityEngine;
using System.Collections;

namespace I2.Loc
{
	public class CallbackNotification:MonoBehaviour
	{
		public void OnModifyLocalization(string winningColor)
		{
			if (string.IsNullOrEmpty(Localize.MainTranslation))
				return;

			string PlayerColor = "";

			if (winningColor == "Red")
			{
			     PlayerColor = LocalizationManager.GetTermTranslation( "Color/Red" );
			}
			else if (winningColor == "Blue")
			{
			     PlayerColor = LocalizationManager.GetTermTranslation( "Color/Blue" );
			}
			
			Localize.MainTranslation = Localize.MainTranslation.Replace("{PLAYER_COLOR}", PlayerColor);
		}
	}
}

What is the best way to go about setting up localization for things like this that change at runtime? I have a ton of this in my game.

I also want to change fonts with languages, which I currently have set up as secondary terms on my UILabels

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

More
8 years 4 months ago - 8 years 4 months ago #1154 by Frank
Hi,
There are a few ways to pass parameters to the Localization callback.

1- you can add the parameters to the Callback class:
namespace I2.Loc
{
	public class CallbackNotification:MonoBehaviour
	{
		public string winningColor;

		public void OnModifyLocalization()
		{
			if (string.IsNullOrEmpty(Localize.MainTranslation))
				return;

			string PlayerColor = LocalizationManager.GetTermTranslation( "Color/" + winningColor );
			Localize.MainTranslation = Localize.MainTranslation.Replace("{PLAYER_COLOR}", PlayerColor);
		}
	}
}

Then in the function that finds the finds the winner in your game:
class GameManager : MonoBehaviour
{
	public GameObject WinnerLabel;   // make his to reference the localized label with the callback script

	public void FindWinner()
	{
		string winner = .... logic for finding the winner .........

		WinnerLabel.GetComponent<CallbackNotification>().winningColor = winnner;

		WinnerLabel.GetComponent<Localize>().OnLocalize(true);  // force localization   (only needed, if the winner label is already enabled, otherwise the callback is executed when the object gets enabled.
	}
}



2- This is the preferred method:
Expose the winningColor in your GameManager as a static variable and then access the variable from within the callback:
class GameManager : MonoBehaviour
{
	public static string WinningColor;

	public void FindWinner()
	{
		WinningColor = .... logic for finding the winner .........
	}
}

Then in the callback, just access GameManager.WinningColor
namespace I2.Loc
{
	public class CallbackNotification:MonoBehaviour
	{
		public void OnModifyLocalization()
		{
			if (string.IsNullOrEmpty(Localize.MainTranslation))
				return;

			string PlayerColor = LocalizationManager.GetTermTranslation( "Color/" + GameManager.WinningColor );
			Localize.MainTranslation = Localize.MainTranslation.Replace("{PLAYER_COLOR}", PlayerColor);
		}
	}
}

This second method is better in the way that you don't have to keep references and the code is more decoupled. (Singletons will do the trick as well)

I also want to change fonts with languages, which I currently have set up as secondary terms on my UILabels


Are you having troubles changing the fonts per language?
Here is some info about it:

inter-illusion.com/tools/i2-localization...ge-font-per-language

But if you still get troubles, just let me know and I can walk you through.

Hope that helps,
Frank

BTW: I started working on enabling the Plurals (should be added in the 2.6.3 or 2.6.4 at most), and that will allow you to hook the parameters directly into the localize component without having you to write a callback and do the replacement yourself. I'm excited about that as it will simplify the workflow by a lot!
I will be upgrading the Normal/Touch functionality next and will reuse that same code for the plurals :-)

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
Last edit: 8 years 4 months ago by Frank.

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

More
8 years 4 months ago #1155 by MooseMouse
Hi Frank,

Thanks for the fast answer! I thought of the ways you suggested, but was worried that the callback may run before my GameManager determines the winning color. But if this is the best way, I will sort through that.

The fonts per language are working fine. I just noticed that one solution suggested for runtime text creation was to not use the localize component and set the text was this way:

Also, If you are not using the Localize component in that label, and instead you are just adjusting things on your code. You can use:

var text = ScriptLocalization.Get("YourTerm", false); // this term should be translated with {0} where you want the replacement. e.g. "Do {0} points"
var Xvalue = 30;

text = text.Replace("{0}", Xvalue);

if (LocalizationManager.IsRight2Left) // Only fix for RTL if the current language is RTL
text = ArabicSupport.ArabicFixer.Fix(text);


This way above seems nice, but might make it difficult to change fonts without the Localize component (unless I am misunderstanding something).

So I will try the way you suggested above.

Thanks!
Shawn

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

More
8 years 4 months ago - 8 years 4 months ago #1156 by Frank

Thanks for the fast answer! I thought of the ways you suggested but was worried that the callback may run before my GameManager determines the winning color. But if this is the best way, I will sort through that.


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.

If you have the UI that should show the player that wins, disabled, and you enable that screen / label after the winner is found, then it will be fine.
Alternatively, just call OnLocalize on the label or LocalizationManager.LocalizeAll(true) after finding the winner.

This way above seems nice, but might make it difficult to change fonts without the Localize component (unless I am misunderstanding something).


Yes, the logic for changing fonts happens in the Localize component, so if you just get the translation and change the label text, the font wont be updated.

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
Last edit: 8 years 4 months ago by Frank.

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

Time to create page: 0.193 seconds
Template by JoomlaShine