0

这是多语言的代码:

    using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Reflection;
using System.Resources;
using System.Text;
using System.Threading;
using System.Windows.Forms;

namespace WindowsFormsMultiLanguage
{
    public partial class Form1 : Form
    {
        ResourceManager m_resourceManger;
        public Form1()
        {
            InitializeComponent();

            m_resourceManger = new ResourceManager("WindowsFormsMultiLanguage.Localization", Assembly.GetExecutingAssembly());
            // Init UICulture to CurrentCulture
            Thread.CurrentThread.CurrentUICulture = Thread.CurrentThread.CurrentCulture;
            // Init Controls
            UpdateUIControls();
        }



        private void UpdateUIControls()
        {
            try
            {
                if (m_resourceManger != null)
                {

                    this.label1.Text = m_resourceManger.GetString("test1");
                    this.label2.Text = m_resourceManger.GetString("test2");
                }
            }
            catch (System.Exception e)
            {
                MessageBox.Show(e.Message);
            }
        }
        private void OnLanguageChange(object sender, EventArgs e)
        {
            RadioButton radioButton = sender as RadioButton;
            string culture = string.Empty;

            switch (radioButton.Text)
            {

                case "French - France (fr-FR)":
                    culture = "fr-FR";
                    break;

                case "U.S. English (en-US)":
                    culture = "en-US";
                    break;

            }

            // This is used for the language of the user interface
            Thread.CurrentThread.CurrentUICulture = new System.Globalization.CultureInfo(culture);

            Thread.CurrentThread.CurrentCulture = new System.Globalization.CultureInfo(culture);
        }

    }


}

我收到一条错误消息,此时无法解析资源项 test&, test2: this.label1.Text = m_resourceManger.GetString("test1"); this.label2.Text = m_resourceManger.GetString("test2");

我添加了 2 个资源文件 1 个用于英语,第二个用于法语,我不知道是什么错误..

![在此处输入图像描述][1] ![在此处输入图像描述][2]

4

1 回答 1

0

可能是资源Localization不在命名空间Localization中,因此构造函数的参数错误"Localization.Localization"ResourceManager我只是在这里猜测,因为没有足够的信息来发现问题。

编辑:我下载了该项目,将其降级为我使用的 Visual Studio 2008(在 .sln 中将数字 11 更改为 10,在 .csproj 中的两个位置将 4.0 更改为 3.5),并删除了对组件 Microsoft.CSharp 的引用。该项目已构建并且我运行它并且没有错误。函数UpdateUIControls在开始时只调用一次,但在我更改语言时没有。UpdateUIControls我从末尾复制了两条主线,OnLanguageChange然后当我更改语言时,文本正在发生变化。

所以,我看不出有什么问题。问题出在其他地方。

于 2012-11-02T10:56:59.667 回答