mirror of
https://github.com/bitwarden/android.git
synced 2024-12-25 02:18:27 +03:00
EC-487 Added helper to localize enum values and also a converter to use in xaml (#2048)
This commit is contained in:
parent
9163b9e4de
commit
d204e812e1
4 changed files with 72 additions and 0 deletions
33
src/App/Utilities/EnumHelper.cs
Normal file
33
src/App/Utilities/EnumHelper.cs
Normal file
|
@ -0,0 +1,33 @@
|
||||||
|
using System;
|
||||||
|
using System.Linq;
|
||||||
|
using System.Reflection;
|
||||||
|
using Bit.App.Resources;
|
||||||
|
using Bit.Core.Attributes;
|
||||||
|
using Xamarin.CommunityToolkit.Helpers;
|
||||||
|
|
||||||
|
namespace Bit.App.Utilities
|
||||||
|
{
|
||||||
|
public static class EnumHelper
|
||||||
|
{
|
||||||
|
public static string GetLocalizedValue<T>(T value)
|
||||||
|
{
|
||||||
|
return GetLocalizedValue(value, typeof(T));
|
||||||
|
}
|
||||||
|
|
||||||
|
public static string GetLocalizedValue(object value, Type type)
|
||||||
|
{
|
||||||
|
if (!type.GetTypeInfo().IsEnum)
|
||||||
|
{
|
||||||
|
throw new ArgumentException("type should be an enum");
|
||||||
|
}
|
||||||
|
|
||||||
|
var valueMemberInfo = type.GetMember(value.ToString())[0];
|
||||||
|
if (valueMemberInfo.GetCustomAttribute<LocalizableEnumAttribute>() is LocalizableEnumAttribute attr)
|
||||||
|
{
|
||||||
|
return AppResources.ResourceManager.GetString(attr.Key);
|
||||||
|
}
|
||||||
|
|
||||||
|
return value.ToString();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
23
src/App/Utilities/LocalizableEnumConverter.cs
Normal file
23
src/App/Utilities/LocalizableEnumConverter.cs
Normal file
|
@ -0,0 +1,23 @@
|
||||||
|
using System;
|
||||||
|
using Xamarin.Forms;
|
||||||
|
|
||||||
|
namespace Bit.App.Utilities
|
||||||
|
{
|
||||||
|
/// <summary>
|
||||||
|
/// It localizes an enum value by using the <see cref="Core.Attributes.LocalizableEnumAttribute"/>
|
||||||
|
/// </summary>
|
||||||
|
public class LocalizableEnumConverter : IValueConverter
|
||||||
|
{
|
||||||
|
public object Convert(object value, Type targetType, object parameter,
|
||||||
|
System.Globalization.CultureInfo culture)
|
||||||
|
{
|
||||||
|
return EnumHelper.GetLocalizedValue(value, value.GetType());
|
||||||
|
}
|
||||||
|
|
||||||
|
public object ConvertBack(object value, Type targetType, object parameter,
|
||||||
|
System.Globalization.CultureInfo culture)
|
||||||
|
{
|
||||||
|
throw new NotSupportedException();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
14
src/Core/Attributes/LocalizableEnumAttribute.cs
Normal file
14
src/Core/Attributes/LocalizableEnumAttribute.cs
Normal file
|
@ -0,0 +1,14 @@
|
||||||
|
using System;
|
||||||
|
namespace Bit.Core.Attributes
|
||||||
|
{
|
||||||
|
[AttributeUsage(AttributeTargets.Field | AttributeTargets.Property, AllowMultiple = false)]
|
||||||
|
public class LocalizableEnumAttribute : Attribute
|
||||||
|
{
|
||||||
|
public LocalizableEnumAttribute(string key)
|
||||||
|
{
|
||||||
|
Key = key;
|
||||||
|
}
|
||||||
|
|
||||||
|
public string Key { get; }
|
||||||
|
}
|
||||||
|
}
|
|
@ -17,6 +17,7 @@
|
||||||
<None Remove="Resources\public_suffix_list.dat" />
|
<None Remove="Resources\public_suffix_list.dat" />
|
||||||
<None Remove="Microsoft.AppCenter.Crashes" />
|
<None Remove="Microsoft.AppCenter.Crashes" />
|
||||||
<None Remove="Services\Logging\" />
|
<None Remove="Services\Logging\" />
|
||||||
|
<None Remove="Attributes\" />
|
||||||
</ItemGroup>
|
</ItemGroup>
|
||||||
|
|
||||||
<ItemGroup>
|
<ItemGroup>
|
||||||
|
@ -35,5 +36,6 @@
|
||||||
|
|
||||||
<ItemGroup>
|
<ItemGroup>
|
||||||
<Folder Include="Services\Logging\" />
|
<Folder Include="Services\Logging\" />
|
||||||
|
<Folder Include="Attributes\" />
|
||||||
</ItemGroup>
|
</ItemGroup>
|
||||||
</Project>
|
</Project>
|
||||||
|
|
Loading…
Reference in a new issue