如何在资源文件中使用字符串插值? [英] How to use string interpolation in a resource file?
问题描述
我想使用资源文件发送电子邮件.在我的资源文件中,我使用了一个变量"EmailConfirmation",其值为"Hello {userName} ..."
I would like to use a resource file to send an email. In my resource file I used a variable "EmailConfirmation" with the value "Hello {userName} ... "
在我的课堂上,我使用了:
In my class I used:
public static string message (string userName)
{
return Resource.WebResource.EmailConfirmation
}
问题在于返回值显示为"Hello {userName}",而不是"Hello Toto".
The problem is that the return says "Hello {userName}" instead of "Hello Toto".
推荐答案
您不能在资源的上下文中使用字符串插值.但是,您可以通过使用string.Format
实现所需的功能.将这样的内容写入您的资源文件:
You can't make use of string interpolation in the context of resources. However you could achieve that you want by making use of string.Format
. Write to your resource file something like this:
Hello {0}
,然后按如下所示使用它:
and then use it like below:
public static string message (string userName)
{
return string.Format(Resource.WebResource.EmailConfirmation, userName);
}
更新
您可以根据需要添加任意数量的参数.例如:
You can add as many parameters as you want. For instance:
Hello {0}, Confirm your email: {1}
然后您可以将其用作:
string.Format(Resource.WebResource.EmailConfirmation
, userName
, HtmlEncoder.Default.Encode(link))
这篇关于如何在资源文件中使用字符串插值?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!