.NET 中的多语言应用程序

发布于 2024-11-30 05:46:33 字数 5269 浏览 0 评论 0原文

尽管我有多年的 OOP 经验,但我是 C# 的新手。

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

到目前为止我所做的事情

我阅读了有关此主题的几篇文章,例如 System.Resources 常见问题解答.NET 中的多语言应用程序C# 多语言支持,但是运行我的演示仍然遇到问题。

我的演示在没有包含所有文化(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\WpfApplication1\WpfApplication1\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;
        }
    }
}

I am a newbie for C#, although I have several years of experience with OOP.

For a project in the design phase, I was chosen to find out how multilinguality can be done under C#. For the project, I am currently using Microsoft Visual Studio 2010 Express...

What I have done till now

I read about several articles on this subject, like System.Resources FAQ, Multilingual Applications in .NET or C# multilingual support, but still have problems getting my demo to run.

My demo worked without resource files with hard-coded text for all cultures (ar-SA, en-UK, de-DE, no, sv-SE, and tr). I run into problems when I tried it with resource files.

How I tried to solve it

To solve it, I created a resource file for each culture, called Resource..resx. All the tutorials were not written for MS VS 2010, so there was no information about the combo box on the resources, which I had left at default ("no code generation") instead of "Internal". :-(

Now, I have the resource files for the six languages:

Resource.ar-SA.resx
Resource.de-DE.resx
etc.

I determined the IDE-generated *.resources files and and determine the path for each of it according to the selected language.

When I run the code, I get stuck with the message

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.

So, a so-called assembly manifest is missing.

My question

What have I to do let the IDE create the assembly manifest properly?

The code

The problematic line is marked below. I checked and found out that the files at C:\Documents and Settings\z002zatp\My Documents\Visual Studio 2010\Projects\WpfApplication1\WpfApplication1\obj\x86\Debug do exist.

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;
        }
    }
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

情释 2024-12-07 05:46:33

这是我在另一个网站的帮助下找到的问题的解决方案:

        /// <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;
        }
    }
}

Here is the solution for my question I reached after help from another site:

        /// <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;
        }
    }
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文