I have a function which gets some text from an xml file like this:
var text = Translate("/foo/bar");
Now in the XML I want to put in string interpolation constants like this:
<foo>
<bar>Hi, {firstName} {lastName}!</bar>
</foo>
So, I want to do string interpolation on the result of the Translate-function, that would be something like:
var firstName = "John";
var lastName = "Smith";
var formattedText = $Translate("/foo/bar");
But that doesn't seem to work. Is there any way to to this, except reverting to string.Format ?
It could be nice if the dollar sign could be placed in front of anything that is a string, be it a variable, a string or a function, to do string interpolation.
It's worth mentioning that I don't have control over the code for the Translate-function, since it's in a third party component.
No, you can't do this. String interpolation is performed at compile-time. The compiler examines the interpolated string literal, finds the relevant format strings and converts them to {0}
etc. There's nothing similar to perform this at execution time.
See more on this question at Stackoverflow