Search code examples
c#asp.net-mvclocalizationresourcesresx

How do you fill in a resource string at run-time?


I have an application I'd like to localise. However, the strings to be localised occasionally contain parts I'd like to provide at runtime, such as action links.

For example, I have a string like this: Please <a href="/help">click here</a> for help. I can't just split it into two resources since in different languages it'll be in a different location.

Is there any way to do this or should I stick to hard-coding the link to the controller/action in the resource itself?


Solution

  • You should modify your string this way:

     string.Format("Please {0]click here{1} for help", "<a href="/help">","</a>")
    

    Now, the first string "Please {0]click here{1} for help" can be easily translated/localized, even if the order of your text changes:

    "Für Hilfe klicken Sie bitte {0}hier{1}."