Skip to content

Instantly share code, notes, and snippets.

@alexshikov
Last active September 1, 2017 13:11
Show Gist options
  • Save alexshikov/d90df33269a9691c15bad2992534a459 to your computer and use it in GitHub Desktop.
Save alexshikov/d90df33269a9691c15bad2992534a459 to your computer and use it in GitHub Desktop.
i18N-Portable iOS auto-wire extensions

This is an example of controls auto-wire for i18N-Portrable library on iOS (storyboards and xibs) as described in the feature request https://github.com/xleon/I18N-Portable/issues/8

This case implements an idea of using User Defined Runtime Attributes

An important thing is runtime attributes can be used only with custom classes, otherwise an error thrown on screen loading:

Failed to set (translate) user defined inspected property on (UILabel)

That is why custom Label and Button classes are implemented and must be used for labels and buttons that should be transtated. See https://stackoverflow.com/a/26674870/4478393.

This approach looks not so easy as using Accessibility Label but if feel right.

A few controls are missing (TextField, TextArea, etc.) ...

[Register("Button")]
public class LocalizedButton: UIButton
{
[Connect]
private NSString Translate
{
get { return (NSString)ValueForKey (new NSString (LocalizationExtensions.LocalizationKey)); }
set { SetValueForKey (new NSString (LocalizationExtensions.LocalizationKey), value); }
}
public LocalizedButton ()
{
}
public LocalizedButton (NSCoder coder) : base (coder)
{
}
public LocalizedButton (CGRect frame, MDButtonType buttonType, UIColor rippleColor) : base (frame, buttonType, rippleColor)
{
}
protected LocalizedButton (NSObjectFlag t) : base (t)
{
}
protected internal LocalizedButton (IntPtr handle) : base (handle)
{
}
public override void WillMoveToWindow (UIWindow window)
{
base.WillMoveToWindow (window);
this.Localize ();
}
}
[Register ("Label")]
public class Label: UILabel
{
[Connect]
private NSString Translate
{
get { return (NSString)ValueForKey (new NSString (LocalizationExtensions.LocalizationKey)); }
set { SetValueForKey (new NSString (LocalizationExtensions.LocalizationKey), value); }
}
public Label ()
{}
public Label (IntPtr handle): base (handle)
{}
public Label (NSCoder coder): base (coder)
{}
public override void WillMoveToWindow (UIWindow window)
{
base.WillMoveToWindow (window);
this.Localize ();
}
}
public static class LocalizationExtensions
{
public const string LocalizationKey = "Translate";
public static void Localize (this UILabel label)
{
var key = GetValueForKey (label);
if (key != null)
{
label.Text = I18N.Current[key];
}
}
public static void Localize (this UIButton button)
{
var key = GetValueForKey (button);
if (key != null)
{
button.SetTitle (I18N.Current[key], UIControlState.Normal);
}
}
private static string GetValueForKey (UIView view)
{
try
{
return (NSString)view.ValueForKey (new NSString (LocalizationKey));
}
catch (MonoTouchException e)
{
if (e.Name == "NSUnknownKeyException")
{
return null;
}
throw;
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment