English 中文(简体)
Localization problem
原标题:

I need to make a form that have to suport two languages (for now). the two languages dont have the same look and also half of the form is not a like but it still have some similiarity beetwen them. what is the best way to deal with this problem in a case of two different languages and above? by the way, the program language that I use is C#.

10x

问题回答

First, configure your project default language, you do this in the Project Properties, in the Assembly Information dialog, there s a "Neutral Language" setting at the bottom.

Set this to be your "default" language, the "main" language if you wish.

Then, make sure the form as it is now, is in that language.

To start translating and changing the form to comply with a different language, first set the "Localizable" property of the form to true, and then change the Language property to your second (or third, fourth, etc.) language.

Once you have changed that, you can start making changes. Make sure you don t delete items on the form, instead just set them invisible. Deletion is done for all languages, but invisible will thus only be set for the current language.

Keep switching back and forth between the languages to make adjustments.

To test your program in a specific language, execute this at the start of your Main method:

Thread.CurrentThread.CurrentCulture = new CultureInfo("code of that other language");
Thread.CurrentThread.CurrentUICulture = new CultureInfo("code of that other language");

For instance, to test it with "Norwegian, Bokmål" language, which is my main language, the code you would use would be "nb-NO". To find the code you need to use, once you ve changed the language of your form to the language you want to localize for, and saved, a new file will be added to the solution explorer with the right name.

For instance, for Form1, the following files will be present:

Form1.cs
    Form1.designer.cs
    Form1.nb-NO.resx     <-- here s the localized content
    Form1.resx

Now, having done this, there s plenty of other things you need to be aware of when making a localized application, I suggest you go read other questions on SO and on the web with more information, like these:

I think a single global resource file for each language is better than having a resource file for each form for each language.
I recently did globalization of a winforms app with the help of the following link "Globalization of Windows Applications in 20 Minutes". Here are the steps for simplicity :

  1. Create a text file say "data.en-US.txt" in the solution (<filename>.<culture string>.txt) in the format below :

    Hello=Hello
    Welcome=Welcome

    This file "data.en-US.txt" is a resource text file in the English language. Create a similar file in your desired language say German, so its filename will be data.de-DE.txt

    Hello=Halo
    Welcome=Willikomen

    The first column are the labels which will be used as keys for referring the text.

  2. Create a "data-en-US.resource" file out of the text files by using the command "resgen.exe data.en-US.txt", Do this for all language files. Now in your solution you have 2 (or more, depending on the languages you want to support)
    data.<culture string>.resource files

  3. Click all the .resource files and set "Build Action" property to "Content".
  4. Click all the .resource files and set "Copy to Output Directory" property to "Copy Always / Copy If new".
  5. Get the localized string using the code below in any of the forms :
string label = "Hello";
string cultureString = "de-DE" // [or "en-US"]
string strResourcesPath = Application.StartupPath;
CultureInfo ci = new CultureInfo(cultureString); [ cultureString  represents your desired language]
ResourceManager rm = ResourceManager.CreateFileBasedResourceManager("data", strResourcesPath , null); ["data" is the first part of the filename]
var answer = rm.GetString(label, ci); // here ans="Halo" as we selected de-DE




相关问题
Bring window to foreground after Mutex fails

I was wondering if someone can tell me what would be the best way to bring my application to the foreground if a mutex was not able to be created for a new instance. E.g.: Application X is running ...

How to start WinForm app minimized to tray?

I ve successfully created an app that minimizes to the tray using a NotifyIcon. When the form is manually closed it is successfully hidden from the desktop, taskbar, and alt-tab. The problem occurs ...

Linqy no matchy

Maybe it s something I m doing wrong. I m just learning Linq because I m bored. And so far so good. I made a little program and it basically just outputs all matches (foreach) into a label control. ...

Handle DataTable.DataRow cell change event

I have a DataTable that has several DataColumns and DataRow. Now i would like to handle an event when cell of this DataRow is changed. How to do this in c#?

Apparent Memory Leak in DataGridView

How do you force a DataGridView to release its reference to a bound DataSet? We have a rather large dataset being displayed in a DataGridView and noticed that resources were not being freed after the ...

ALT Key Shortcuts Hidden

I am using VS2008 and creating forms. By default, the underscore of the character in a textbox when using an ampersand is not shown when I run the application. ex. "&Goto Here" is not ...

WPF-XAML window in Winforms Application

I have a Winforms application coded in VS C# 2008 and want to insert a WPF window into the window pane of Winforms application. Could you explain me how this is done.

热门标签