This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
[Localizable] | |
[Display(Name = "Name")] | |
[Required] | |
[StringLength(64)] | |
public string Name { get; set; } | |
public IEnumerable<Localization> NameLocalizations { get; set; } |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
private void CreateLocalizations(PropertyInfo propertyInfo, LocalizationSet localizationSet) | |
{ | |
ICultureRepository cultureRepository = this.HttpContext.RequestServices.GetService<ICultureRepository>(); | |
ILocalizationRepository localizationRepository = this.HttpContext.RequestServices.GetService<ILocalizationRepository>(); | |
Storage storage = this.HttpContext.RequestServices.GetService<Storage>(); | |
foreach (Culture culture in cultureRepository.GetAll()) | |
{ | |
Localization localization = new Localization(); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
private LocalizationSet GetOrCreateLocalizationSetForProperty(object entity, PropertyInfo propertyInfo) | |
{ | |
ILocalizationSetRepository localizationSetRepository = this.HttpContext.RequestServices.GetService<ILocalizationSetRepository>(); | |
Storage storage = this.HttpContext.RequestServices.GetService<Storage>(); | |
PropertyInfo localizationSetIdPropertyInfo = entity.GetType().GetProperty(propertyInfo.Name + "Id"); | |
int? localizationSetId = (int?)localizationSetIdPropertyInfo.GetValue(entity); | |
LocalizationSet localizationSet = null; | |
if (localizationSetId == null || localizationSetId == 0) | |
{ |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
protected void CreateOrUpdateLocalizationsFor(object entity) | |
{ | |
foreach (PropertyInfo propertyInfo in this.GetLocalizationSetPropertiesFromEntity(entity)) | |
{ | |
LocalizationSet localizationSet = this.GetOrCreateLocalizationSetForProperty(entity, propertyInfo); | |
this.DeleteLocalizations(localizationSet); | |
this.CreateLocalizations(propertyInfo, localizationSet); | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
[HttpPost] | |
[ExportModelStateToTempData] | |
public IActionResult AddOrEdit(AddOrEdit addOrEdit) | |
{ | |
if (!this.ModelState.IsValid) | |
return this.RedirectToAction("AddOrEdit"); | |
Data.Entities.Book book = addOrEdit.Id == null ? new Data.Entities.Book() : this.bookRepository.GetById((int)addOrEdit.Id); | |
this.CreateOrUpdateLocalizationsFor(book); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
protected string GetValue(Localization localization) | |
{ | |
ModelStateEntry modelState; | |
if (this.ViewContext.ModelState.TryGetValue(this.GetIdentity(localization), out modelState) && !string.IsNullOrEmpty(modelState.AttemptedValue)) | |
return modelState.AttemptedValue; | |
return localization.Value; | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
protected TagBuilder CreateInputTag(Localization localization) | |
{ | |
TagBuilder tb = new TagBuilder("input"); | |
tb.TagRenderMode = TagRenderMode.SelfClosing; | |
if (!this.IsValid(localization)) | |
tb.AddCssClass("input-validation-error"); | |
tb.AddCssClass("field__input"); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public override void Process(TagHelperContext context, TagHelperOutput output) | |
{ | |
if (this.For == null) | |
return; | |
output.SuppressOutput(); | |
output.Content.Clear(); | |
foreach (Localization localization in this.Localizations) | |
{ |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
@foreach (var localization in this.Model.NameLocalizations) | |
{ | |
<input name="name@(localization.CultureCode)" type="text" value="@(this.ViewContext.ViewData.ModelState["name" + localization.CultureCode]?.AttemptedValue ?? localization.Value)" /> <span>(@localization.CultureCode)</span> | |
<br /> | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
@model AspNetCoreContentLocalization.ViewModels.Books.AddOrEdit | |
@inject IViewLocalizer Localizer | |
<h1>@Localizer["Add/Edit Book"]</h1> | |
<form class="form" method="post"> | |
<div class="form__field field"> | |
<label asp-for="Name"></label> | |
<br /> | |
<localizable-input asp-for="Name" asp-localizations="this.Model.NameLocalizations" /> | |
</div> | |
<div class="form__field field"> |