在网络上跋涉之后(以及GitHub的来源和问题),我确实找到了描述基本相同问题的this SO post,以及"解决方案"对于它(SHIFT + ENTER,等于" \ r \ n",在Resx编辑器中)。这似乎是hackish,远远不够理想(我认为它会导致不一致的行终止。)
然而,在挖掘源代码以尝试重现它或查看是否存在已知问题时,我克隆了ms repo here。
git clone -b master https://github.com/aspnet/Localization.git
Master非常重要(开发人员不会编译。)它会对项目文件(我正在使用VS2017,v15.2)进行升级而不会出现问题。更改" samples \ LocalizationSample"成为启动项目。
我在using System.Text
的顶部添加Startup.cs
,然后在original line 66添加以下内容...
const string HELLO = "Hello";
var name = "John Doe";
var sb = new StringBuilder($"{name}\n");
// Similiar to what I am currently using that is not working.
// However, tmplt gets the expected value here in this cloned repo!
// Yet, after the AppendFormat call below, it is back to escaped "\n" ("\\n")
var tmplt = SR[HELLO]; // A
// In my code that started this rabbit hole, tmplt gets the
// following via my injected _localizer/resx.
//var tmplt = "{0}:\\n Score: {1}\\n Pct: {2}\\n\\n"; // B
// Comment out above tmplt lines, and uncomment this and it works as expected
// This is the string that I have in my resx
//var tmplt = "{0}:\n Score: {1}\n Pct: {2}\n\n"; // C
string subj = "Subject";
int score = 100;
int? pct = 100;
sb.AppendFormat(
tmplt, // A = "\n"!, B = "\\n" (expected), C = "\n" (known good)
subj,
score,
pct.Value
);
// A = "\\n" :-(
// B = "\\n"
// C = "\n"
var msg = sb.ToString();
var just_a_place_to_set_a_breakpoint = true;
然后我将Startup.es-ES.resx文件复制为Startup.en-US.resx
,然后更改新resx中单个键的值以匹配" C"中显示的字符串。上面(// C
)
我现在可以在just_a_place_to_set_a_breakpoint
设置断点并启动项目。
在此项目的本地化数据的换行符之前,能够正确获取值,而无需额外的转义字符!我还没弄清楚为什么在这个克隆/修改后的回购中有多大作用,但不是我原来的私人回购。
然而,一旦进入ToString
电话,它就会回到场景A和B中不需要的转义换行符(" \ n")! : - (
任何帮助都非常感激,我的眼球即将消失(我担心这很容易/记录在案,谷歌也没能把它搞砸。)
更新:我发现the branch that I presume should match我原始项目正在使用的Microsoft.Extensions.Localization
版本,v1.1.2 ......但无法构建。