Multilingual Applications in .NET
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 开发者_JS百科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;
}
}
}
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;
}
}
}
Take a look at these articles:
http://msdn.microsoft.com/en-us/library/y99d1cd3(v=VS.100).aspx
http://msdn.microsoft.com/en-us/library/y99d1cd3(v=VS.90).aspx
...and some examples:
http://msdn.microsoft.com/en-us/vstudio/aa718334.aspx
精彩评论