question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

IViewLocalizer in cshtml template

See original GitHub issue

Hello 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:open
  • Created 6 years ago
  • Reactions:8
  • Comments:18 (2 by maintainers)

github_iconTop GitHub Comments

7reactions
olivierr91commented, Jun 19, 2018

@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.

            services.AddRazorLight(() => {
                return new RazorLightEngineBuilder()
                      .UseFilesystemProject(AppContext.BaseDirectory)
                      .UseMemoryCachingProvider()
                      .Build();
            });

If I do serviceProvider.GetService<IViewLocalizer>() I see that ViewLocalizer is correctly registered.

0reactions
Ric43commented, Aug 28, 2020

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

Read more comments on GitHub >

github_iconTop 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 >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found