Abp-Dynamic-Form input Placeholder and enum DisplayName attribute localization #667

pull/670/head
Yunus Emre Kalkan 7 years ago
parent 475b9820c7
commit a9ef9552b0

@ -1,9 +1,13 @@
using System; using System;
using System.Collections.Generic;
using System.Linq; using System.Linq;
using System.Text.Encodings.Web; using System.Text.Encodings.Web;
using Microsoft.AspNetCore.Mvc.TagHelpers; using Microsoft.AspNetCore.Mvc.TagHelpers;
using Microsoft.AspNetCore.Mvc.ViewFeatures; using Microsoft.AspNetCore.Mvc.ViewFeatures;
using Microsoft.AspNetCore.Razor.TagHelpers; using Microsoft.AspNetCore.Razor.TagHelpers;
using Microsoft.Extensions.Localization;
using Microsoft.Extensions.Options;
using Volo.Abp.AspNetCore.Mvc.Localization;
using Volo.Abp.AspNetCore.Mvc.UI.Bootstrap.Microsoft.AspNetCore.Razor.TagHelpers; using Volo.Abp.AspNetCore.Mvc.UI.Bootstrap.Microsoft.AspNetCore.Razor.TagHelpers;
namespace Volo.Abp.AspNetCore.Mvc.UI.Bootstrap.TagHelpers.Form namespace Volo.Abp.AspNetCore.Mvc.UI.Bootstrap.TagHelpers.Form
@ -12,11 +16,15 @@ namespace Volo.Abp.AspNetCore.Mvc.UI.Bootstrap.TagHelpers.Form
{ {
private readonly IHtmlGenerator _generator; private readonly IHtmlGenerator _generator;
private readonly HtmlEncoder _encoder; private readonly HtmlEncoder _encoder;
private readonly IStringLocalizerFactory _stringLocalizerFactory;
private readonly AbpMvcDataAnnotationsLocalizationOptions _options;
public AbpInputTagHelperService(IHtmlGenerator generator, HtmlEncoder encoder) public AbpInputTagHelperService(IHtmlGenerator generator, HtmlEncoder encoder, IOptions<AbpMvcDataAnnotationsLocalizationOptions> options, IStringLocalizerFactory stringLocalizerFactory)
{ {
_generator = generator; _generator = generator;
_encoder = encoder; _encoder = encoder;
_stringLocalizerFactory = stringLocalizerFactory;
_options = options.Value;
} }
public override void Process(TagHelperContext context, TagHelperOutput output) public override void Process(TagHelperContext context, TagHelperOutput output)
@ -184,10 +192,23 @@ namespace Volo.Abp.AspNetCore.Mvc.UI.Bootstrap.TagHelpers.Form
if (attribute != null) if (attribute != null)
{ {
inputTagHelperOutput.Attributes.Add("placeholder", attribute.Value); inputTagHelperOutput.Attributes.Add("placeholder", LocalizeText(attribute.Value));
} }
} }
protected virtual string LocalizeText(string text)
{
IStringLocalizer localizer = null;
var resourceType = _options.AssemblyResources.GetOrDefault(TagHelper.AspFor.ModelExplorer.ModelType.Assembly);
if (resourceType != null)
{
localizer = _stringLocalizerFactory.Create(resourceType);
}
return localizer == null? text: localizer[text].Value;
}
protected virtual bool IsInputCheckbox(TagHelperContext context, TagHelperOutput output, TagHelperAttributeList attributes) protected virtual bool IsInputCheckbox(TagHelperContext context, TagHelperOutput output, TagHelperAttributeList attributes)
{ {
return attributes.Any(a => a.Value != null && a.Name == "type" && a.Value.ToString() == "checkbox"); return attributes.Any(a => a.Value != null && a.Name == "type" && a.Value.ToString() == "checkbox");

@ -6,11 +6,23 @@ using System.Text;
using Microsoft.AspNetCore.Mvc.Rendering; using Microsoft.AspNetCore.Mvc.Rendering;
using Microsoft.AspNetCore.Mvc.ViewFeatures; using Microsoft.AspNetCore.Mvc.ViewFeatures;
using Microsoft.AspNetCore.Razor.TagHelpers; using Microsoft.AspNetCore.Razor.TagHelpers;
using Microsoft.Extensions.Localization;
using Microsoft.Extensions.Options;
using Volo.Abp.AspNetCore.Mvc.Localization;
namespace Volo.Abp.AspNetCore.Mvc.UI.Bootstrap.TagHelpers.Form namespace Volo.Abp.AspNetCore.Mvc.UI.Bootstrap.TagHelpers.Form
{ {
public class AbpRadioInputTagHelperService : AbpTagHelperService<AbpRadioInputTagHelper> public class AbpRadioInputTagHelperService : AbpTagHelperService<AbpRadioInputTagHelper>
{ {
private readonly AbpMvcDataAnnotationsLocalizationOptions _options;
private readonly IStringLocalizerFactory _stringLocalizerFactory;
public AbpRadioInputTagHelperService(IOptions<AbpMvcDataAnnotationsLocalizationOptions> options, IStringLocalizerFactory stringLocalizerFactory)
{
_options = options.Value;
_stringLocalizerFactory = stringLocalizerFactory;
}
public override void Process(TagHelperContext context, TagHelperOutput output) public override void Process(TagHelperContext context, TagHelperOutput output)
{ {
var selectItems = GetSelectItems(context,output); var selectItems = GetSelectItems(context,output);
@ -76,12 +88,32 @@ namespace Volo.Abp.AspNetCore.Mvc.UI.Bootstrap.TagHelpers.Form
protected virtual bool GetSelectItemsIfProvidedByEnum(TagHelperContext context, TagHelperOutput output, ModelExplorer explorer, out List<SelectListItem> selectItems) protected virtual bool GetSelectItemsIfProvidedByEnum(TagHelperContext context, TagHelperOutput output, ModelExplorer explorer, out List<SelectListItem> selectItems)
{ {
IStringLocalizer localizer = null;
var resourceType = _options.AssemblyResources.GetOrDefault(explorer.ModelType.Assembly);
if (resourceType != null)
{
localizer = _stringLocalizerFactory.Create(resourceType);
}
selectItems = explorer.Metadata.IsEnum ? explorer.ModelType.GetTypeInfo().GetMembers(BindingFlags.Public | BindingFlags.Static) selectItems = explorer.Metadata.IsEnum ? explorer.ModelType.GetTypeInfo().GetMembers(BindingFlags.Public | BindingFlags.Static)
.Select((t, i) => new SelectListItem { Value = i.ToString(), Text = t.Name }).ToList() : null; .Select((t, i) => new SelectListItem { Value = i.ToString(), Text = GetLocalizedPropertyName(localizer, explorer.ModelType, t.Name) }).ToList() : null;
return selectItems != null; return selectItems != null;
} }
protected virtual string GetLocalizedPropertyName(IStringLocalizer localizer, Type enumType, string propertyName)
{
if (localizer == null)
{
return propertyName;
}
var localizedString = localizer[enumType.Name + "." + propertyName];
return !localizedString.ResourceNotFound ? localizedString.Value : localizer[propertyName].Value;
}
protected virtual bool GetSelectItemsIfProvidedFromAttribute(TagHelperContext context, TagHelperOutput output, ModelExplorer explorer, out List<SelectListItem> selectItems) protected virtual bool GetSelectItemsIfProvidedFromAttribute(TagHelperContext context, TagHelperOutput output, ModelExplorer explorer, out List<SelectListItem> selectItems)
{ {
selectItems = GetAttribute<SelectItems>(explorer)?.GetItems(explorer)?.ToList(); selectItems = GetAttribute<SelectItems>(explorer)?.GetItems(explorer)?.ToList();

@ -8,6 +8,9 @@ using Microsoft.AspNetCore.Mvc.Rendering;
using Microsoft.AspNetCore.Mvc.TagHelpers; using Microsoft.AspNetCore.Mvc.TagHelpers;
using Microsoft.AspNetCore.Mvc.ViewFeatures; using Microsoft.AspNetCore.Mvc.ViewFeatures;
using Microsoft.AspNetCore.Razor.TagHelpers; using Microsoft.AspNetCore.Razor.TagHelpers;
using Microsoft.Extensions.Localization;
using Microsoft.Extensions.Options;
using Volo.Abp.AspNetCore.Mvc.Localization;
using Volo.Abp.AspNetCore.Mvc.UI.Bootstrap.Microsoft.AspNetCore.Razor.TagHelpers; using Volo.Abp.AspNetCore.Mvc.UI.Bootstrap.Microsoft.AspNetCore.Razor.TagHelpers;
namespace Volo.Abp.AspNetCore.Mvc.UI.Bootstrap.TagHelpers.Form namespace Volo.Abp.AspNetCore.Mvc.UI.Bootstrap.TagHelpers.Form
@ -16,11 +19,15 @@ namespace Volo.Abp.AspNetCore.Mvc.UI.Bootstrap.TagHelpers.Form
{ {
private readonly IHtmlGenerator _generator; private readonly IHtmlGenerator _generator;
private readonly HtmlEncoder _encoder; private readonly HtmlEncoder _encoder;
private readonly IStringLocalizerFactory _stringLocalizerFactory;
private readonly AbpMvcDataAnnotationsLocalizationOptions _options;
public AbpSelectTagHelperService(IHtmlGenerator generator, HtmlEncoder encoder) public AbpSelectTagHelperService(IHtmlGenerator generator, HtmlEncoder encoder, IOptions<AbpMvcDataAnnotationsLocalizationOptions> options, IStringLocalizerFactory stringLocalizerFactory)
{ {
_generator = generator; _generator = generator;
_encoder = encoder; _encoder = encoder;
_stringLocalizerFactory = stringLocalizerFactory;
_options = options.Value;
} }
public override void Process(TagHelperContext context, TagHelperOutput output) public override void Process(TagHelperContext context, TagHelperOutput output)
@ -113,12 +120,32 @@ namespace Volo.Abp.AspNetCore.Mvc.UI.Bootstrap.TagHelpers.Form
protected virtual bool GetSelectItemsIfProvidedByEnum(TagHelperContext context, TagHelperOutput output, ModelExplorer explorer, out List<SelectListItem> selectItems) protected virtual bool GetSelectItemsIfProvidedByEnum(TagHelperContext context, TagHelperOutput output, ModelExplorer explorer, out List<SelectListItem> selectItems)
{ {
IStringLocalizer localizer = null;
var resourceType = _options.AssemblyResources.GetOrDefault(explorer.ModelType.Assembly);
if (resourceType != null)
{
localizer = _stringLocalizerFactory.Create(resourceType);
}
selectItems = explorer.Metadata.IsEnum ? explorer.ModelType.GetTypeInfo().GetMembers(BindingFlags.Public | BindingFlags.Static) selectItems = explorer.Metadata.IsEnum ? explorer.ModelType.GetTypeInfo().GetMembers(BindingFlags.Public | BindingFlags.Static)
.Select((t, i) => new SelectListItem { Value = i.ToString(), Text = t.Name }).ToList() : null; .Select((t, i) => new SelectListItem { Value = i.ToString(), Text = GetLocalizedPropertyName(localizer, explorer.ModelType, t.Name) }).ToList() : null;
return selectItems != null; return selectItems != null;
} }
protected virtual string GetLocalizedPropertyName(IStringLocalizer localizer, Type enumType, string propertyName)
{
if (localizer == null)
{
return propertyName;
}
var localizedString = localizer[enumType.Name + "." + propertyName];
return !localizedString.ResourceNotFound ? localizedString.Value : localizer[propertyName].Value;
}
protected virtual bool GetSelectItemsIfProvidedFromAttribute(TagHelperContext context, TagHelperOutput output, ModelExplorer explorer, out List<SelectListItem> selectItems) protected virtual bool GetSelectItemsIfProvidedFromAttribute(TagHelperContext context, TagHelperOutput output, ModelExplorer explorer, out List<SelectListItem> selectItems)
{ {
selectItems = GetAttribute<SelectItems>(explorer)?.GetItems(explorer)?.ToList(); selectItems = GetAttribute<SelectItems>(explorer)?.GetItems(explorer)?.ToList();

Loading…
Cancel
Save