首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > .NET > C# >

请问c#多语言有关问题

2013-06-19 
请教c#多语言问题直接上代码新建一dll工程代码如下using Systemusing System.Collections.Genericusing

请教c#多语言问题
直接上代码
新建一dll工程
代码如下

using System;
using System.Collections.Generic;
using System.Text;
using System.Threading;

namespace ClassLibrary1
{
    public class Class1
    {
        public delegate void Test();
        public Test test;
        public Class1()
        {
            ThreadPool.QueueUserWorkItem(new WaitCallback(test1));
        }

        private void test1(object sender)
        {
            while (true)
            {
                Thread.Sleep(10000);
                if (test != null)
                    test();
            }
        }

        public void test2()
        {
            if (test != null)
                test();
        }
    }
}


然后建立控制台程序,添加资源文件Localize.resx,添加字符串String1值:测试;在添加英文资源Localize.en.resx添加字符串String1值:Test.
控制台程序代码如下:
using System;
using System.Collections.Generic;
using System.Text;
using System.Threading;
using System.Resources;

namespace ConsoleApplication1
{
    class Program
    {
        private static ResourceManager m_resourceManager = new ResourceManager("ConsoleApplication1.Localize", System.Reflection.Assembly.GetExecutingAssembly());
        static void Main(string[] args)
        {
            Thread.CurrentThread.CurrentUICulture = System.Globalization.CultureInfo.GetCultureInfo("en");
            ClassLibrary1.Class1 c1 = new ClassLibrary1.Class1();
            c1.test = new ClassLibrary1.Class1.Test(test);

            c1.test2();

            Console.ReadLine();
            c1.test2();

            Console.ReadLine();
        }

        static void test()
        {


            Console.WriteLine(m_resourceManager.GetString("String1"));
        }
    }
}



运行后直接调用c1.test2()打印出来的字符串为英文,而ClassLibrary1十秒运行一次的test1打印出来为中文,请问有什么办法解决?
[解决办法]
CurrentUICulture = System.Globalization.CultureInfo.GetCultureInfo("en"); 
把这个写在委托里面。
[解决办法]
设置语言的代码必须写在每一个线程的开头,这是基本要求。
Thread.CurrentThread.CurrentUICulture 这明明就是对当前线程进行的设置

热点排行