Welcome, Guest
Username: Password: Remember me

TOPIC: How to Extend - add localization to mononobehavior

How to Extend - add localization to mononobehavior 2 months 3 weeks ago #3141

  • tcwicks
  • tcwicks's Avatar
  • Offline
  • New Member
  • Posts: 7
  • Karma: 0
Whats the best way to extend this. So for example I have a UIToolTip class. I want to make that localizable by I2Localize

Looks like you have ILocalizeTargetDescriptor and ILocalizeTarget
So I'm assuming that by either adding descriptiors or supporting an interface I can make the target class localizable.

Could you give me a pointer on what I should do for a monobehavior to make I2 Localize pick it up as a translation target

Kind Regards
Tikiri
The administrator has disabled public write access.

How to Extend - add localization to mononobehavior 2 months 3 weeks ago #3142

  • Frank
  • Frank's Avatar
  • Offline
  • Administrator
  • Posts: 1123
  • Thank you received: 240
  • Karma: 70
Hi,
Its quite simple, You need to create a LocalizeTarget class that does the localization, and a descriptor that its used in the editor to show all localized targets and prioritize them.

The following example, adds support for localizing the UIToolTip class with a string UIToolTip.tooltipText

Just create a new script named LocalizeTarget_UIToolTip.cs with the following code:
namespace I2.Loc
{
    #if UNITY_EDITOR
    [UnityEditor.InitializeOnLoad] 
    #endif

    public class LocalizeTarget_UIToolTip : LocalizeTarget<UIToolTip>
    {
        static LocalizeTarget_UIToolTip() { AutoRegister(); }
		
        [RuntimeInitializeOnLoadMethod(RuntimeInitializeLoadType.BeforeSceneLoad)]
		static void AutoRegister() 
		{ 
			LocalizationManager.RegisterTarget(new LocalizeTargetDesc_Type<UIToolTip, LocalizeTarget_UIToolTip>() 
			{ 
				Name = "UI ToolTip", 
				Priority = 100 
			}); 
		}

        public override eTermType GetPrimaryTermType(Localize cmp) { return eTermType.Text; }
        public override eTermType GetSecondaryTermType(Localize cmp) { return eTermType.Text; }
        public override bool CanUseSecondaryTerm() { return false; }
        public override bool AllowMainTermToBeRTL() { return true; }
        public override bool AllowSecondTermToBeRTL() { return false; }

        public override void GetFinalTerms ( Localize cmp, string Main, string Secondary, out string primaryTerm, out string secondaryTerm)
        {
            primaryTerm = mTarget ? mTarget.tooltipText : null;
            secondaryTerm = string.Empty;
        }

        public override void DoLocalize(Localize cmp, string mainTranslation, string secondaryTranslation)
        {
			mTarget.tooltipText = mainTranslation;
        }
    }
}

That will register automatically on startup.
The GetFinalTerms allows inferring the term name from the UITooltip text.
The DoLocalize applies the translation.

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
The administrator has disabled public write access.

How to Extend - add localization to mononobehavior 2 months 3 weeks ago #3143

  • tcwicks
  • tcwicks's Avatar
  • Offline
  • New Member
  • Posts: 7
  • Karma: 0
Hi Frank

Thanks much for that.

Also some variant of the following extension could be packaged with i2 Localize such that people could use an Enum rather than string keys when accessing translations from code
        public static string LocalizedEnumText<T>(this T TermKeyEnum, string Default)
            where T: struct
        {
            if (!typeof(T).IsEnum) throw new ArgumentException("T must be an enumerated type");
            string Key;
            Key = Enum.GetName(typeof(T), TermKeyEnum);
            if (string.IsNullOrEmpty(I2.Loc.Localize.MainTranslation))
                return Default;
            string Result;
            Result = I2.Loc.LocalizationManager.GetTermTranslation(Key);
            if (string.IsNullOrEmpty(Result))
            {
                //UnityEngine.Debug.Log(string.Concat(@"Translation not found for Key: ", Key));
                return Default;
            }
            return Result;
        }

So For example if using a non generics based method coupled with a specific enum
//usage example
// UITerm.term_initial_mission_energy.Translate(@"Initial Mission Energy");

        public static string Translate(this UITerm Term, string Default)
        {
            string Key;
            Key = Enum.GetName(typeof(UITerm), Term);
            if (string.IsNullOrEmpty(I2.Loc.Localize.MainTranslation))
                return Default;
            string Result;
            Result = I2.Loc.LocalizationManager.GetTermTranslation(Key);
            if (string.IsNullOrEmpty(Result))
            {
                //UnityEngine.Debug.Log(string.Concat(@"Translation not found for Key: ", Key));
                return Default;
            }
            return Result;

        }
Last Edit: 2 months 3 weeks ago by tcwicks.
The administrator has disabled public write access.

How to Extend - add localization to mononobehavior 2 months 3 weeks ago #3144

  • Frank
  • Frank's Avatar
  • Offline
  • Administrator
  • Posts: 1123
  • Thank you received: 240
  • Karma: 70
Hi,
There is already a built-in way of accessing translations without using strings:
inter-illusion.com/assets/I2Localization...riptsforsaferac.html

Just bake the terms, and instead of doing
string text1 = I2.Loc.LocalizationManager.GetTermTranslation("Tutorial/Tittle");

You can do:
string text1 = I2.Loc.ScriptLocalization.Tutorial.Tittle;

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: 2 months 3 weeks ago by Frank.
The administrator has disabled public write access.

How to Extend - add localization to mononobehavior 2 months 3 weeks ago #3145

  • tcwicks
  • tcwicks's Avatar
  • Offline
  • New Member
  • Posts: 7
  • Karma: 0
Thansk Frank
Facepalm - RTFM - LOL
:ohmy:
The administrator has disabled public write access.
Time to create page: 0.210 seconds
Colors