76

C#6.0 have a string interpolation - a nice feature to format strings like:

 var name = "John";
 WriteLine($"My name is {name}");

The example is converted to

 var name = "John";
 WriteLine(String.Format("My name is {0}", name));

From the localization point of view, it is much better to store strings like :

"My name is {name} {middlename} {surname}" 

than in String.Format notation:

"My name is {0} {1} {2}"

How to use the string interpolation for .NET localization? Is there going to be a way to put $"..." to resource files? Or should strings be stored like "...{name}" and somehow interpolated on fly?

P.S. This question is NOT about "how to make string.FormatIt extension" (there are A LOT of such libraries, SO answers, etc.). This question is about something like Roslyn extension for "string interpolation" in "localization" context (both are terms in MS .NET vocabulary), or dynamic usage like Dylan proposed.

4
  • Can you clarify your question? Are you asking how to do this in C# 6.0, or how to do this in earlier versions of C#? Commented Mar 16, 2015 at 1:45
  • I wouldn't store these strings on a file to read in later. You should still make a class that stores these fields. Read them in. and then pass them through the string interpolation locally. Commented Mar 16, 2015 at 1:56
  • @nintendojunkie I'm asking about C# 6.0. About if it possible to use C# 6.0 string interpolation for localization and if possible how to do it. Commented Mar 16, 2015 at 2:02
  • 1
    No response yet, only that I'm working on a fork of Roslyn to provide TranslatableStrings Commented Jun 19, 2015 at 15:24

9 Answers 9

36

An interpolated string evaluates the block between the curly braces as a C# expression (e.g. {expression}, {1 + 1}, {person.FirstName}).

This means that the expressions in an interpolated string must reference names in the current context.

For example this statement will not compile:

var nameFormat = $"My name is {name}"; // Cannot use *name*
                                       // before it is declared
var name = "Fred";
WriteLine(nameFormat);

Similarly:

class Program
{
    const string interpolated = $"{firstName}"; // Name *firstName* does not exist
                                                // in the current context
    static void Main(string[] args)
    {
        var firstName = "fred";
        Console.WriteLine(interpolated);
        Console.ReadKey();
    }
}

To answer your question:

There is no current mechanism provided by the framework to evaluate interpolated strings at runtime. Therefore, you cannot store strings and interpolate on the fly out of the box.

There are libraries that exist that handle runtime interpolation of strings.

Sign up to request clarification or add additional context in comments.

3 Comments

I agree that interpolation is not the same as format("...{name}") that many libraries do. Your post just states that it is done on another level. It is true. But .NET, C# , Roslyn provides you with such rich possibilities, that I disagree on "you cannot store strings and interpolate on the fly". You definitely CAN. The questions are: how to do it, had anyone implemented it, does someone have ideas, is it worth investing time to implement it at some level. Etc.
I believe you are correct in that it is possible to write a roslyn extension for runtime string interpolation. I have updated my answer to reflect that the framework does not provide support.
Interpolation libraries: My favorite is HenriFormatter, outlined on haacked.com/archive/2009/01/14/named-formats-redux.aspx. It will allow you to write WriteLine("My name is {name}.".HenriFormat(new {name = "fred"})).
15

According to this discussion on the Roslyn codeplex site, string interpolation will likely not be compatible with resource files (emphasis mine):

String interpolation could be neater and easier to debug than either String.Format or concatenation...

Dim y = $"Robot {name} reporting
{coolant.name} levels are {coolant.level}
{reactor.name} levels are {reactor.level}"

However, this example is fishy. Most professional programmers won't be writing user-facing strings in code. Instead they'll be storing those strings in resources (.resw, .resx or .xlf) for reasons of localization. So there doesn't seem much use for string interpolation here.

1 Comment

I've seen the post. It looks more like discussion than like specs or developers vision. It was the time, If you read the post, when string interpolation formatting hadn't been defined. You can find another posts on the very same forum discussing how localization benefits from string interpolation
8

Assuming that your question is more about how to localise interpolated strings in your source code, and not how to handle interpolated string resources...

Given the example code:

var name = "John";
var middlename = "W";
var surname = "Bloggs";
var text = $"My name is {name} {middlename} {surname}";
Console.WriteLine(text);

The output is obviously:

My name is John W Bloggs

Now change the text assignment to fetch a translation instead:

var text = Translate($"My name is {name} {middlename} {surname}");

Translate is implemented like this:

public static string Translate(FormattableString text)
{
    return string.Format(GetTranslation(text.Format),
        text.GetArguments());
}

private static string GetTranslation(string text)
{
    return text; // actually use gettext or whatever
}

You need to provide your own implementation of GetTranslation; it will receive a string like "My name is {0} {1} {2}" and should use GetText or resources or similar to locate and return a suitable translation for this, or just return the original parameter to skip translation.

You will still need to document for your translators what the parameter numbers mean; the text used in the original code string doesn't exist at runtime.

If, for example, in this case GetTranslation returned "{2}. {0} {2}, {1}. Don't wear it out." (hey, localisation is not just about language!) then the output of the full program would be:

Bloggs.  John Bloggs, W.  Don't wear it out.

Having said this, while using this style of translation is easy to develop, it's hard to actually translate, since the strings are buried in the code and only surface at runtime. Unless you have a tool that can statically explore your code and extract all the translatable strings (without having to hit that code path at runtime), you're better off using more traditional resx files, since they inherently give you a table of text to be translated.

Comments

6

As already said in previous answers: you currently cannot load the format string at runtime (e.g. from resource files) for string interpolation because it is used at compile time.

If you don't care about the compile time feature and just want to have named placeholders, you could use something like this extension method:

public static string StringFormat(this string input, Dictionary<string, object> elements)
{
    int i = 0;
    var values = new object[elements.Count];
    foreach (var elem in elements)
    {
        input = Regex.Replace(input, "{" + Regex.Escape(elem.Key) + "(?<format>[^}]+)?}", "{" + i + "${format}}");
        values[i++] = elem.Value;
    }
    return string.Format(input, values);
}

Be aware that you cannot have inline expressions like {i+1} here and that this is not code with best performance.

You can use this with a dictionary you load from resource files or inline like this:

var txt = "Hello {name} on {day:yyyy-MM-dd}!".StringFormat(new Dictionary<string, object>
            {
                ["name"] = "Joe",
                ["day"] = DateTime.Now,
            });

3 Comments

Thanks for the answer. But it doesn't answer the question. Unfortunately you committed your time answering this without spending your time carefully and thoughtfully reading around.
@MajesticRa He answered your question, and was even so kind as to give an example of how you could achieve it. If you want a different answer, ask a more accurate question. You asked if there is going to be a way to use string interpolation for localization. The answer is no, especially since it seems to be a compiler feature rather than a language feature (i.e. syntactic sugar). Should strings be stored like "{name}" and interpolated on the fly? Yes. How? stb suggested a reasonable method above.
Because both "string interpolation" and "localization" are terms with the clear meaning in MS .NET vocabulary. But even despite of that, there already WHERE answers with formatting extension library and others proposing this in one or another way. All with discussions describing why this question is not about it. There are TONS of "How to make string format extension" QA on SO. So It is clearly out of context. But OK, as you say, I updated the initial post with the clarification...
6

String interpolation is difficult to combine with localization because the compiler prefers to translate it to string.Format(...), which does not support localization. However, there is a trick that makes it possible to combine localization and string interpolation; it is described near the end of this article.

Normally string interpolation is translated to string.Format, whose behavior cannot be customized. However, in much the same way as lambda methods sometimes become expression trees, the compiler will switch from string.Format to FormattableStringFactory.Create (a .NET 4.6 method) if the target method accepts a System.FormattableString object.

The problem is, the compiler prefers to call string.Format if possible, so if there were an overload of Localized() that accepted FormattableString, it would not work with string interpolation because the C# compiler would simply ignore it [because there is an overload that accepts a plain string]. Actually, it's worse than that: the compiler also refuses to use FormattableString when calling an extension method.

It can work if you use a non-extension method. For example:

static class Loca
{
    public static string lize(this FormattableString message)
        { return message.Format.Localized(message.GetArguments()); }
}

Then you can use it like this:

public class Program
{
    public static void Main(string[] args)
    {
        Localize.UseResourceManager(Resources.ResourceManager);

        var name = "Dave";
        Console.WriteLine(Loca.lize($"Hello, {name}"));
    }
}

It's important to realize that the compiler converts the $"..." string into an old-fashioned format string. So in this example, Loca.lize actually receives "Hello, {0}" as the format string, not "Hello, {name}".

2 Comments

This needs more upvotes. I like the new $"" format, but was disappointed by it not working with resources. Using your example I was able to add a GetFormattedString(FormattableString str) to our resource lookup code that passes in the str.Format to GetString(string str), then returns String.Format(translatedFormat, str.GetArguments()). Now I can call that function with $"My nicely formatted strings" and get a translated + formatted string back.
This is the best answer to the question. It is my first time to get know about FormattableString.
4

Using the Microsoft.CodeAnalysis.CSharp.Scripting package you can achieve this.

You will need to create an object to store the data in, below a dynamic object is used. You could also create an specific class with all the properties required. The reason to wrap the dynamic object in a class in described here.

public class DynamicData
{
    public dynamic Data { get; } = new ExpandoObject();
}

You can then use it as shown below.

var options = ScriptOptions.Default
    .AddReferences(
        typeof(Microsoft.CSharp.RuntimeBinder.RuntimeBinderException).GetTypeInfo().Assembly,
        typeof(System.Runtime.CompilerServices.DynamicAttribute).GetTypeInfo().Assembly);

var globals = new DynamicData();
globals.Data.Name = "John";
globals.Data.MiddleName = "James";
globals.Data.Surname = "Jamison";

var text = "My name is {Data.Name} {Data.MiddleName} {Data.Surname}";
var result = await CSharpScript.EvaluateAsync<string>($"$\"{text}\"", options, globals);

This is compiling the snippet of code and executing it, so it is true C# string interpolation. Though you will have to take into account the performance of this as it is actually compiling and executing your code at runtime. To get around this performance hit if you could use CSharpScript.Create to compile and cache the code.

1 Comment

I'm not sure if it is practical but at least it is funny and answers the question! I hope @Monoman will ever finish the Roslyn extension
4

The C# 6.0 string interpolation won't help you if the format string is not in your C# source code. In that case, you will have to use some other solution, like this library.

2 Comments

I know, that there are several... I could say, many such libraries and extensions. But string interpolation is another beast, as @Romoku states in his answer. It is interesting if this tool could be used.
Or my favorite, HenriFormatter, outlined on haacked.com/archive/2009/01/14/named-formats-redux.aspx. It will allow you to write WriteLine("My name is {name}.".HenriFormat(new {name = "fred"})).
1

If we use interpolation then we are thinking in terms of methods, not constants. In that case we could define our translations as methods:

public abstract class InterpolatedText
{
    public abstract string GreetingWithName(string firstName, string lastName);
}

public class InterpolatedTextEnglish : InterpolatedText
{
    public override string GreetingWithName(string firstName, string lastName) =>
        $"Hello, my name is {firstName} {lastName}.";
}

We can then load an implementation of InterpolatedText for a specific culture. This also provides a way to implement fallback, as one implementation can inherit from another. If English is the default language and other implementations inherit from it, there will at least be something to display until a translation is provided.

This seems a bit unorthodox, but offers some benefits:

Primarily, the string used for interpolation is always stored in a strongly-typed method with clearly-specified arguments.

Given this: "Hello, my name is {0} {1}" can we determine that the placeholders represent first name and last name in that order? There will always be a method which matches values to placeholders, but there's less room for confusion when the interpolated string is stored with its arguments.

Similarly, if we store our translation strings in one place and use them in another, it becomes possible to modify them in a way that breaks the code using them. We can add {2} to a string which will be used elsewhere, and that code will fail at runtime.

Using string interpolation this is impossible. If our translation string doesn't match the available arguments it won't even compile.


There are drawbacks, although I see difficulty in maintaining any solution.

The greatest is portability. If your translation is coded in C# and you switch, it's not the easiest thing to export all of your translations.

It also means that if you wish to farm out translations to different individuals (unless you have one person who speaks everything) then the translators must modify code. It's easy code, but code nonetheless.

Comments

0

Interpolated strings can not refactored out from their (variable) scope because of using of the embedded variables in them.

The only way to relocate the string literal part is passing the scope bound variables as parameter to an other location, and mark their position in the string with special placeholders. However this solution is already "invented" and out there:

string.Format("literal with placeholers", parameters);

or some of advanced library (interpolating runtime), but using the very same concept (passing parameters).

Then you can refactor out the "literal with placeholers" to a resource.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.