/// <summary> /// Called by ASP.NET Core when we need to translate view model display labels /// </summary> /// <param name="context"></param> public void CreateDisplayMetadata(DisplayMetadataProviderContext context) { var theAttributes = context.Attributes; var modelMetadata = context.DisplayMetadata; var propertyName = context.Key.Name; var containerType = context.Key.ContainerType; if (containerType == null) { return; } var currentMetaData = modelMetadata.DisplayName?.Invoke(); if (currentMetaData == null) { return; } modelMetadata.DisplayName = () => !_configurationContext.ShouldLookupResource(currentMetaData) ? _metadataHelper.GetTranslation(currentMetaData) : _metadataHelper.GetTranslation(containerType, propertyName); var displayAttribute = theAttributes.OfType <DisplayAttribute>().FirstOrDefault(); if (displayAttribute?.Description != null) { modelMetadata.Description = () => _metadataHelper.GetTranslation(containerType, $"{propertyName}-Description"); } }
public void UseLegacyMode_DisplayNameIsNull_ReturnsFalse() { var ctx = new ConfigurationContext { EnableLegacyMode = () => true }; var result = ctx.ShouldLookupResource(null); Assert.False(result); }
public void DontUseLegacyMode_DisplayNameIsLegacyModeWithLegacyModeEnabled_ReturnsFalse() { var displayName = "/legacy/path"; var ctx = new ConfigurationContext { EnableLegacyMode = () => false }; var result = ctx.ShouldLookupResource(displayName); Assert.False(result); }
public void UseLegacyMode_DisplayNameIsNotLegacyModeWithLegacyModeEnabled_ReturnsFalse() { var displayName = "propertyName"; var ctx = new ConfigurationContext { ResourceLookupFilter = key => true }; var result = ctx.ShouldLookupResource(displayName); Assert.True(result); }
public void UseLegacyMode_EnableLegacyModeIsFalse_ReturnsTrue() { var displayName = "propertyName"; var ctx = new ConfigurationContext { EnableLegacyMode = () => true }; var result = ctx.ShouldLookupResource(displayName); Assert.True(result); }
public void DontUseLegacyMode_DisplayNameHasNotLegacyFormat_ReturnsTrue() { var displayName = "propertyName"; var ctx = new ConfigurationContext { EnableLegacyMode = () => false }; var result = ctx.ShouldLookupResource(displayName); Assert.True(result); }
public void UseLegacyMode_DisplayNameIsLegacyModeWithLegacyModeEnabled_ReturnsTrue() { var displayName = "/legacy/path"; var ctx = new ConfigurationContext { EnableLegacyMode = () => true, ResourceLookupFilter = key => true }; var result = ctx.ShouldLookupResource(displayName); Assert.True(result); }
internal string GetTranslation(string resourceKey) { var result = resourceKey; if (!_configurationContext.EnableLocalization()) { return(result); } var localizedDisplayName = _localizationProvider.GetString(resourceKey); result = localizedDisplayName; // for the legacy purposes - we need to look for this resource translation using display name // once again - this will make sure that existing XPath resources are still working if (localizedDisplayName != null && !_configurationContext.ShouldLookupResource(localizedDisplayName)) { result = _localizationProvider.GetString(localizedDisplayName); } // If other data annotations exists except for [Display], an exception is thrown when display name is "" // It should be null to avoid exception as ModelMetadata.GetDisplayName only checks for null and not String.Empty return(string.IsNullOrWhiteSpace(localizedDisplayName) ? null : result); }