Razor does not return the localized resource according to the current thread culture and I have no idea why. It always return the string within the neutral resx file.
PurchaseOrderModel.cs:
public class PurchaseOrderModel : ProfileSpecificEntityModel {
[Display(ResourceType = typeof(PurchaseOrderModelRes), Name = "ExpectedDeliveryOn")]
public DateTimeOffset? ExpectedDeliveryOn { get; set; }
}
In the same folder, I have:
PurchaseOrderModelRes.resx
-> Neutral resource. Access modifier set to "public".
PurchaseOrderModelRes.fr.resx
-> Localized French resource.
PurchaseOrderReport.cs:
<div>@Html.DisplayNameFor(purchaseOrder => purchaseOrder.ExpectedDeliveryOn)</div>
PurchaseOrderController.cs:
[HttpGet]
public async Task<IActionResult> PurchaseOrdersReportAsync([FromQuery] PurchaseOrderListQueryOptionsModel queryOptions) {
CultureInfo.CurrentCulture = CultureInfo.GetCultureInfo("fr"); //Should use *.fr.resx files.
CultureInfo.CurrentUICulture = CultureInfo.GetCultureInfo("fr");
PaginatedList<PurchaseOrder> purchaseOrders = await _purchaseOrderService.GetPurchaseOrdersAsync(CurrentUser, queryOptions.ToQueryOptions());
PaginatedListModel<PurchaseOrderModel> purchaseOrderModels = _paginatedListModelConverter.Convert(purchaseOrders, p => _purchaseOrderModelConverter.Convert(p));
return View("PurchaseOrderReport", purchaseOrderModels.Items);
}
Startup.cs:
services.AddMvc().
.AddViewLocalization(LanguageViewLocationExpanderFormat.Suffix)
.AddDataAnnotationsLocalization();