Parameters


I2 Localization allows automatically changing part of the translations with runtime values from your game.

Texts like "The winner is {[WINNER}]"  can be translated at runtime to "The winner is Frank".


To accomplish this, your program should provide values for the parameters specified between {[ and ]}. These parameters can be added directly in the code or by adding a built-in component that manages them.





Global Parameters


These allows defining values that are global to the game. This means that these {[ tags ]} when used in any translation in your game will be automatically changed.

Global parameters are meant to be access your game data, so this has to be added as a script in your code.


The idea is creating a class that implements the ILocalizationParamsManager interface and overrides the GetParameterValue function.


This class should be registered in the LocalizationManager.ParamManagers. Once registered, every time a translation containing a {[ tag ]} is requested, the plugin will call the GetParameterValue of each of the registered managers and if any of them returns a value, then the tag is replaced with a returned value.


There is an example in the "Callbacks and Parameters.unity" sample scene. Check the "Global Parameters" object containing and example that inherits from RegisterGlobalParameters component. To make your own global managers, just duplicate the GlobalParametersExample script, rename it and add the parameters you need:



public class RegisterGlobalParameters : MonoBehaviour, ILocalizationParamsManager

{

       // Register this class every time this object is enabled

       public void OnEnable()

       {

           if (!LocalizationManager.ParamManagers.Contains(this))

           {

               LocalizationManager.ParamManagers.Add(this);

               LocalizationManager.LocalizeAll(true);

           }

       }


       // If the object is disabled, then unregister it

     public void OnDisable()

     {

           LocalizationManager.ParamManagers.Remove(this);

     }


       // Find the value of Parameters or return null if not found

     public virtual string GetParameterValue( string ParamName )

     {

           if (ParamName == "WINNER")

               return "Javier";      // You should get this value from your Game Manager

           

           if (ParamName == "NUM PLAYERS")

               return 5.ToString();


           return null; // not found is defined null, not ""

     }

}





Local Parameters


In some projects, it is desired to avoid adding code to manage the parameters. For this cases, the plugin provide a built-in component (LocalizationParamsManager) that automatically manages them.


This component is only applied to the Localize component in its same GameObject, therefore, it also allows "overriding" the Global Parameters for labels in your game.

(e.g. Let say that you have a Global Parameter that defines {[SCORE]}, returning "10 points". By adding a LocalizationParamsManager defining that same parameter to the High-scores screen, the {[SCORE}] tag could be replaced with the highscore instead of the local score of the player)


From the code, you can change the value of a local parameter by doing:


public void OnClick()

{

    var localManager = this.GetComponent<LocalizationParamsManager>();

    localManager.SetParameterValue("WINNER", "Javier");

}




Example


In the "Callbacks and Parameters.unity" example scene, is shown how to modify the translations by using Callbacks, Global and Local Parameters.


  • The first line, is a regular text without Parameters.
  • The second line uses a callback to modify the text.
  • The third line uses Global Parameters for replacing the WINNER and POINTS parameters.
  • The last line override the global parameters for this text with Local Parameters.


Created with the Personal Edition of HelpNDoc: Easily create EPub books