Skip to content
This repository was archived by the owner on Dec 14, 2018. It is now read-only.

Localize Enum Display names #5185

Merged
merged 1 commit into from
Sep 7, 2016
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,11 @@ namespace Microsoft.AspNetCore.Mvc.ModelBinding
/// </summary>
public struct EnumGroupAndName
{
private Func<string> _name;

/// <summary>
/// Initializes a new instance of the EnumGroupAndName structure.
/// Initializes a new instance of the <see cref="EnumGroupAndName"/> structure. This constructor should
/// not be used in any site where localization is important.
/// </summary>
/// <param name="group">The group name.</param>
/// <param name="name">The name.</param>
Expand All @@ -28,7 +31,30 @@ public EnumGroupAndName(string group, string name)
}

Group = group;
Name = name;
_name = () => name;
}

/// <summary>
/// Initializes a new instance of the <see cref="EnumGroupAndName"/> structure.
/// </summary>
/// <param name="group">The group name.</param>
/// <param name="name">A <see cref="Func{String}"/> which will return the name.</param>
public EnumGroupAndName(
string group,
Func<string> name)
{
if (group == null)
{
throw new ArgumentNullException(nameof(group));
}

if (name == null)
{
throw new ArgumentNullException(nameof(name));
}

Group = group;
_name = name;
}

/// <summary>
Expand All @@ -39,6 +65,12 @@ public EnumGroupAndName(string group, string name)
/// <summary>
/// Gets the name.
/// </summary>
public string Name { get; }
public string Name
{
get
{
return _name();
}
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -153,15 +153,17 @@ public void CreateDisplayMetadata(DisplayMetadataProviderContext context)
// Dictionary does not guarantee order will be preserved.
var groupedDisplayNamesAndValues = new List<KeyValuePair<EnumGroupAndName, string>>();
var namesAndValues = new Dictionary<string, string>();
var enumLocalizer = _stringLocalizerFactory?.Create(underlyingType);
foreach (var name in Enum.GetNames(underlyingType))
{
var field = underlyingType.GetField(name);
var displayName = GetDisplayName(field);
var groupName = GetDisplayGroup(field);
var value = ((Enum)field.GetValue(obj: null)).ToString("d");

groupedDisplayNamesAndValues.Add(new KeyValuePair<EnumGroupAndName, string>(
new EnumGroupAndName(groupName, displayName),
new EnumGroupAndName(
groupName,
() => GetDisplayName(field, enumLocalizer)),
value));
namesAndValues.Add(name, value);
}
Expand Down Expand Up @@ -291,18 +293,19 @@ public void CreateValidationMetadata(ValidationMetadataProviderContext context)
}
}
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Return these methods to their previous order. No longer sure what changed here.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

thx


// Return non-empty name specified in a [Display] attribute for a field, if any; field.Name otherwise.
private static string GetDisplayName(FieldInfo field)
private static string GetDisplayName(FieldInfo field, IStringLocalizer stringLocalizer)
{
var display = field.GetCustomAttribute<DisplayAttribute>(inherit: false);
if (display != null)
{
// Note [Display(Name = "")] is allowed.
// Note [Display(Name = "")] is allowed but we will not attempt to localize the empty name.
var name = display.GetName();
if (name != null)
if (stringLocalizer != null && !string.IsNullOrEmpty(name) && display.ResourceType == null)
{
return name;
name = stringLocalizer[name];
}

return name ?? field.Name;
}

return field.Name;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@

using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.IO;
using System.Linq;
using System.Text;
Expand Down Expand Up @@ -522,6 +523,23 @@ protected virtual IHtmlContent GenerateDisplay(
string templateName,
object additionalViewData)
{
var modelEnum = modelExplorer.Model as Enum;
if (modelExplorer.Metadata.IsEnum && modelEnum != null)
{
var value = modelEnum.ToString("d");
var enumGrouped = modelExplorer.Metadata.EnumGroupedDisplayNamesAndValues;
Debug.Assert(enumGrouped != null);
foreach (var kvp in enumGrouped)
{
if (kvp.Value == value)
{
// Creates a ModelExplorer with the same Metadata except that the Model is a string instead of an Enum
modelExplorer = modelExplorer.GetExplorerForModel(kvp.Key.Name);
break;
}
}
}

var templateBuilder = new TemplateBuilder(
_viewEngine,
_bufferScope,
Expand Down
Loading