C#6.0串插本地化 [英] C#6.0 string interpolation localization

查看:160
本文介绍了C#6.0串插本地化的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

C#6.0有一个串插 - 一个不错的功能,以格式字符串,如:

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}" 

比的String.Format符号:

than in String.Format notation:

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

如何使用字符串插值.NET本地化?是否有将是一个办法,把$......到资源文件?还是应该字符串存储,如... {名},不知怎么插入飞?

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?

推荐答案

这是内插字符串评估花括号之间的块为一个C#EX pression(如 {EX pression} {1 + 1} {person.FirstName}

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

这意味着EX pressions在插值字符串必须引用的名称在当前的背景下。

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

同样的:

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

要回答你的问题:

目前在运行时评估插值字符串框架提供无电流机制。因此,你不能存储字符串和插值飞开箱。

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.

这篇关于C#6.0串插本地化的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆