diff --git a/src/App/Utilities/EnumHelper.cs b/src/App/Utilities/EnumHelper.cs new file mode 100644 index 000000000..ef9faf286 --- /dev/null +++ b/src/App/Utilities/EnumHelper.cs @@ -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 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() is LocalizableEnumAttribute attr) + { + return AppResources.ResourceManager.GetString(attr.Key); + } + + return value.ToString(); + } + } +} diff --git a/src/App/Utilities/LocalizableEnumConverter.cs b/src/App/Utilities/LocalizableEnumConverter.cs new file mode 100644 index 000000000..8c1dc287a --- /dev/null +++ b/src/App/Utilities/LocalizableEnumConverter.cs @@ -0,0 +1,23 @@ +using System; +using Xamarin.Forms; + +namespace Bit.App.Utilities +{ + /// + /// It localizes an enum value by using the + /// + 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(); + } + } +} diff --git a/src/Core/Attributes/LocalizableEnumAttribute.cs b/src/Core/Attributes/LocalizableEnumAttribute.cs new file mode 100644 index 000000000..25230a791 --- /dev/null +++ b/src/Core/Attributes/LocalizableEnumAttribute.cs @@ -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; } + } +} diff --git a/src/Core/Core.csproj b/src/Core/Core.csproj index 6aceb3c57..6058ba3aa 100644 --- a/src/Core/Core.csproj +++ b/src/Core/Core.csproj @@ -17,6 +17,7 @@ + @@ -35,5 +36,6 @@ +