IViewLocalizer in cshtml template
See original GitHub issueHello people,
I’m trying to create an email using Razorlight.
In my template, I wan’t to inject IViewLocalizer
to have a localized email.
My issue is when I render the template, I have a Null Reference
if I use the IViewLocalizer
This is normal or I’m doing something wrong ?
Code
@using RazorLight
@inherits TemplatePage<Gardendynamics.Commons.Picaplant.Models.Output.OrderModel>
@using Microsoft.AspNetCore.Mvc.Localization
@inject IViewLocalizer Localizer
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title> @Localizer["OrderTitleEmail"]</title>
<style type="text/css">
body {margin: 0; padding: 0; min-width: 100%!important;}
.content {width: 100%; max-width: 800px;}
.header {padding: 40px 30px 20px 30px;}
.body {padding: 10px 20px;}
.footer {padding: 10px 0px;}
.orderitem{
border-collapse:separate; border-spacing:0 5px;
}
.orderitem thead tr th{border-bottom: 1px solid black; border-collapse:separate; border-spacing:5px 5px;}
</style>
</head>
string result = await engine.CompileRenderAsync("OrderCompleteEmail.cshtml", new { Name = "John Doe" });
Issue Analytics
- State:
- Created 6 years ago
- Reactions:8
- Comments:18 (2 by maintainers)
Top Results From Across the Web
Use localization in .cshtml razor template ...
It seems RazorLightEngine has not implemented localizaion. Check IViewLocalizer in cshtml template #166. – Edward. Dec 27, 2018 at 2:00.
Read more >Make an ASP.NET Core app's content localizable
The following code example shows how to wrap the string "About Title" for localization ... Mvc.Localization @inject IViewLocalizer Localizer ...
Read more >Globalization and localization in ASP.NET Core
Culture: A language and, optionally, a region. Neutral culture: A culture that has a specified language, but not a region (for example "en",...
Read more >Adding Localisation to an ASP.NET Core application
X. By default you would define a number of .resx resource files in ... MyView.fr.cshtml) and inject the IViewLocalizer , to allow you...
Read more >ASP.NET Core Localization and Internationalization
Then add the following line to the _ViewImports.cshtml. @using Microsoft.AspNetCore.Mvc.Localization @inject IViewLocalizer Localizer. This will automatically ...
Read more >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
@toddams I have the same problem as OP. I have used the extension method above and it doesn’t work. Usages of ‘Localizer[]’ throws null reference exception. Using RazorLight 2.0 beta 1.
If I do
serviceProvider.GetService<IViewLocalizer>()
I see that ViewLocalizer is correctly registered.I don’t know if this is still an issue. I couldn’t get the localizer to inject into the view. I have created a workaround by injecting the localizer into the model and exposing it as a model property for use in the view. Works fine for my purposes.
Minimal implementation here: https://github.com/Ric43/RazorLightPoC