Skip to content

Commit

Permalink
Updated localization extensions
Browse files Browse the repository at this point in the history
  • Loading branch information
rubo committed Oct 9, 2017
1 parent 7eda2a1 commit 9827544
Show file tree
Hide file tree
Showing 11 changed files with 63 additions and 96 deletions.
23 changes: 0 additions & 23 deletions Interop/VariadicArguments/CharArgument.cs

This file was deleted.

23 changes: 0 additions & 23 deletions Interop/VariadicArguments/DecimalArgument.cs

This file was deleted.

2 changes: 0 additions & 2 deletions Interop/VariadicArguments/DoubleArgument.cs
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,5 @@ public sealed class DoubleArgument : VariadicArgument
protected internal override void CopyTo(IntPtr ptr) => Marshal.Copy(new[] { _value }, 0, ptr, 1);

public override int Size { get; } = sizeof(double);

public override object Value => _value;
}
}
2 changes: 0 additions & 2 deletions Interop/VariadicArguments/Int32Argument.cs
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,5 @@ public sealed class Int32Argument : VariadicArgument
protected internal override void CopyTo(IntPtr ptr) => Marshal.Copy(new[] { _value }, 0, ptr, 1);

public override int Size { get; } = sizeof(int);

public override object Value => _value;
}
}
2 changes: 0 additions & 2 deletions Interop/VariadicArguments/Int64Argument.cs
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,5 @@ public sealed class Int64Argument : VariadicArgument
protected internal override void CopyTo(IntPtr ptr) => Marshal.Copy(new[] { _value }, 0, ptr, 1);

public override int Size { get; } = sizeof(long);

public override object Value => _value;
}
}
23 changes: 0 additions & 23 deletions Interop/VariadicArguments/SingleArgument.cs

This file was deleted.

2 changes: 0 additions & 2 deletions Interop/VariadicArguments/StringArgument.cs
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,5 @@ protected internal override void CopyTo(IntPtr ptr)

Marshal.Copy(new [] { _handle }, 0, ptr, 1);
}

public override object Value => _value;
}
}
10 changes: 3 additions & 7 deletions Interop/VariadicArguments/VariadicArgument.cs
Original file line number Diff line number Diff line change
Expand Up @@ -24,24 +24,20 @@ protected virtual void Dispose(bool disposing)

public virtual int Size { get; } = IntPtr.Size;

public abstract object Value { get; }

protected internal abstract void CopyTo(IntPtr ptr);

#region Operators
public static implicit operator VariadicArgument(char arg) => new CharArgument(arg);
public static implicit operator VariadicArgument(char arg) => new StringArgument(arg.ToString());

public static implicit operator VariadicArgument(decimal arg) => new DecimalArgument(arg);
public static implicit operator VariadicArgument(decimal arg) => new DoubleArgument((double) arg);

public static implicit operator VariadicArgument(double arg) => new DoubleArgument(arg);

public static implicit operator VariadicArgument(float arg) => new SingleArgument(arg);

public static implicit operator VariadicArgument(int arg) => new Int32Argument(arg);

public static implicit operator VariadicArgument(long arg) => new Int64Argument(arg);

public static implicit operator VariadicArgument(nfloat arg) => nfloat.Size == sizeof(double) ? new DoubleArgument(arg) : (VariadicArgument) new SingleArgument((float) arg);
public static implicit operator VariadicArgument(nfloat arg) => new DoubleArgument(arg);

public static implicit operator VariadicArgument(nint arg) => nint.Size == sizeof(long) ? new Int64Argument(arg) : (VariadicArgument) new Int32Argument((int) arg);

Expand Down
3 changes: 0 additions & 3 deletions MissinKit.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -43,9 +43,6 @@
<Folder Include="Resources\" />
</ItemGroup>
<ItemGroup>
<Compile Include="Interop\VariadicArguments\DecimalArgument.cs" />
<Compile Include="Interop\VariadicArguments\CharArgument.cs" />
<Compile Include="Interop\VariadicArguments\SingleArgument.cs" />
<Compile Include="Interop\VariadicArguments\DoubleArgument.cs" />
<Compile Include="Interop\VariadicArguments\Int32Argument.cs" />
<Compile Include="Interop\VariadicArguments\Int64Argument.cs" />
Expand Down
4 changes: 2 additions & 2 deletions Properties/AssemblyInfo.cs
Original file line number Diff line number Diff line change
Expand Up @@ -31,5 +31,5 @@
// You can specify all the values or you can default the Build and Revision Numbers
// by using the '*' as shown below:
// [assembly: AssemblyVersion("1.0.*")]
[assembly: AssemblyInformationalVersion("1.4.1")]
[assembly: AssemblyVersion("1.4.1.*")]
[assembly: AssemblyInformationalVersion("1.4.2")]
[assembly: AssemblyVersion("1.4.2.*")]
65 changes: 58 additions & 7 deletions Utilities/StringExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ public static class StringExtensions
/// A localized string for the current key in the default table of the main bundle
/// into which the remaining argument values in <code>args</code> are substituted.
/// </returns>
public static string Localize(this string key, params VariadicArgument[] args) => Localize(key, null, NSBundle.MainBundle, null, args);
public static string Localize(this string key, params object[] args) => Localize(key, null, NSBundle.MainBundle, null, args);

/// <summary>
/// Returns a localized string for the current key into which the remaining argument values are substituted.
Expand All @@ -37,19 +37,70 @@ public static class StringExtensions
/// A localized string for the current key in <code>table</code> of <code>bundle</code>
/// into which the remaining argument values in <code>args</code> are substituted.
/// </returns>
public static string Localize(this string key, string table, NSBundle bundle, string value, params VariadicArgument[] args)
public static string Localize(this string key, string table, NSBundle bundle, string value, params object[] args)
{
if (bundle == null)
throw new ArgumentNullException(nameof(bundle));

if (args.Length == 0)
var argCount = args?.Length ?? 0;

if (argCount == 0)
return bundle.LocalizedString(key, table, value);

var str = bundle.LocalizedNSString(key, table, value);

return string.CompareOrdinal(str.Class.Name, NSLocalizedString) == 0
? NSStringUtility.LocalizedFormat(str, args)
: string.Format(str, args.Select(a => a.Value).ToArray());

if (string.CompareOrdinal(str.Class.Name, NSLocalizedString) != 0)
return string.Format(str, args);

var varargs = new VariadicArgument[argCount];

for (var i = 0; i < argCount; i++)
{
var arg = args[i];
var type = arg?.GetType() ?? typeof(string);

switch (Type.GetTypeCode(type))
{
case TypeCode.Byte:
case TypeCode.Int16:
case TypeCode.Int32:
case TypeCode.SByte:
case TypeCode.UInt16:
case TypeCode.UInt32:
varargs[i] = (int) arg;
break;

case TypeCode.Decimal:
case TypeCode.Double:
case TypeCode.Single:
varargs[i] = (double) arg;
break;

case TypeCode.Int64:
case TypeCode.UInt64:
varargs[i] = (long) arg;
break;

case TypeCode.String:
varargs[i] = (string) arg;
break;

case TypeCode.Object:
if (type == typeof(nfloat))
varargs[i] = (nfloat) arg;
else if (type == typeof(nint))
varargs[i] = (nint) arg;
else
varargs[i] = arg.ToString();
break;

default:
varargs[i] = arg.ToString();
break;
}
}

return NSStringUtility.LocalizedFormat(str, varargs);
}
}
}

0 comments on commit 9827544

Please sign in to comment.