How to Extend - add localization to mononobehavior
6 years 5 months ago #3141
by tcwicks
How to Extend - add localization to mononobehavior was created by tcwicks
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
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
Please Log in or Create an account to join the conversation.
6 years 5 months ago #3142
by Frank
Are you Give I2L 5 stars!
Are you Please lets us know how to improve it!
Replied by Frank on topic How to Extend - add localization to mononobehavior
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:
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
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
Please Log in or Create an account to join the conversation.
6 years 5 months ago - 6 years 5 months ago #3143
by tcwicks
Replied by tcwicks on topic How to Extend - add localization to mononobehavior
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
So For example if using a non generics based method coupled with a specific enum
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: 6 years 5 months ago by tcwicks.
Please Log in or Create an account to join the conversation.
6 years 5 months ago - 6 years 5 months ago #3144
by Frank
Are you Give I2L 5 stars!
Are you Please lets us know how to improve it!
Replied by Frank on topic How to Extend - add localization to mononobehavior
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
You can do:
Hope that helps,
Frank
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: 6 years 5 months ago by Frank.
Please Log in or Create an account to join the conversation.
6 years 5 months ago #3145
by tcwicks
Replied by tcwicks on topic How to Extend - add localization to mononobehavior
Thansk Frank
Facepalm - RTFM - LOL
Facepalm - RTFM - LOL
Please Log in or Create an account to join the conversation.
Time to create page: 0.192 seconds