我正在使用来自 insert... resx 文件和附属程序集的字符串资源。你确定你的文件命名正确吗?
资源 1.resx:
<!-- snip-->
<data name="foo" xml:space="preserve">
<value>bar</value>
</data>
Resource1.FR-fr.resx
<--! le snip -->
<data name="foo" xml:space="preserve">
<value>le bar</value>
</data>
Class1.cs:
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Globalization;
using System.IO;
using System.Threading;
namespace Frankenstein
{
public class Class1
{
struct LocalizedCallback
{
private WaitCallback localized;
public LocalizedCallback(WaitCallback user)
{
var uiCult = Thread.CurrentThread.CurrentUICulture;
// wrap
localized = (state) =>
{
var tp = Thread.CurrentThread;
var oldUICult = tp.CurrentUICulture;
try
{
// set the caller thread's culture for lookup
Thread.CurrentThread.CurrentUICulture = uiCult;
// call the user-supplied callback
user(state);
}
finally
{
// let's restore the TP thread state
tp.CurrentUICulture = oldUICult;
}
};
}
public static implicit operator WaitCallback(LocalizedCallback me)
{
return me.localized;
}
}
public static void Main(string[] args)
{
AutoResetEvent evt = new AutoResetEvent(false);
WaitCallback worker = state =>
{
Console.Out.WriteLine(Resource1.foo);
evt.Set();
};
// use default resource
Console.Out.WriteLine(">>>>>>>>>>{0}", Thread.CurrentThread.CurrentUICulture);
Console.Out.WriteLine("without wrapper");
ThreadPool.QueueUserWorkItem(worker);
evt.WaitOne();
Console.Out.WriteLine("with wrapper");
ThreadPool.QueueUserWorkItem(new LocalizedCallback(worker));
evt.WaitOne();
// go froggie
Thread.CurrentThread.CurrentUICulture = CultureInfo.GetCultureInfo("FR-fr");
Console.Out.WriteLine(">>>>>>>>>>{0}", Thread.CurrentThread.CurrentUICulture);
Console.Out.WriteLine("without wrapper");
ThreadPool.QueueUserWorkItem(worker);
evt.WaitOne();
Console.Out.WriteLine("with wrapper");
ThreadPool.QueueUserWorkItem(new LocalizedCallback(worker));
evt.WaitOne();
}
}
}
输出:
>>>>>>>>>>en-US
without wrapper
bar
with wrapper
bar
>>>>>>>>>>fr-FR
without wrapper
bar
with wrapper
le bar
Press any key to continue . . .
这样做的原因是 Resource1.Culture 属性始终设置为 null,因此它回退到默认值 (IE Thread.CurrentThread.UICulture)。
为了证明这一点,编辑 Resource1.Designer.cs 文件并从类中删除以下属性:
//[global::System.Diagnostics.DebuggerNonUserCodeAttribute()]
然后在 Resource.Culture 属性访问器和 foo 属性访问器中设置断点,并启动调试器。
干杯,弗洛里安