How to use string interpolation in a resource file?

不羁岁月 提交于 2019-12-01 15:57:23

问题


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
}

The problem is that the return says "Hello {userName}" instead of "Hello Toto".


回答1:


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);
}

Update

You can add as many parameters as you want. For instance:

Hello {0}, Confirm your email: {1}

And then you can use it as:

string.Format(Resource.WebResource.EmailConfirmation
    , userName
    , HtmlEncoder.Default.Encode(link))


来源:https://stackoverflow.com/questions/48845242/how-to-use-string-interpolation-in-a-resource-file

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!