Skip to content
This repository has been 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

Conversation

ryanbrandenburg
Copy link
Contributor

@ryanbrandenburg ryanbrandenburg commented Aug 25, 2016

This PR addresses a bug and a couple missing features.

The bug (#5198) is that Enum display values would previously store the first value that was used and always use that. By passing a function which returns a Name value to the constructor of EnumGroupAndName and executing that when we need the name we avoid this.

One missing feature is #5197, that GetEnumSelectList was checking the DisplayAttributes for values to display, but it wasn't localizing those values like other uses of DisplayAttributes do.

The other missing feature was #4215. It boils down to that Html.DisplayFor didn't pay attention to DisplayAttributes on enums, and consequently didn't localize them.

_nameFunc = () => name;
}

public EnumGroupAndName(string group, Func<string> name)
Copy link
Contributor Author

@ryanbrandenburg ryanbrandenburg Aug 25, 2016

Choose a reason for hiding this comment

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

I couldn't decide if we should Obsolete the old constructor. My instinct was no, to allow people to do the simple thing if they didn't need anything complex, but I fear that will lead to ignoring the new constructor and getting us right back into the same spot.

Also, going back to add comments to this and any new public methods now.

Copy link
Member

Choose a reason for hiding this comment

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

Yeah I'm not sure if it needs to be obsoleted. If the docs just said precisely what it did and didn't do, that would seem to be fine.

@dougbu dougbu self-assigned this Aug 25, 2016
@dougbu
Copy link
Member

dougbu commented Aug 25, 2016

I'll take reviewing this one.

@ryanbrandenburg
Copy link
Contributor Author

CC @Eilon and @dougbu. I know this isn't exactly what we had talked about (adding new Enum properties to the DisplayName context) but once I got down into it it seemed cleaner this way.

@dougbu
Copy link
Member

dougbu commented Aug 25, 2016

The bug is that Enum value localization

When submitting the final commit, please update the description to avoid the implication that [Display(Name = "Fred")] is only about localization. The attribute is in general about UI overrides, in some cases including localization of the displayed strings.

/// </summary>
/// <param name="group">The group name.</param>
/// <param name="name">A function which returns the name. (Necessary for proper localization.)</param>
public EnumGroupAndName(string group, Func<string> name)
Copy link
Member

Choose a reason for hiding this comment

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

Probably should [Obsolete] the old constructor and file a 2.0 bug about removing it.

@dougbu
Copy link
Member

dougbu commented Aug 25, 2016

This PR addresses a bug

What bug number? Do we have anything tracking the bug or either of the gaps mentioned in the description?

Not sure about doing the three separate pieces in one PR. I'm not confident the added tests are covering each bit sufficiently.

@dougbu
Copy link
Member

dougbu commented Aug 25, 2016

@ryanbrandenburg ryanbrandenburg force-pushed the rybrande/DisplayNameLocalizer branch from 5b2a912 to ae50929 Compare August 30, 2016 17:32
@ryanbrandenburg
Copy link
Contributor Author

🆙📅 I started to split this up into three separate PR's but they're connected enough that it was going to take more time than I though was really prudent to spend on it. Are there any specific test case you feel are missing?

/// <summary>
/// Initializes a new instance of the EnumGroupAndName structure.
/// Initializes a new instance of the EnumGroupAndName structure. Should not be used if localization is in use.
Copy link
Member

Choose a reason for hiding this comment

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

What does "in use" mean? This method should not be used in any site where users may choose multiple locales.

public void CreateDisplayMetadata_EnumGroupedDisplayNamesAndValues_NameWithIStringLocalizer()
{
// Arrange & Act
var enumNameAndGroup = GetLocalizedEnumGroupedDisplayNamesAndValues();
Copy link
Member

Choose a reason for hiding this comment

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

enumGroupAndName

@dougbu
Copy link
Member

dougbu commented Sep 1, 2016

⌚ mainly for EnumGroupAndNameTest refactoring

@@ -728,6 +779,46 @@ public void CreateDisplayMetadata_IsFlagsEnum_ReflectsModelType(Type type, bool
}
Copy link
Contributor Author

Choose a reason for hiding this comment

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

CreateDisplayMetadata_EnumGroupedDisplayNamesAndValues_ReflectsModelType fails with our new EnumGroupAndName.Equals because all the EnumGroupAndName's in the MemberData don't use FieldInfo (and thus _displayattribute). Would it be enough for this test to check that the Name and group values on EnumGroupedDisplayNamesAndValues are what they should be? Because changing the MemberData to use the FieldInfo's would be basically testing that it equals itself, correct?

Copy link
Member

Choose a reason for hiding this comment

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

Yeah, best to check Name and Group in that test. Might want to create a EnumGroupAndNameComparer if that needs to be done in more than one place.

Copy link
Member

Choose a reason for hiding this comment

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

🙈 this comment thread is no longer relevant.

@ryanbrandenburg
Copy link
Contributor Author

🆙📅 Good idea on the comparer, made it much cleaner than the silly foreach zip I had started trying to do.

@ryanbrandenburg
Copy link
Contributor Author

🆙📅

/// Initializes a new instance of the EnumGroupAndName structure.
/// </summary>
/// <param name="group">The group name.</param>
/// <param name="name">A func which will return the name</param>
Copy link
Member

Choose a reason for hiding this comment

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

End w/ a period.

@dougbu
Copy link
Member

dougbu commented Sep 7, 2016

⌚ for some cleanup

@ryanbrandenburg
Copy link
Contributor Author

🆙📅

public static string DisplayAttribute_Description { get; } = Resources.DisplayAttribute_Description;
public static string Type_Three_Name => "type three name " + CultureInfo.CurrentCulture;
public static string Type_Three_Description => "type three description " + CultureInfo.CurrentCulture;
public static string Type_Three_Prompt => "type three prompt " + CultureInfo.CurrentCulture;
Copy link
Member

Choose a reason for hiding this comment

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

Please double-check that [ReplaceCulture] or using (new CultureReplacer(...)) is used in all tests referencing the CurrentCulture properties from this class.

Copy link
Member

Choose a reason for hiding this comment

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

Hmm, didn't notice Type_Three_Description and Type_Three_Prompt aren't referenced. Remove those two.

@dougbu
Copy link
Member

dougbu commented Sep 7, 2016

⌚ for the last couple of wrinkles to be ironed out.

@ryanbrandenburg ryanbrandenburg force-pushed the rybrande/DisplayNameLocalizer branch from 69c912b to a02f29f Compare September 7, 2016 18:57
@ryanbrandenburg
Copy link
Contributor Author

🆙📅

@dougbu
Copy link
Member

dougbu commented Sep 7, 2016

:shipit: w/ one removal and a better commit description. Also mention the relevant bugs.

@ryanbrandenburg
Copy link
Contributor Author

@dougbu are you talking about the actual commit message or about the Pull Request description? Because I already added the # for the 3 bugs and removed (to my eyes) the implication that it's only about localization on the Pull Request description.

Fixes #5197 GetEnumSelectList uses IStringLocalizer
Fixes #4215 Html.DisplayFor now checks DisplayAttributes on enums
@ryanbrandenburg ryanbrandenburg force-pushed the rybrande/DisplayNameLocalizer branch from a02f29f to 68bc79e Compare September 7, 2016 21:45
@ryanbrandenburg ryanbrandenburg merged commit 830983a into dev Sep 7, 2016
@ryanbrandenburg ryanbrandenburg deleted the rybrande/DisplayNameLocalizer branch September 7, 2016 23:11
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants