C# NET中的多语言应用程序

C# NET中的多语言应用程序,c#,.net,multilingual,C#,.net,Multilingual,我是C的新手,虽然我有几年OOP的经验 在设计阶段的一个项目中,我被选中研究如何在C语言下实现多语言。对于该项目,我目前正在使用Microsoft Visual Studio 2010 Express 我到现在为止都做了些什么 我读了几篇关于这个主题的文章,比如,或者,但是在运行我的演示时仍然有问题 我的演示在没有资源文件的情况下运行,资源文件包含所有文化的硬编码文本ar SA、en UK、de de、no、sv SE和tr。我在尝试使用资源文件时遇到了问题 我是如何试图解决它的 为了解决这个问

我是C的新手,虽然我有几年OOP的经验

在设计阶段的一个项目中,我被选中研究如何在C语言下实现多语言。对于该项目,我目前正在使用Microsoft Visual Studio 2010 Express

我到现在为止都做了些什么

我读了几篇关于这个主题的文章,比如,或者,但是在运行我的演示时仍然有问题

我的演示在没有资源文件的情况下运行,资源文件包含所有文化的硬编码文本ar SA、en UK、de de、no、sv SE和tr。我在尝试使用资源文件时遇到了问题

我是如何试图解决它的

为了解决这个问题,我为每个区域性创建了一个名为resource..resx的资源文件。并非所有教程都是为MS VS 2010编写的,因此参考资料中没有关于组合框的信息,我在默认情况下没有生成代码,而没有生成内部代码-

现在,我有六种语言的资源文件:

Resource.ar-SA.resx
Resource.de-DE.resx
etc.
我确定了IDE生成的*.resources文件,并根据所选语言确定了每个文件的路径

当我运行代码时,我被消息卡住了

MainWindow:DetermineResourceManager(): Exception: System.BadImageFormatException: Im Modul wurde ein Assemblymanifest erwartet. (Ausnahme von HRESULT: 0x80131018)
   bei System.Reflection.RuntimeAssembly.nLoadFile(String path, Evidence evidence)
   bei System.Reflection.Assembly.LoadFile(String path)
   bei WpfApplication1.MainWindow.DetermineResourceManager() in C:\Documents and Settings\z002zatp\My Documents\Visual Studio 2010\Projects\WpfApplication1\WpfApplication1\MainWindow.xaml.cs:Zeile 136.
因此,缺少所谓的程序集清单

我的问题

让IDE正确地创建程序集清单,我该怎么做

代码

下面标记了有问题的行。我检查并发现C:\Documents and Settings\z002zatp\My Documents\Visual Studio 2010\Projects\WPFAApplication1\WPFAApplication1\obj\x86\Debug中的文件确实存在

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;

using System.Globalization; // class CultureInfo
using System.Resources;     // class Thread
using System.Threading;     // 

namespace WpfApplication1
{
    public partial class MainWindow : Window
    {
        static string[,] culture = new string[,] { { "ar-SA", "Arabic", "Saudi Arabia" }, { "de-DE", "German", "Germany" },
                                               { "en-UK", "English", "United Kingdom"}, // { "en-US", "English", "United States of America"}, 
                                               { "no", "Norwegian (Bokmål)", "Norway"}, { "sv-SE", "Swedish", "Sweden"},
                                               { "tr", "Turkish", null } };
        static string[] label = new string[] { "Caption", "Message" };

        static short selectedCulture = 1;   // de-DE
        ResourceManager rm = null;

        public MainWindow()
        {
            InitializeComponent();
            textBox1.AppendText(culture[selectedCulture, 1]);
            Keyboard.Focus(btnMessage);
        }

        private void btnMessage_Click(object sender, RoutedEventArgs rea)
        {
            const string METHOD = "MainWindow:btnMessage_Click: ";

            try
            {
                DetermineResourceManager();

                MessageBox.Show(rm.GetString(label[1]), rm.GetString(label[0]),
                    MessageBoxButton.OK, MessageBoxImage.None, MessageBoxResult.None);
            }
            catch (MissingManifestResourceException mmre)
            {
                MessageBox.Show(METHOD + "Exception: " + mmre, "Error");
            }
            catch (Exception e)
            {
                MessageBox.Show(METHOD + "Unexpected exception: " + e, "Error");
            }

            SwitchLanguage();
            textBox1.Text = culture[selectedCulture, 1];
        }

        private void DetermineResourceManager()
        {
            const string METHOD = "MainWindow:DetermineResourceManager(): ";
            string path = "C:\\Documents and Settings\\z002zatp\\My Documents\\Visual Studio 2010\\Projects\\" +
                "WpfApplication1\\WpfApplication1\\obj\\x86\\Debug\\";
            string resource = "WpfApplication1.Resource." + culture[selectedCulture, 0] + ".resources"; 

            System.Reflection.Assembly assembly = null;
            //MessageBox.Show(METHOD + "Resource to be loaded: " + path + resource);

            try
            {
   >>>          assembly = System.Reflection.Assembly.LoadFile(path + resource);  <<< ERROR OCCURS HERE

                CultureInfo ci = new CultureInfo(culture[selectedCulture, 0]);

                Thread.CurrentThread.CurrentCulture = ci;

                rm = new ResourceManager(resource, assembly);
            }
            catch (Exception e)
            {
                MessageBox.Show(METHOD + "Exception: " + e);
                throw e;
            }
        }

        private void SwitchLanguage()
        {
            if (++selectedCulture >= culture.Length/3)
                selectedCulture = 0;
        }
    }
}

以下是我从另一个网站获得帮助后得出的问题的解决方案:

        /// <summary>
    /// Interaktionslogik für MainWindow.xaml
    /// </summary>
    /// 

    public partial class MainWindow : Window
    {
        static string[,] culture = new string[,] { { "ar-SA", "Arabic", "Saudi Arabia" }, { "de-DE", "German", "Germany" },
                                                   { "en-GB", "English", "United Kingdom"}, // { "en-US", "English", "United States of America"}, 
                                                   { "no", "Norwegian (Bokmål)", "Norway"}, { "sv-SE", "Swedish", "Sweden"},
                                                   { "tr", "Turkish", null } };
        static string[] label = new string[] { "Caption", "Message" };

        static short selectedCulture = 1;   // de-DE
        ResourceManager rm = null;

        public MainWindow()
        {
            InitializeComponent();
            textBox1.AppendText(culture[selectedCulture, 1]);
            Keyboard.Focus(btnMessage);
        }

        private void btnMessage_Click(object sender, RoutedEventArgs rea)
        {
            const string METHOD = "MainWindow:btnMessage_Click: ";

            try
            {
                DetermineResourceManager();

                if (culture[selectedCulture, 0].Substring(0, 2) != "ar")
                {
                    MessageBox.Show(rm.GetString(label[1]), rm.GetString(label[0]),
                        MessageBoxButton.OK, MessageBoxImage.None, MessageBoxResult.None);
                }
                else
                {
                    MessageBox.Show(rm.GetString(label[1]), rm.GetString(label[0]),
                        MessageBoxButton.OK, MessageBoxImage.None, MessageBoxResult.None,
                        MessageBoxOptions.RightAlign | MessageBoxOptions.RtlReading);
                }
            }
            catch (MissingManifestResourceException mmre)
            {
                MessageBox.Show(METHOD + "Exception: " + mmre, "Error");
            }
            catch (Exception e)
            {
                MessageBox.Show(METHOD + "Unexpected exception: " + e, "Error");
            }

            SwitchLanguage();
            textBox1.Text = culture[selectedCulture, 1];
        }

        private void DetermineResourceManager()
        {
            const string METHOD = "MainWindow:DetermineResourceManager(): ";

            string resource = "WpfApplication1.Resource." + culture[selectedCulture, 0] + ".resources";

            try
            {
                Thread.CurrentThread.CurrentUICulture = new CultureInfo(culture[selectedCulture, 0], false);

                rm = new ResourceManager("WpfApplication1.Resource", Assembly.GetExecutingAssembly());
            }
            catch (Exception e)
            {
                MessageBox.Show(METHOD + ", unexpected exception: " + e);
                throw e;
            }
        }

        private void SwitchLanguage()
        {
            if (++selectedCulture >= culture.Length/3)
                selectedCulture = 0;
        }
    }
}

请看这些文章:

…还有一些例子:


您可能需要将throw e更改为仅插入determinateResourceManager。throw e将损坏异常的堆栈traceHmm。你能告诉我们你想达到什么目标吗?你只是想切换语言吗?