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 beet开发者_运维问答wen 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:
- Best practice to make a multi language application in C#/WinForms?
- Parsing DateTime on Localized systems
- Pluralising and Localizing strings in C#
- How do I best localize an entire app to many different languages?
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 :
Create a text file say "data.en-US.txt" in the solution (<filename>.<culture string>.txt) in the format below :
Hello=Hello
Welcome=WelcomeThis 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=WillikomenThe first column are the labels which will be used as keys for referring the text.
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- Click all the .resource files and set "Build Action" property to "Content".
- Click all the .resource files and set "Copy to Output Directory" property to "Copy Always / Copy If new".
- 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
精彩评论