[英]Use localized Razor pages as e-mail templates
I am developing a .NET Core project where I send Razor pages as e-mail templates.我正在开发一个 .NET Core 项目,我在其中将 Razor 页面作为电子邮件模板发送。 I followed this tutorial: https://scottsauber.com/2018/07/07/walkthrough-creating-an-html-email-template-with-razor-and-razor-class-libraries-and-rendering-it-from-a-net-standard-class-library/ The only thing I couldn't find was how to send a localized e-mail by passing the needed language as a parameter (example):
我跟着这个教程: https : //scottsauber.com/2018/07/07/walkthrough-creating-an-html-email-template-with-razor-and-razor-class-libraries-and-rendering-it-from -a-net-standard-class-library/我唯一找不到的是如何通过将所需语言作为参数传递来发送本地化电子邮件(示例):
public async Task<string> RenderViewToStringAsync<TModel>(string viewName, TModel model, string lang)
I found some stuff online about POs
and resx
files but everything required a Startup
implementation.我在网上找到了一些关于
POs
和resx
文件的内容,但一切都需要Startup
实现。 The razor pages is not the main project, the main project is just an API, while the front-end is handled by another Angular project. razor pages 不是主项目,主项目只是一个 API,而前端由另一个 Angular 项目处理。
How can I implement localization in a Razor Class Library without a Startup file?如何在没有启动文件的情况下在 Razor 类库中实现本地化?
This is the project files这是项目文件
I see at least two options for you:我看到至少有两个选项可供您选择:
Optoin 1 - Use different resource files for different cultures Optoin 1 - 针对不同的文化使用不同的资源文件
Follow these steps:按着这些次序:
In API project, register IStringLocalizerFactory
and IStringLocalizer<>
:在 API 项目中,注册
IStringLocalizerFactory
和IStringLocalizer<>
:
services.AddSingleton<IStringLocalizerFactory, ResourceManagerStringLocalizerFactory>(); services.AddScoped(typeof(IStringLocalizer<>), typeof(StringLocalizer<>)); services.AddScoped<IRegisterAccountService, RegisterAccountService>(); services.AddScoped<IRazorViewToStringRenderer, RazorViewToStringRenderer>();
Create a Resources.Resx file in Razor View Library and set its custom tool to PublicResXFileCodeGenerator
.在 Razor 视图库中创建一个 Resources.Resx 文件,并将其自定义工具设置为
PublicResXFileCodeGenerator
。 Then for each language, create the resource file like Resources.fa-IR.Resx
and clear the custom tool to not generate code for the language files.然后对于每种语言,创建资源文件(如
Resources.fa-IR.Resx
并清除自定义工具以不为语言文件生成代码。 Then add resource name and value, for example for fa-IR
:然后添加资源名称和值,例如
fa-IR
:
Name Value Comment ========================================= Welcome خوش آمدید
Inject string localizer to the views that you want:将字符串定位器注入您想要的视图:
@using Microsoft.Extensions.Localization @inject IStringLocalizer<RazorHtmlEmails.RazorClassLib.SharedResources> SR
In above example, RazorHtmlEmails.RazorClassLib
is namespace of the resource.在上面的例子中,
RazorHtmlEmails.RazorClassLib
是资源的命名空间。
Use SR["resource key in resource file"]
whenever you want to show a string from resource file:每当您想显示资源文件中的字符串时,请使用
SR["resource key in resource file"]
:
@SR["Welcome"]
Add culture as parameter to RenderViewToStringAsync
of IRazorViewToStringRenderer
:将文化作为参数添加到
IRazorViewToStringRenderer
RenderViewToStringAsync
:
Task<string> RenderViewToStringAsync<TModel> (string viewName, TModel model, string culture);
Add culture to implementation of RenderViewToStringAsync
in RazorViewToStringRenderer
:将文化添加到
RazorViewToStringRenderer
的RenderViewToStringAsync
实现:
public async Task<string> RenderViewToStringAsync<TModel> (string viewName, TModel model, string culture) { Thread.CurrentThread.CurrentCulture = CultureInfo.GetCultureInfo(culture); Thread.CurrentThread.CurrentUICulture = CultureInfo.GetCultureInfo(culture); ...
Use it:用它:
string body = await _razorViewToStringRenderer.RenderViewToStringAsync( "/Views/Emails/ConfirmAccount/ConfirmAccountEmail.cshtml", confirmAccountModel, "fa-IR");
Option 2 - Use different cshtml file for different cultures选项 2 - 为不同的文化使用不同的 cshtml 文件
If you don't want to use resource files and you want to have different cshtml files for different cultures, just use naming convention.如果您不想使用资源文件,并且希望针对不同文化使用不同的 cshtml 文件,只需使用命名约定即可。 For example create a
template.fa-IR.cshtml
for Persian language and then when rendering, use that view:例如,为波斯语创建一个
template.fa-IR.cshtml
,然后在渲染时使用该视图:
string body = await _razorViewToStringRenderer.RenderViewToStringAsync(
"/Views/Emails/ConfirmAccount/ConfirmAccountEmail.fa-IR.cshtml",
confirmAccountModel);
As an addition to the accepted answer, this is how you can have the translation effect from the resources in the e-mail subject:作为已接受答案的补充,您可以通过以下方式从电子邮件主题中的资源获得翻译效果:
Edit RegisterAccountService
class like this:像这样编辑
RegisterAccountService
类:
private readonly IStringLocalizer<RazorHtmlEmails.RazorClassLib.SharedResources> _localizer;
Replace RazorHtmlEmails.RazorClassLib.SharedResources
with your namespace, then:将
RazorHtmlEmails.RazorClassLib.SharedResources
替换为您的命名空间,然后:
public RegisterAccountService(IRazorViewToStringRenderer razorViewToStringRenderer, IStringLocalizer<RazorHtmlEmails.RazorClassLib.SharedResources> localizer)
{
_razorViewToStringRenderer = razorViewToStringRenderer;
_localizer = localizer;
}
and apply:并申请:
SendEmail(toAddresses, "donotreply@contoso.com", _localizer["My_Email_Subject_Translation"].Value, body);
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.